A Queue and First-In, First-Out Order

A FIFO queue exposes the oldest retained element. New items enter behind all pending items.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a sequence of arrivals
  2. Choose the endpoint operations
  3. Observe each removal rather than only the final state
  4. Separate retrieval order from completion order

Trace a sequence of arrivals

Append A and B, remove A, then append C. The remaining order is B, C. An arrival after processing begins should follow B unless another priority rule is part of the contract. Work records can carry identifiers and inputs together.

Choose the endpoint operations

A deque can append at one end and remove from the other. A stack using a single end would yield B before A on the first removal. Last-in, first-out order explains the alternative.[1]

Observe each removal rather than only the final state

Put upload jobs A, B, C in a queue in that order. After dequeuing A, insert D. A correct FIFO queue removes B next even though D is the newest item. An implementation that accidentally uses the same end for insertion and removal behaves as a stack. See stack order.

Front → back
enqueue A, B, C: [A, B, C]
remove: A, remaining [B, C]
enqueue D: [B, C, D]
drain: B, C, D

Separate retrieval order from completion order

With multiple workers, B can finish before A even if A was retrieved first. Promise FIFO retrieval when that is what the queue can guarantee. If downstream effects must appear in order, test that requirement independently under different processing times.

References

  1. Jeff Erickson, Algorithms ↩