Maintaining a Separate Queue for Each Key

A map of queues preserves arrival order for each key while allowing the application to choose which key to service next.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace interleaved arrivals
  2. Define scheduling across keys
  3. Keep order within each key while scheduling across keys
  4. Decide when a queue is still active

Trace interleaved arrivals

Jobs A1, B1, and A2 create a queue with A1 and A2 for key A and a queue with B1 for key B. Choosing B can serve B1 first globally while preserving the FIFO rule within each key.

Define scheduling across keys

A round-robin policy can rotate through nonempty queues. A priority rule may need fairness constraints when one key is never empty. Grouping records handles storage separately from that selection policy.[1]

Keep order within each key while scheduling across keys

Jobs A1 and A2 for customer A arrive before B1 for customer B. Each queue retains its own FIFO order. A round-robin scheduler can retrieve A1, B1, A2 when both keys remain active. The grouping rule and scheduler solve different parts of the work. See FIFO queues.

Initial: A=[A1,A2], B=[B1]
Take A1 → A=[A2], B=[B1]
Take B1 → A=[A2], B=[]
Take A2 → both queues empty

Decide when a queue is still active

An implementation may discard empty per-key containers to save memory. It must coordinate that with producers so an enqueue cannot land in an abandoned container. Test a new job arriving just after draining its key and operations for several independent keys.

References

  1. Jeff Erickson, Algorithms ↩