Cola de Mensajes

Cola de trabajo · Cola de tareas

Escrito por Darjus Kazakevic · revisado el

A buffer between two speeds, where each message goes to one worker.

A message queue sits between producers and workers. A producer adds a job and moves on; workers take jobs as fast as they can. Each message goes to one worker, so more workers share the load. A burst of 10,000 orders becomes a backlog that drains, not an outage. Amazon SQS, RabbitMQ and BullMQ on Redis are common choices.

The cost is delivery rules you must design for. Delivery is at least once, so workers must handle duplicates. A message that always fails needs a dead-letter queue. And backlog depth needs an alert, because a queue that only grows is a slow outage.

En esencia — Put a durable queue between producers and workers so each runs at its own pace, and judge its health by how deep the backlog is.

Cuándo recurrir a él

Recurre a él cuando

  • Producers arrive in bursts that workers cannot handle in real time
  • Work can finish after the request returns: emails, exports, resizing
  • Slow or flaky downstream calls should retry without a user waiting

Reconsidéralo cuando

  • The caller needs the result to answer its own request
  • Several services must each react to every event: use pub/sub
  • Strict order across all messages is required

Conceptos clave

At-least-once delivery
The queue deletes a message only after a worker acknowledges it. If the worker crashes first, the message is delivered again. Duplicates are normal, so handlers must be idempotent.
Visibility timeout (Ack deadline · Lease)
When a worker receives a message, the queue hides it for a set time: 30 seconds by default in SQS. If the worker has not deleted it by then, it becomes visible and another worker gets it.
Dead-letter queue (DLQ)
A second queue for messages that failed too many times, for example 5 receives. They stop wasting worker time, and someone can inspect and replay them after a fix.
Backlog depth and age
How many messages are waiting, and how old the oldest one is. Rising depth means workers are slower than producers. Age is the delay users actually feel.
Ordering
With many workers and retries, order is not guaranteed, even if the queue is roughly first in, first out. SQS FIFO queues keep order within a message group, at lower throughput.

Reglas de diseño

Ack only after the work is done
Delete or ack a message after the handler succeeds, never before. A crash then means a retry, not a lost job. Make the handler idempotent, because retries will happen.
Send poison messages to a dead-letter queue
After a fixed number of failed receives, such as 5, move the message to a DLQ and alert on it. Without one, a bad message is retried forever while workers waste time on it.
Set the visibility timeout above the slowest job
If a job can take a minute, set the timeout to about 6 minutes, or extend it while working. A short timeout lets a second worker start the same job.
Alert on depth and age, not only errors
Alert when depth keeps growing for 15 minutes or the oldest message is older than your target delay. Workers can fall behind without a single error.

Errores habituales

  • A visibility timeout shorter than the job. A second worker starts the same job while the first is still running, and both write results.
  • No dead-letter queue. One malformed message fails forever, wastes worker time on every retry, and in a FIFO queue blocks its whole group.
  • Acking on receive. The worker crashes halfway through, and the job is lost without a trace.
  • Alerting only on errors. Workers fall behind with no errors at all, and users notice hours of delay first.