Chapter 37 · Discrete teaching model
When the consumer falls behind
A queue buys time. It cannot create capacity.
Prerequisite Measure latency and batching.
Account for arrivals, service, queue growth and overflow.
The question
Checkouts produce two records per tick. The sink processes one. The queue looks healthy at first, then fills and starts rejecting new records.
Backlog is work waiting to be processed. This model puts arrivals in a fixed-size queue and drops any new record that does not fit. Real backpressure sends a signal upstream so that the producer can slow down. Here the arrival rate stays fixed.
- Offered loadArrival rate λDecide what is admitted or rejected.
- Bounded queueUnfinished workBacklog grows if admitted work exceeds completion.
- ConsumerService rate μOnly completed work leaves this population.
Conceptual diagram. This is the discrete teaching model. Count queued, completed and dropped work under its stated definitions.
Predict the result
The model runs eight ticks. Two records arrive on each of the first five ticks and none arrive on the last three. Each tick adds arrivals up to the queue limit, counts drops, then processes up to the service rate.
With capacity 3, two arrivals and one completion per tick, will a larger queue prevent loss indefinitely?
Run the experiment
The editor contains the published example. Select Run with the code unchanged.
Python prepares in the background while you read. First use can download about 84 MiB. Your code runs only when you select Run; each run starts with clean experiment state.
A saved draft is open. Select Restore code to see the published example. Recover draft brings back your draft.
Ctrl+Enter or ⌘+Enter runs the code.
Edits are saved in this browser when storage is available.
Technical error details
Your result
Console output
Troubleshooting
If Python stops responding, restart the environment and try again. Your code and last completed result are kept.
View the published result
The course build ran the published code to generate this output.
{
"trace": [
{
"tick": 1,
"offered": 2,
"completed": 1,
"queued": 1,
"dropped": 0
},
{
"tick": 2,
"offered": 2,
"completed": 1,
"queued": 2,
"dropped": 0
},
{
"tick": 3,
"offered": 2,
"completed": 1,
"queued": 2,
"dropped": 1
},
{
"tick": 4,
"offered": 2,
"completed": 1,
"queued": 2,
"dropped": 1
},
{
"tick": 5,
"offered": 2,
"completed": 1,
"queued": 2,
"dropped": 1
},
{
"tick": 6,
"offered": 0,
"completed": 1,
"queued": 1,
"dropped": 0
},
{
"tick": 7,
"offered": 0,
"completed": 1,
"queued": 0,
"dropped": 0
},
{
"tick": 8,
"offered": 0,
"completed": 0,
"queued": 0,
"dropped": 0
}
],
"dropped": 3,
"queued": 0
}Read the result
Compare offered, completed, queued, and dropped in the first five trace rows. The last three ticks have no arrivals, so any waiting work can drain. The top-level dropped adds losses across ticks.
Change the experiment
Replace capacity=3 with capacity=6 in the editor. Select Run again. Keep offered=2 and service=1. Then replace service=1 with service=2 and select Run again. Compare dropped and the queued values while arrivals continue. Which change increases storage, and which makes service keep up with offered work?
Runtime, source code and downloads
Runtime and reset details explain what stays ready and what is isolated on each run. Download Python packages the published helpers with the code in your editor.
Browse the sources for Bytewax windows and authored models, Kafka log and group exercises, live Kafka input, checkpoint recovery and processing patterns.
Check conservation before speed
For the complete run, offered equals completed plus dropped plus still queued. A larger queue that hides rejection for a short run has not fixed a sustained rate mismatch. If the two arrivals per tick continue after tick five, a queue served at one record per tick eventually fills and starts dropping records.
Work, state and recovery costs
Where used, n counts input events, k counts retained distinct keys, r counts overlapping windows per event and z counts output matches. Bounds assume the stated algorithm. Collecting, sorting and displaying results adds work.
| Question | Mechanism and boundary |
|---|---|
| Work | Eight discrete ticks; each uses scalar arithmetic. General reference work is O(t) for t ticks. |
| State | O(1) queue-depth state plus O(t) trace. It does not store actual queued payloads. |
| Movement | No broker or network. In a real job backpressure can move waiting work into the retained log. |
| Progress | The model services work after admitting arrivals each tick; the ordering is part of the contract. |
| Recovery | A real queue needs persistence/replay or a declared loss policy. Counters alone cannot recover payloads. |
Think it through
The queue is empty at the end. Does that prove every offered record was processed?
Show hints
Hint 1
Queue depth is a current state, not a delivery history.
Hint 2
Check the dropped counter.
Hint 3
Balance input against completion, drops and remaining work.
Show explanation
No. The queue may have drained after rejecting work. Across the run, offered must equal completed plus dropped plus still queued. Always inspect completeness alongside latency and queue size.
ReferencesFunction documentation and helper source
References open in a new tab; course helpers are documented separately from library APIs.
Demand and bounded queues
queue_run is an authored admission/service model. It does not execute the Reactive Streams request protocol or measure a network queue.
Course helper implementation
These are functions written for this course, not additional library APIs. Inspect their parameters, operator calls and authored policies. The source shown is generated from the same Python files packaged with Run and Download Python.
Experiment scope
These are deterministic model ticks, not broker measurements. The helper is explicit Python queue arithmetic, not a Bytewax backpressure measurement.
Primary reference →