“Substantial drift from celery worker” (logged by celery-beat / Scheduler) and “missed heartbeat” (logged by RabbitMQ or Redis) are symptoms of the same underlying problem:
- Celery workers stop responding for longer than the broker heartbeat interval.
- Because workers are silent, celery-beat also stops receiving event information and warns that its internal clock is “drifting”.
- After a few lost heart-beats RabbitMQ/Redis declares the connection dead, drops it and prints “missed heartbeat”.
- New connections are opened, tasks are often re-queued, and you see a cascade of reconnect / channel-close messages.
In nearly every case the root cause is one (or a combination) of:
• A worker process was BUSY / BLOCKED for too long
• The host was overloaded or put to sleep → CPU pauses / GC / swap
• A clock jump (virtual machines without NTP)
• The heartbeat settings are too aggressive for your workload
────────────────────────────────────────────────────────
How to fix it
────────────────────────────────────────────────────────
Step 1 ‑ Upgrade first
Many heartbeat glitches were fixed in recent versions of Celery, Kombu and eventlet/gevent.
pip install -U "celery[redis,rabbitmq]" kombu
Step 2 ‑ Make sure the worker can actually send the heartbeat
-
Long or blocking tasks
• Break gigantic tasks into smaller ones.
• For I/O heavy code use eventlet/gevent (or run it in a separate thread) so the main process is free to answer the broker.
• If you already use eventlet/gevent make sure you monkey-patch everything and never call blocking libraries that bypass the event loop (e.g. time.sleep).
-
CPU starvation
• Limit concurrency sensibly:
celery -A proj worker --concurrency 4 -P prefork
• Don’t run CPU-bound tasks with eventlet/gevent – use prefork/threads.
• Consider --autoscale or simply add more workers instead of raising --concurrency indefinitely.
-
Memory leaks → swap → stop-the-world
• Use --max-tasks-per-child 100 or similar.
• Add memory monitoring/alerts.
Step 3 ‑ Relax or tune the heartbeat
Celery passes a heartbeat value (seconds) when it connects:
# celeryconfig.py
broker_heartbeat = 30 # default is 60 for Rabbit, 10 for Redis
broker_heartbeat_checkrate = 2 # (send every hb/2)
or CLI:
celery worker -A proj --broker=amqp://guest@localhost// --heartbeat-interval 20
If you frequently hit the limit, try 60-120 s.
You can also disable broker heartbeats for testing:
broker_heartbeat = 0
(Only use this in trusted, LAN-only deployments.)
Step 4 ‑ Let Beat breathe
When beat and worker share the same process (celery worker -B), any blockage in the worker blocks the scheduler as well.
Prefer:
# one terminal / container
celery -A proj worker --concurrency 4
# second terminal / container
celery -A proj beat --scheduler django # or redis / default
If you must keep them together, increase the max loop interval:
beat_max_loop_interval = 60 # default 20
Step 5 ‑ Make sure the clocks are sane
Inside VMs / containers always run an NTP daemon or systemd-timesyncd; a leap of >1 s shows up as “substantial drift”.
Step 6 ‑ Check network and broker limits
• Long-lived TLS connections can stall on renegotiation.
• A firewall dropping idle TCP connections can look like “missed heartbeat”.
• For RabbitMQ increase heartbeat_timeout or vm_memory_high_watermark, for Redis raise timeout.
────────────────────────────────────────────────────────
Quick checklist
────────────────────────────────────────────────────────
☑ Celery ≥ 5.x, Kombu ≥ 5.x, broker client ≥ latest
☑ Separate celery beat from workers
☑ No blocking calls in eventlet/gevent tasks
☑ Prefork pool for CPU tasks; green pools for I/O tasks
☑ broker_heartbeat ≥ 30 s (or disabled)
☑ --max-tasks-per-child to avoid leaks
☑ NTP running on every host
☑ Broker and OS not starving on CPU / I/O
Fix the real bottleneck, then lower the heartbeat again if you like. When workers can respond within the interval the two warnings disappear.