Chapter 36 · Deterministic queueing model
Measure latency and batching
A higher throughput can hide a longer wait.
Prerequisite Measure operation effort.
Compute completed-event latency quantiles and explain batching, waiting and omitted populations.
The question
Batching reduces repeated setup work. It also makes early events wait for a batch to fill. A faster total run can make one customer’s order appear later.
The model has eight fixed arrival times. A single first-in, first-out server spends two milliseconds setting up each batch and three milliseconds processing each record. A batch is ready when its last record arrives. All its records finish together. A latency percentile summarizes completion times relative to arrivals. For example, the nearest-rank 99th percentile (p99) is the largest of these eight latencies.
- ArrivalWork becomes eligibleStart the end-to-end clock at a declared point.
- Batch assemblyWait for more workLarger batches can add queueing delay.
- ServiceAmortize fixed overheadThroughput can improve.
- CompletionObserve latency tooFaster throughput need not mean faster first results.
Conceptual diagram. This is the labelled deterministic latency model. Its computed milliseconds are not measured CPU or network performance.
Predict the result
Records arrive at 0, 1, 2, 10, 11, 12, 20 and 21 ms. The server spends 2 ms setting up each batch and 3 ms processing each record.
If batch grows from 1 to 4, will the first event finish earlier because setup work is shared?
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": [
{
"id": 0,
"arrived_ms": 0,
"start_ms": 0,
"completed_ms": 5,
"latency_ms": 5
},
{
"id": 1,
"arrived_ms": 1,
"start_ms": 5,
"completed_ms": 10,
"latency_ms": 9
},
{
"id": 2,
"arrived_ms": 2,
"start_ms": 10,
"completed_ms": 15,
"latency_ms": 13
},
{
"id": 3,
"arrived_ms": 10,
"start_ms": 15,
"completed_ms": 20,
"latency_ms": 10
},
{
"id": 4,
"arrived_ms": 11,
"start_ms": 20,
"completed_ms": 25,
"latency_ms": 14
},
{
"id": 5,
"arrived_ms": 12,
"start_ms": 25,
"completed_ms": 30,
"latency_ms": 18
},
{
"id": 6,
"arrived_ms": 20,
"start_ms": 30,
"completed_ms": 35,
"latency_ms": 15
},
{
"id": 7,
"arrived_ms": 21,
"start_ms": 35,
"completed_ms": 40,
"latency_ms": 19
}
],
"p50_ms": 13,
"p99_ms": 19,
"completed": 8,
"elapsed_ms": 40,
"note": "Modeled costs; nearest-rank quantiles of completed records."
}Read the result
Read arrived_ms, start_ms, completed_ms, and latency_ms for the first record in trace. elapsed_ms is the time until the entire fixture finishes. p50_ms and p99_ms summarize per-record waits and processing, with p99 equal to the maximum for these eight records.
Change the experiment
Replace batch=1 with batch=2 in the editor. Select Run again. Then replace batch=2 with batch=4 and select Run again. Across all three runs, compare elapsed_ms, p50_ms and p99_ms. Compare the first record’s latency_ms as well. Can the fixture finish sooner while that record takes longer?
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.
Choose a measurement boundary
Event-to-visible-result latency includes every wait in the chosen path. Operator service time excludes upstream queues and downstream visibility. Polling a database adds detection delay. Record IDs connect observations; timestamp definitions and clock uncertainty determine whether subtraction is meaningful.
Run a real local measurement too
The Field notes link a native Bytewax harness that verifies answers before reporting service throughput. Run independent trials and save raw observations. It does not generate an open-loop distributed load or establish Kafka/Flink capacity; that needs a real topology, controlled offered load and source-to-sink verification.
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 | O(n) scheduling records plus O(n log n) sorting for exact quantiles. Model service costs are authored parameters. |
| State | O(n) per-event observations. Real online quantile sketches trade bounded space against approximation. |
| Movement | The single server and its queue are modeled in Python using fixed arrival times. |
| Progress | Every member of a batch becomes visible at its modeled completion time. |
| Recovery | The model tracks one uninterrupted schedule. Use a separate failure model to estimate unfinished work or replay. |
Think it through
Can p99 among completed events look healthy while the system is overloaded?
Show hints
Hint 1
Look for records missing from the observed population.
Hint 2
A finite observation can omit all the records still queued.
Hint 3
Failure and timeout counts belong beside quantiles.
Show explanation
Yes. A completed-only population can omit the slowest unfinished or dropped events. Report offered, admitted, completed, rejected and still-pending counts alongside duration and quantiles. In a real load generator, distinguish an externally scheduled arrival process from a closed-loop client that waits before sending more work.
ReferencesFunction documentation and helper source
References open in a new tab; course helpers are documented separately from library APIs.
Choose the measured boundary
latency_run is a deterministic schedule model. Read the monitoring guidance before interpreting average completion, first-item delay or tail latency as interchangeable metrics.
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 computed model milliseconds, not measured browser latency or a benchmark of Flink/Kafka.
Primary reference →