Hand slow work to background workers, and run jobs on a timetable.
A task queue lets your app hand slow work to background workers, so the user does not have to wait. Scheduled jobs are the same idea, started by a clock instead of by a user.
Some work is too slow or too heavy to do while a user waits: sending emails, resizing photos, making a PDF report, charging a card through a slow partner, or calling an unreliable service. Doing this inside the web request makes pages slow and fragile. A task queue moves this work out of the request.
The pieces are simple. A producer (often your web app) creates a task: a small message that says what to do and with what data. The task waits in a queue held by a broker such as Redis, RabbitMQ or Amazon SQS. A worker is a separate process that pulls tasks, runs them and reports the result.
| Step | What happens | Why it matters |
|---|---|---|
| Enqueue | The producer saves the task in the queue | The user is free to go on straight away |
| Lease | A worker takes the task, and it is hidden from other workers for a while (a visibility timeout) | Two workers do not run the same task at once |
| Run | The worker does the job | Slow work is off the request path |
| Acknowledge | On success the worker tells the queue to delete the task | The task is finished only after this |
| Retry | On failure or a lost worker the lease runs out and the task returns | Failures are not lost |
| Dead letter | After a set number of failures the task is parked | A poison task cannot block the line |
| Part | Job | Examples |
|---|---|---|
| Producer | Creates tasks | Your web app or an API |
| Broker / queue | Stores tasks safely until a worker takes them | Redis, RabbitMQ, Amazon SQS |
| Worker | Pulls and runs tasks | Celery, Sidekiq, BullMQ, or a function triggered by SQS |
| Result store (optional) | Keeps the outcome or the status of a task | A database or Redis, read by status polling |
| Dead letter queue | Parks tasks that kept failing | A second queue you can inspect and replay |
| Setting | What it means | Why it matters |
|---|---|---|
| Concurrency | How many tasks one worker runs at once | Too high overloads a downstream service. Too low wastes the machine. |
| Visibility timeout / lease | How long a task is hidden while a worker runs it | Shorter than the task, and a second worker starts it too. Much longer, and a crash is noticed late. |
| Heartbeat | A message that extends the lease for a long task | Lets a long job run without being handed to someone else. |
| Max attempts | How many times a task may fail before it is dead-lettered | Stops a poison task from retrying for ever. |
| Priority | Some queues or tasks are served first | A password-reset email should not wait behind 10,000 report jobs. |
| Rate limit | A cap on tasks per second to a partner | Protects a service that has its own limits. |
A scheduled job runs at a fixed time or every so often, for example "every night at 02:00". The hard part is not the clock. It is making sure the job runs once, even when many app instances are running.
At 02:00 the job runs 3 times.
Rule of thumb: workers needed = arrival rate x time per task. Normal load needs about 4, and the spike needs about 12. Your fixed fleet of 4 can finish about 20 tasks per second. A simplified model with a simulated clock (no random numbers). The numbers are illustrative.
loop:
task = queue.receive(leaseSeconds = 60)
if task is empty: continue
try:
if alreadyDone(task.id): queue.ack(task); continue # idempotent
process(task)
queue.ack(task) # ack AFTER success
except TransientError:
queue.releaseWithDelay(task, backoff(task.attempts))
except Exception:
if task.attempts >= MAX_ATTEMPTS: queue.deadLetter(task)| Term | Meaning in easy words |
|---|---|
| Task / job | A small unit of work, stored as a message in a queue |
| Producer | The code that puts tasks into the queue |
| Worker | A separate process that pulls tasks and runs them |
| Acknowledge (ack) | Telling the queue that a task finished, so it can be deleted |
| Visibility timeout / lease | The time a taken task stays hidden from other workers |
| Heartbeat | A signal from a worker that extends the lease of a long task |
| Dead letter queue | A parking place for tasks that failed too many times |
| Poison task | A task that fails every time it is tried |
| Backlog | The number of tasks waiting in the queue |
| Autoscaling | Adding or removing workers based on load, such as queue depth |
| Cron | A schedule format and tool for running jobs at set times |
| Choice | We gain | We lose |
|---|---|---|
| More workers | Shorter backlog and wait | More cost, and more pressure on the database or partner |
| Short lease | Crashes are noticed fast | Slow tasks may be run twice at the same time |
| Long lease | Fewer double runs | A crashed task waits a long time to return |
| Many retries | Survive long blips | A poison task wastes more work before parking |
| Priority queues | Urgent work goes first | Low-priority work can starve |
| Cron per instance | Simple to start | Duplicate runs when you scale out |