Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Queueing Theory

Queueing theory is the mathematical study of waiting lines. It provides formal models to predict latency, throughput, and resource utilization in systems where requests contend for shared resources. Every time a request waits for a database connection, a thread, or a CPU core, it enters a queue — and queueing theory tells you what happens next.

Why Software Engineers Should Care

You don’t need to solve differential equations daily, but queueing theory gives you:

  • Intuition for why latency explodes near 100% utilization (it’s not linear)
  • Formulas for sizing resources (thread pools, connection pools, queue depths)
  • Vocabulary for system design discussions (utilization, arrival rate, service time)
  • Models for capacity planning that go beyond “let’s add more machines”

The core insight: as utilization approaches 100%, latency goes to infinity. This is not a software bug — it’s mathematics.

Kendall’s Notation

Queueing models are described using Kendall’s notation A/S/c/K/N/D:

SymbolMeaningCommon Values
AArrival processM (Markovian/Poisson), D (Deterministic), G (General)
SService time distributionM (Exponential), D (Constant), G (General)
cNumber of servers1, 2, …, ∞
KSystem capacity (max items)∞ (default), or finite
NPopulation size (possible arrivals)∞ (default), or finite
DQueue disciplineFIFO (default), LIFO, PRI, PS

The most common models:

ModelMeaningReal-World Analog
M/M/1Poisson arrivals, exponential service, 1 serverSingle-threaded request handler
M/M/cSame, but c parallel serversc-thread worker pool
M/M/∞Infinite servers (no queueing)Serverless auto-scaling
M/D/1Deterministic (constant) service timeFixed-size packet processing
M/G/1General service time distributionMost real systems

Core Terminology

TermSymbolDefinition
Arrival rateλ (lambda)Average requests arriving per second
Service rateμ (mu)Average requests a single server can handle per second
Utilizationρ (rho)Fraction of time the server is busy: ρ = λ / (c × μ)
Average number in systemLIncluding those being served and waiting
Average number in queueL_qOnly those waiting
Average time in systemWWait time + service time (“sojourn time”)
Average wait in queueW_qTime spent waiting before service starts

Little’s Law

Little’s Law is the most fundamental result in queueing theory, and it applies to any stable system:

L = λ × W
  • L: average number of items in the system
  • λ: average arrival rate
  • W: average time an item spends in the system

Rearranged: W = L / λ or λ = L / W

Example: Sizing a Thread Pool

A service receives λ = 1000 requests/sec. Average processing time (service time) is 50ms, so W ≈ 50ms = 0.05s.

L = λ × W = 1000 × 0.05 = 50

On average, 50 requests are in the system at any time. So you need at least 50 threads. In practice, add headroom for variance: 60-75 threads.

Real-World Applications

SystemQueue ModelWhat You Optimize
Load balancerM/M/c (c = backend servers)Number of backends, queue depth before 503
Database connection poolM/M/c (c = max connections)Pool size, timeout settings
Request queue (RabbitMQ)M/M/c (c = consumers)Consumer count, prefetch count
Thread poolM/M/c (c = threads)Core/max pool size, rejection policy
TCP backlogM/M/c/K (finite buffer)somaxconn, tcp_max_syn_backlog
ServerlessM/M/∞ (ideal) or M/M/c with auto-scalingScale-to-zero, cold start latency

Topics in This Section

TopicDescription
FundamentalsPoisson processes, M/M/1 and M/M/c formulas, queue disciplines
Applied SystemsConnection pools, thread pools, load balancers, backpressure
Interview QuestionsCurated questions from beginner to advanced

References

  • Kleinrock, L. Queueing Systems, Volumes 1-2. Wiley, 1975.
  • Harchol-Balter, M. Performance Modeling and Design of Computer Systems. Cambridge, 2013. free preprint chapters
  • Gunther, N. Analyzing Computer System Performance with Perl::PDQ. Springer, 2005.
  • Wikipedia: Queueing theory

Interview Questions

  1. What is Little’s Law? Give a practical example of using it.
  2. What happens to latency as utilization approaches 100% in an M/M/1 queue?
  3. How would you size a database connection pool for a service with 500 QPS and 10ms average query time?
  4. What is Kendall’s notation? Explain M/M/c.
  5. Why can’t you achieve 100% utilization in a queueing system without infinite latency?