Celery Beat
Celery Beat schedules periodic work by publishing task messages to a broker; Celery workers execute those messages. Operate the scheduler deliberately and make periodic tasks robust to overlap and duplicate delivery.
Celery Beat is a scheduler that checks configured schedules and publishes task messages when they are due. A Celery worker receives those messages and executes the tasks, so scheduling and execution are separate responsibilities.
Schedules can be defined in application configuration, including interval-based schedules and calendar-like schedules such as crontab expressions. Beat sends a task name and any configured arguments through the broker; the task must be registered and importable by workers.
A typical deployment runs one active Beat scheduler for a given schedule set. Running multiple schedulers against the same schedule can publish duplicate messages because Beat does not generally coordinate ownership across instances. High availability requires an approach that provides appropriate coordination or leader election.
Beat does not guarantee that a prior invocation has finished before the next occurrence is published. If a task takes longer than its interval, executions may overlap unless the task or deployment uses locking, idempotency, or another concurrency control. Treat periodic deliveries as potentially repeated and design task effects accordingly.
Time-zone configuration matters for calendar schedules, and changing time-zone or schedule settings can affect when tasks are next due. Some scheduler implementations persist schedule state; operational behavior, persistence, and supported schedule types depend on the chosen scheduler and configuration.
Key points
- Beat decides when periodic tasks are due and publishes their messages.
- Workers execute scheduled tasks; the broker transports their messages.
- Use one coordinated scheduler per schedule set unless the scheduler design explicitly supports multiple instances.
- Periodic execution can overlap, so account for concurrency and duplicate effects.
- Choose schedule and timezone settings to match the intended wall-clock or elapsed-time behavior.
Interview questions
What is Celery Beat, and how does it relate to a Celery worker?
Celery Beat evaluates periodic schedules and publishes task messages when entries become due. Workers consume those messages and execute the task functions. This separation lets the scheduler and execution capacity scale or be operated independently. The broker is the transport between them.
How would you configure a task to run periodically?
Define a Beat schedule entry with a task name and a schedule, optionally including arguments or keyword arguments. The task must be registered with the Celery application and available to workers. Intervals work well for elapsed-time cadence, while crontab-style schedules express calendar times. Configure the timezone deliberately when calendar timing matters.
How would you operate Beat reliably in a production deployment?
Running two schedulers with the same schedule can cause both to publish the same due task, since ordinary Beat operation does not provide distributed ownership across instances. A common deployment runs one active scheduler for that schedule. For failover, use a scheduler or orchestration design that coordinates leadership, and still make tasks safe against duplicate delivery. Monitoring should distinguish scheduler liveness from successful task execution.
What can happen when a periodic task takes longer than its schedule interval?
Beat publishes due occurrences without waiting for earlier task executions to finish. If execution duration exceeds the schedule interval, multiple instances may run concurrently, depending on worker concurrency and queueing. Prevent harmful overlap with an appropriate distributed lock, task-level concurrency control, or a schedule that accounts for execution time. Make operations idempotent as well, because retries and duplicate delivery can still happen.