Performance: Redis as message broker
This affects the responsiveness and throughput of message passing between Django components, impacting interaction speed and background task handling.
Jump into concepts and practice - no test required
from celery import shared_task from django.core.mail import send_mail @shared_task def send_notification(): send_mail('Subject', 'Message', 'from@example.com', ['to@example.com']) # Triggered asynchronously via Redis broker
from django.core.mail import send_mail def send_notification(): send_mail('Subject', 'Message', 'from@example.com', ['to@example.com']) # Called directly in a view or request handler
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous task execution in request | Minimal | 0 | 0 | [X] Bad |
| Asynchronous task via Redis broker | Minimal | 0 | 0 | [OK] Good |
updates?publish, not subscribe, send, or receive.publish('updates', 'New data available') sends the message to the 'updates' channel correctly.chat channel?
import redis
r = redis.Redis()
pubsub = r.pubsub()
pubsub.subscribe('chat')
for message in pubsub.listen():
if message['type'] == 'message':
print(f"Received: {message['data'].decode()}")
breakimport redis
r = redis.Redis()
pubsub = r.pubsub()
pubsub.subscribe('notifications')
for message in pubsub.listen():
print(message)