Backpressure Queue Simulation

medium · distributed-systems, resilience, sre

Backpressure Queue Simulation

You have a bounded queue with capacity C. Each tick provides:

  • Arrivals: number of incoming requests
  • Service: number of requests the system can process

Rules per tick (in order):

  1. Add arrivals to the queue.
  2. If queue exceeds capacity, drop the excess (load shedding).
  3. Process up to Service requests from the queue.

Return the total dropped requests and the max queue size observed (after arrivals and shedding, before service).

Types

type Tick struct {
    Arrivals int
    Service  int
}

Function signature

func BackpressureSim(capacity int, ticks []Tick) (dropped int, maxQueue int)

Example

capacity = 5
ticks = [
  {Arrivals: 3, Service: 1},  // queue: 3 -> process 1 => 2
  {Arrivals: 5, Service: 2},  // queue: 7 -> drop 2 => 5 -> process 2 => 3
]

Dropped = 2
Max queue = 5

Constraints

  • 0 <= len(ticks) <= 100000
  • 0 <= Arrivals, Service <= 1_000_000
  • 0 <= capacity <= 1_000_000
Run tests to see results
No issues detected