Work Queues Learn
Distributed systems

Work Queues

A work queue lets producers hand off jobs and keep going. Workers take the jobs at their own pace, so a slow job never blocks the caller.

8 min · 3 sources

Map · the whole pipeline

Producer, queue, workers, result store

Simplified
Producer 1 Queue 2 Worker A Worker B 3 Results
  1. The producer enqueues a job and returns at once.
  2. Each idle worker takes the next job. The queue holds the rest.
  3. A worker writes the result. Other services read it from the store.
What to notice: the queue is the only part both sides touch. So producers and workers can run at different speeds.

The idea in five steps

  1. One lineA queue separates who asks for work from who does it.Read →
  2. WhyA caller that waits for slow work stalls whenever the work is slow.Read →
  3. MechanismWorkers pull jobs, and the queue buffers bursts.Read →
  4. Trade-offYou gain throughput and lose an immediate answer.Read →
  5. PrincipleIt is a bounded buffer between two clocks, like a Go channel.Read →
Problem · Rung 2

A slow job blocks its caller

If each caller waits for a 2-second job, 100 requests a second keep 200 callers waiting.

Each waiting caller holds a thread and a connection. So the load you can serve falls as the jobs get slower. Little's law gives the count: L = \lambda W, jobs in flight equal the arrival rate times the wait.

Arrivals stay at 100 a second, and the job time doubles to 4 s. How many callers wait at once?

Little's law: 100 a second times 4 s is 400 in flight.
The count in flight grows with the wait. So doubling the wait doubles it.
Mental model · Rung 3

A buffer between two clocks

The producer runs on its clock, and the workers run on theirs. The queue absorbs the difference.

Producers enqueue 120 jobs a second. Two workers finish 50 a second each. What happens to the queue?

In 120, out 100. The backlog grows by 20 a second until you add a worker.
It holds steady only when arrivals equal the service rate.

Work queue

Producers enqueue, and a fixed pool of workers dequeues. A full queue pushes back on producers.

Counterpart

jobs := make(chan Job, 100) // the queue
for w := 0; w < 2; w++ {
    go func() { for j := range jobs { run(j) } }()
}
jobs <- j // blocks when the buffer is full

In the Go version, what happens when all 100 buffer slots are full?

A full buffered channel blocks the sender. That is backpressure.
Go never drops a send on a channel. The sender waits.
Trade-offs · Rungs 4 and 5

What the queue costs

You trade an immediate answer for throughput. You must also handle jobs that fail after you have returned.

Anti-patterns

  • An unbounded queue. It hides overload until memory runs out. Bound it and push back.
  • Side effects without an idempotency key. A retried job charges the card twice.

A worker crashes after it charges a card but before it acknowledges the job. What prevents a second charge on retry?

The payment service sees the same key and returns the first result.
Queue size does not change delivery. The job comes back either way.
Use it

Notes and sources

  1. Little, J. D. C. "A Proof for the Queuing Formula L = λW." Operations Research, 1961.
  2. The Go Programming Language Specification, "Channel types".
  3. Kleppmann, M. Designing Data-Intensive Applications. O'Reilly, 2017.