← 返回 citadel 的题目列表Design a Low-Latency Bounded Queue and Size Its Capacity
类型:online_judge
Problem: Design a Low-Latency Bounded Queue and Size Its Capacity
Design a bounded in-memory queue for a latency-sensitive data path. Producers continuously write messages and consumers read them. The goal is to minimize enqueue/dequeue latency and avoid dynamic allocation during normal operation.
Design Requirements
State which concurrency model must be established first: SPSC, MPSC, SPMC, or MPMC.
For the SPSC (single-producer, single-consumer) case, implement or describe a fixed-capacity ring buffer that:
provides try_push(item), which fails immediately when full;
provides try_pop(), which fails immediately when empty;
uses neither mutexes nor dynamic allocation on the normal data path;
handles head/tail wraparound correctly;
explains the required C++ memory ordering and how to avoid false sharing between producer and consumer state.
Explain what must change for MPSC or MPMC, and why a naive SPSC implementation cannot be reused directly.
Derive a capacity-sizing method. Let:
B be the maximum burst size;
λ be the sustained producer rate after the burst, in messages/second;
μ be a conservative lower bound on consumer throughput, in messages/second;
T_pause be the longest consumer pause;
T be the load window during which no loss is required.
Give the capacity requirement for both λ > μ and λ <= μ. Explain why a finite queue alone cannot guarantee no overflow when λ > μ persists indefinitely, and describe the safety margin that should be added.
Example
For B = 10,000, λ = 800,000/s, μ = 1,000,000/s, and T_pause = 5ms, the lower bound on backlog caused by the burst and consumer pause alone is:
10,000 + 800,000 × 0.005 = 14,000
The real capacity should additionally include headroom for measurement error, scheduling jitter, and recovery behavior, and is commonly rounded up to a ring-buffer-friendly capacity.