12.3 Latency versus Throughput
Latency is the time for one operation to complete. Throughput is the number of operations completed per unit time. These two goals often conflict. Optimizing for one frequently costs the other. An interactive data analysis workload cares most about latency. A batch processing pipeline cares most about throughput.
12.3.1 Latency and responsiveness
Latency is how long you wait for a result. You run an R command. The REPL waits. You stare at the screen. The time from when you press Enter to when the answer appears is latency.
For interactive work, latency matters more than throughput. If a command takes 2 seconds, you are annoyed. You lose your train of thought. If a background job takes 200 seconds but uses resources efficiently, you can tolerate it because you can walk away.
Latency-optimized systems minimize idle time on critical paths. They do not wait for large batches. They process each request as quickly as possible, even if the average throughput is lower.
Common scenarios where latency dominates:
- Interactive data exploration in RStudio or Jupyter.
- User-facing web applications serving HTTP requests.
- Real-time control systems where delays cause errors.
12.3.2 Throughput and batch processing
Throughput is work per unit time. A batch processing pipeline with 1000 data files does not care if each file takes 2 seconds or 5 seconds. The pipeline cares about total time for all 1000 files. If processing 100 files in parallel takes 50 seconds total instead of sequential 200 seconds, the throughput improved 4x.
Throughput-optimized systems keep all workers busy. They use batching. They sacrifice latency for better resource use. A file sits in a queue waiting for a worker to finish with the previous file. The file waits, but the system as a whole moves fast.
Common scenarios where throughput dominates:
- Data processing pipelines.
- Scientific computing jobs.
- Distributed computing clusters.
12.3.3 The tradeoff between latency and throughput
A system optimized for latency is often inefficient. Each request runs independently. Setup and teardown cost happen for each request. Worker pools sit partially idle waiting for work. Expensive resources are underutilized in service of speed.
A system optimized for throughput batches requests. Many requests run together. Workers stay busy. But if your request is in a large batch, you wait for all previous requests to finish before your answer arrives. Worst-case latency is bad.
This is why an interactive analysis environment in RStudio uses different strategies than a high-throughput batch system. An analysis session in RStudio delays each command as little as possible. A distributed MapReduce job batches work across thousands of workers for throughput.
12.3.4 Worked example: worker pool latency and throughput
You build an R worker pool with ten workers. Your application submits one short task at a time. Each task takes 1 millisecond to run. Worker startup takes 100 milliseconds.
A fresh worker per task: - Start one worker for each task, run the task, shut the worker down. - Latency per task: 100 ms startup + 1 ms execution + shutdown overhead = roughly 101 ms. - Throughput: roughly 10 tasks per second (limited by startup cost).
A warm pool: - Start all ten workers upfront. - Queue tasks. Workers pull tasks and process them continuously. - Latency for the first task: 100 ms startup + 1 ms execution = 101 ms. - Latency for a task that arrives later, once the pool is warm and a worker is free: 1 ms. - Latency for the last of 100 tasks submitted at once: the ten workers clear them in ten waves of 1 ms, so about 110 ms counting startup. Arrival pattern decides which of these two numbers you get. - Throughput: up to 10,000 tasks per second, if nothing else costs time (ten workers, each finishing a 1 ms task every millisecond). In practice the cost of sending each task to a worker and its result back lowers this. - Amortized startup: 100 ms ÷ 10,000 tasks = 0.01 ms per task.
The warm pool wins on both counts here, because the fresh-worker approach pays startup on every task. The two goals start to conflict once the pool has to decide how much work to hand out at a time, which the rest of this page covers. For interactive analysis where you submit a few tasks per session, latency dominates. For a server processing thousands of requests per hour, throughput dominates.
12.3.5 Task granularity
Task granularity is how much work one task contains. Fine-grained tasks are small. Coarse-grained tasks are large.
Fine-grained tasks spread evenly across workers, since no single task can hold a worker for long, and the first results come back early. Their weakness is per-task overhead: every task pays to be sent to a worker and to send its result back, and when a task does only a millisecond of work, that cost can match the work itself.
Coarse-grained tasks amortize that overhead, which favors throughput. Their weakness is load balance: a single slow task can hold one worker long after the others have finished.
12.3.6 Failure modes
A system that optimizes for latency often leaves resources idle. An interactive R session keeps a worker ready for each command. That worker is busy for 2 seconds and idle for 30 seconds while you type. Over an hour, it does under 4 minutes of useful work and sits idle for the other 56.
A system that optimizes for throughput creates long tail latencies. If you batch 1000 tasks and submit them all at once, the last one waits for every wave ahead of it. With ten workers that is 99 waves, not 999 tasks, but the wait still grows with the batch and the task duration. In a large enough batch the last task waits minutes for its turn.
A developer builds a batch system and measures median latency (500 milliseconds). This looks fast. The 99th percentile latency is 30 seconds because of batching. The developer does not measure tail latencies and ships the system. Users submit tasks and wait. Some tasks complete fast. Others sit in the queue so long that users give up.
12.3.7 Batch size and the latency-throughput tradeoff
In a throughput-optimized system, batch size controls the latency-throughput tradeoff. Large batches (1000 tasks) maximize throughput but add latency for the last task. Small batches (10 tasks) reduce latency for the last task but hurt throughput.
The best batch size depends on task arrival rate and compute cost. If tasks arrive slowly (one per second), waiting to fill a large batch adds latency, so small batches serve them better. If tasks arrive in bursts, large batches amortize startup cost.
For an R parallel system, setting the work chunk size (how many tasks per worker) controls this. Small chunks favor latency. Large chunks favor throughput.
12.3.8 Response time percentiles matter more than average
Average latency can hide bad tail latency. A system with average latency 100 milliseconds sounds good. If the 99th percentile latency is 30 seconds, users are unhappy.
A well-tuned system has its median, 99th and 99.9th percentiles close to each other. A poorly tuned one has a 99th percentile much larger than its median.
12.3.9 Measurement and reporting
Latency and throughput require different metrics. Report both if your system serves both goals.
For latency-focused systems, report percentiles:
- Median (50th): typical latency.
- 99th percentile: common delays.
- 99.9th percentile: rare outliers.
- Max: worst case.
For throughput-focused systems, report:
- Operations per second.
- Total time for a batch.
- Efficiency (actual vs theoretical maximum).
Percentiles say nothing about throughput, and throughput says nothing about the tail, which is why a report needs both.
12.3.10 Relevance to R parallelism
R’s parallel package can use either approach, and its defaults lean toward throughput. parLapply() and mclapply() both split the input into one chunk per worker by default. parLapply(chunk.size = ) and parLapplyLB() hand out smaller chunks, which balances load at a higher per-task cost. mclapply(mc.preschedule = FALSE) goes furthest and forks one child per element.
An interactive analysis session in RStudio needs low latency. A data processing pipeline running overnight needs throughput. The same R code can have different optimal chunk sizes depending on use case and user expectations.
Setting the chunk size and pool size requires understanding this tradeoff. Large pools with small chunks waste resources for interactive work. Small pools with large chunks frustrate users who expect responsiveness.
sunlight (the R package) uses prewarmed workers to reduce startup latency. This favors latency-optimized workloads. The tradeoff is that idle workers consume memory and resources.