Measuring efficiency: query scheduling and caching
Measuring and improving query efficiency uses queueing theory (Little's Law, M/M/1), scheduling policies, and caching metrics (hit rate, miss penalty) to reduce latency and increase throughput in IR systems.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Search engineers use query scheduling and caching to make search systems fast and cost-effective. Good scheduling reduces user-perceived latency (time to get a result) and increases throughput (queries served per second); good caching reduces work (I/O and CPU) by reusing earlier results or index data.
Key ideas
Throughput: number of queries completed per unit time. Measured in requests per second (req/s).
Arrival rate (λ): average number of queries arriving per second. Unit: req/s.
Service rate (μ): average number of queries a server can complete per second when busy. Unit: req/s.
Utilisation (ρ): fraction of time the server is busy. ρ = λ / μ (dimensionless).
Number in system (L): average number of queries in the system (waiting + in service). Unit: queries.
Response time (W): average time a query spends in the system (latency). Unit: seconds.
Cache hit rate (h): fraction of accesses served from cache (0 ≤ h ≤ 1), unitless.
Miss rate (m): fraction of accesses that miss the cache, m = 1 − h.
Miss time (T_miss): total time to serve a request on a cache miss (miss path, e.g., disk fetch + processing). Unit: seconds.
Hit time (T_hit): time to serve a request from cache. Unit: seconds.
Queueing fundamentals and exact statements universities expect:
Little's Law (exact statement): For a stable queueing system in steady state, the long-run time-average number of items in the system L equals the arrival rate λ multiplied by the long-run average time W an item spends in the system: L = λ · W. Assumptions: system is stable (arrival rate equals departure rate on average), averages exist, measurements over the same long time window. Derivation sketch often required: consider time-averages or sample-path argument counting total customer-time and dividing by total time; show equivalence to λ times average sojourn time.
M/M/1 queue (mean formulas and assumptions): Model with Poisson arrivals (rate λ) and exponential service times (rate μ), single server, infinite buffer, first-come-first-served (FCFS). Stability requires λ < μ. Commonly used exact formulas:
- Mean number in system: L = ρ / (1 − ρ)
- Mean response time: W = 1 / (μ − λ)
- Mean number in queue (waiting only): L_q = ρ^2 / (1 − ρ)
- Mean waiting time in queue: W_q = ρ / (μ − λ) Assumptions must be stated to use these formulas. Derivation: from birth–death steady-state probabilities P_n = (1 − ρ) ρ^n, then use sums Σ n P_n and W = L/λ (Little's Law).
Scheduling policies (exact definitions):
- FIFO (First-In First-Out): serve queries in arrival order. Fair, simple.
- SJF (Shortest-Job-First): serve query with smallest estimated service time next. Minimises average response time among non-preemptive policies if service times are known exactly. Assumption: accurate job size estimates.
- SRPT (Shortest-Remaining-Processing-Time): preemptive version of SJF; optimal for minimizing mean response time when remaining sizes are known.
- Priority scheduling: each query given a priority; higher priority served first; requires tie-breaking rules and may cause starvation.
- EDF (Earliest-Deadline-First): schedule query with earliest deadline when queries have deadlines.
For each, state trade-offs: SJF/SRPT optimise mean latency but can starve long queries; FIFO is fair but may increase mean latency.
Caching in IR systems (layers and policies):
- Layers: Query result cache (stores final ranked list for full query strings), Posting-list cache (stores portions of inverted lists in memory), Document/snippet cache (stores rendered snippets), Block cache (cache blocks of postings). Explain when each is effective: result cache is great for repeated identical queries; posting-list cache helps when queries share terms.
- Eviction policies (definitions): LRU (Least Recently Used): evict item not used for the longest time. LFU (Least Frequently Used): evict item with smallest access count. FIFO: evict oldest item. Belady's MIN (optimal): evict item whose next use is farthest in the future — optimal offline policy (requires perfect future knowledge). Statement of optimality: MIN minimises misses for a given request trace; proof by exchange argument (swap an eviction that differs from MIN and show no worse outcome). Assumptions: fixed trace, deterministic decisions, finite cache size.
Average access time with caching (exact formula used in exams): If T_hit is time on a hit and T_miss is the total time on a miss (miss path), then average access time T_avg = h·T_hit + (1 − h)·T_miss. If instead T_pen denotes a miss penalty (extra time over a hit), use T_avg = T_hit + (1 − h)·T_pen. State which model you use.
Query batching and merging: grouping similar queries or merging term-at-a-time work to reuse postings scans. Define query batching: grouping a set of incoming queries and processing them together to exploit common work (same terms). Trade-off: increases throughput at cost of per-query latency.
Tail latency and SLAs: Tail latency refers to high-percentile response times (e.g., 95th, 99th percentile). Systems often optimise to meet Service Level Agreements (SLA) on p99 latency, not just mean latency.
Diagrams (what to draw for exam answers):
- For queueing: draw a FIFO queue (horizontal line of boxes) feeding a single server (rectangle labelled "server"). Label arrival rate λ at left arrow, departures at right, average number in system L above the queue/server, average response time W below, service rate μ on the server. For M/M/1 steady-state, annotate ρ = λ/μ.
- For caching: draw two layers: client query → cache (hit path with time T_hit) and miss path to disk/index (time T_miss) then back to cache. Label hit rate h and miss rate 1 − h.
Be explicit about assumptions for each theorem or formula: Poisson arrivals, exponential service, steady state, known job sizes, etc.
Formulas
L = λ · W — Little's Law; L = average number in system (queries), λ = arrival rate (req/s), W = average response time (s); applies in steady state.
ρ = λ / μ — Utilisation; λ = arrival rate (req/s), μ = service rate (req/s); dimensionless; valid when μ > 0.
W = 1 / (μ − λ) — Mean response time in M/M/1; W in seconds, μ and λ in req/s; requires Poisson arrivals, exponential service, λ < μ.
L = ρ / (1 − ρ) — Mean number in system (M/M/1); L in queries, ρ dimensionless; requires M/M/1 assumptions.
T_avg = h·T_hit + (1 − h)·T_miss — Average access time with caching; T_avg, T_hit, T_miss in seconds; h is hit rate.
Throughput ≈ λ_completed = λ * success_fraction — Throughput equals arrival rate times fraction of queries completed (for stable systems fraction ≈1).
W_q = ρ / (μ − λ) — Mean waiting time in queue (M/M/1); seconds.
Worked examples
M/M/1 queue mean response time Given: arrival rate λ = 40 req/s, service rate μ = 50 req/s. Units: req/s. Formula:
W = 1 / (μ − λ). Compute: μ − λ = 50 − 40 = 10 req/s. So W = 1 / 10 = 0.1 s. Average number in system L = λ · W = 40 * 0.1 = 4 queries. Average waiting time in queue W_q = ρ / (μ − λ). First ρ = λ/μ = 40/50 = 0.8. Then W_q = 0.8 / 10 = 0.08 s. Answer: Mean response time 0.1 s, mean number in system 4 queries, mean waiting time 0.08 s.Cache hit rate and average access time Given: a result cache with hit time T_hit = 2 ms (0.002 s), miss time T_miss = 50 ms (0.05 s) on a miss path (includes fetching and ranking). Hit rate h = 0.85. Formula:
T_avg = h·T_hit + (1 − h)·T_miss. Compute: T_avg = 0.85·0.002 + 0.15·0.05 = 0.0017 + 0.0075 = 0.0092 s = 9.2 ms. Answer: Average access time = 9.2 ms.Scheduling: SJF vs FIFO (simple numeric) Two queries A and B arrive at time 0. Service times: A = 0.5 s, B = 2.0 s.
- FIFO order (A then B): Completion times: A finishes 0.5 s, B finishes 0.5 + 2.0 = 2.5 s. Response times: A=0.5, B=2.5. Mean = (0.5 + 2.5)/2 = 1.5 s.
- SJF order (A then B) is same as FIFO here because A is shorter. If arrival reversed (both arrive at 0 but compare order): suppose FIFO served B then A: B finishes 2.0, A finishes 2.5. Mean = (2.0 + 2.5)/2 = 2.25 s. SJF yields smaller mean response time. Show calculation steps and final answers. Answer: SJF reduces mean response time from 2.25 s to 1.5 s in this example.
Common mistakes
- Using T_miss that does not include T_hit without saying so — be explicit.
- Thinking L_q = λ / (μ − λ) for M/M/1; correct is L_q = ρ^2 / (1 − ρ) and W_q = ρ / (μ − λ) (while L = λ·W = ρ / (1 − ρ)).
- Assuming cache hit rate h increases linearly with cache size without considering access distribution (e.g., Zipf-like distributions).
- Claiming Belady's MIN is implementable online — it is optimal but requires future knowledge (offline only).
- Ignoring tail latency: optimising for mean response time can leave p99 very high.
For your exam
Types of questions:
- Definitions: define latency, throughput, hit rate, miss penalty — include units and simple example.
- Short notes: compare scheduling policies (FIFO, SJF, SRPT) and trade-offs; layers of caching in IR systems.
- Derivations: derive Little's Law from first principles; derive M/M/1 steady-state probabilities then L and W formulas; prove why SRPT minimises mean response time (sketch).
- Numericals: compute W and L for M/M/1 given λ and μ; compute average access time given hit rate and times; compute effect of changing cache hit rate.
- Diagrams: draw queueing diagram (label λ, μ, L, W) or caching two-path diagram (hit vs miss). Always label units.
What a full-marks answer includes:
- Clear definitions with units, explicit assumptions for formulas used, step-by-step derivation where requested, correct algebra, labeled diagram drawn exactly as described above, and final numeric answers with units (and bolded if asked to highlight). If a caching question, state whether T_miss includes T_hit.
Quick check
- State Little's Law and one necessary assumption.
- For an M/M/1 queue with λ = 30 req/s and μ = 40 req/s, compute mean response time W in seconds.
- If cache hit time is 1 ms, miss time (total miss path) 30 ms, and hit rate 90%, what is average access time (in ms)? Answers: Little's Law: L = λ · W; assumption: system in steady state (stable).; W = 1/(μ − λ) = 1/(40 − 30) = 0.1 s.; T_avg = 0.9·1 + 0.1·30 = 0.9 + 3 = 3.9 ms.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?