Ring Buffer Backpressure
Prerequisites: SPSC Ring Buffer Handoff, Condition Variables and Producer-Consumer Queues
The feed thread pushes AAPL market updates into a fixed-size buffer, and the strategy thread pops them. During a burst the feed pushes faster than the strategy pops, and the buffer fills. Simulate what the buffer does when a push finds it full.
- Overflow policy: a fixed rule for which value loses when a push has nowhere to store its value. The buffer can keep the queued values and discard the new one, keep the new one and discard the oldest, or refuse the push and tell the producer.
- Backpressure: the last choice. The full buffer pushes back on the producer instead of losing a value without a trace.
The simulation is deterministic and single-threaded. Pushes and pops arrive in one command stream and are applied one at a time, in order. It models the decision a full buffer makes, not the synchronization between producer and consumer.
What to implement
process_commands(capacity, policy, commands)returns the printed lines, in command order. Each command is one line.- The buffer holds at most
capacityintegers (one per update) in first-in, first-out (FIFO) order. It starts empty. policyis the overflow policy, fixed for the whole run.- On stdin: the capacity
C, then the policy, then the number of commandsQ, then theQcommands, one per line. - There are three parts, and each adds a policy. One solution must pass all ten tests.
| Command | Effect | Prints |
|---|---|---|
PUSH x | Append the integer x as the newest value; if the buffer is full, apply the policy | nothing, except BLOCKED under the BLOCK policy (Part 3) |
POP | Remove the oldest value | that value, or EMPTY if the buffer is empty |
PEEK | Read the oldest value without removing it | that value, or EMPTY if the buffer is empty |
Part 1: Drop the new value
- The policy is
DROP_NEW. - A push into a full buffer is discarded. The buffer keeps the values it holds, and the push prints nothing.
- Once a
POPfrees a slot, the nextPUSHsucceeds as usual, so a later command can push a discarded value again.
Limits
Cis between 1 and 105.Qis between 1 and 2 · 105.- Every pushed value fits in a signed 32-bit integer.
Tests 1–6 cover this part. All of them use DROP_NEW.
Part 2: Prefer fresh data
Add the DROP_OLD policy. DROP_NEW must keep working.
- A push into a full buffer removes the oldest value, then appends the new one. The buffer keeps the most recent
Cvalues. - The push still prints nothing.
This suits data whose newest value matters most, such as the latest AAPL price: a strategy that has fallen behind would rather see the newest updates than stale ones.
Tests 7–8 cover this part.
Part 3: Signal backpressure
Add the BLOCK policy. POP, PEEK, and the other two policies keep their behavior.
- A push into a full buffer leaves the buffer unchanged and prints
BLOCKED, telling the producer the value was not stored. - There is no automatic retry. A later
PUSHof the same value is a separate attempt, and succeeds only if aPOPhas freed a slot in the meantime. - Only
BLOCKmakes aPUSHprint anything, and only when the buffer is full.
In a threaded program, this is the policy where the producer waits:
- with a condition variable, it would sleep until the consumer frees a slot;
- with the SPSC ring, whose
pushreturnsfalsewhen the ring is full, it would try again.
The simulation prints BLOCKED and leaves any retry to a later command.
Tests 9–10 cover this part.
2 DROP_NEW 9 PUSH 5 PUSH 6 PUSH 7 POP PUSH 8 PEEK POP POP POP
5 6 6 8 EMPTY
The buffer holds two values. PUSH 5 and PUSH 6 fill it, so PUSH 7 is discarded and prints nothing.
POP prints the oldest value, 5, which frees a slot, and PUSH 8 takes it. The buffer now holds 6 and then 8.
PEEK prints 6 and leaves it in place. The next two POP commands print 6 and 8, and the last POP finds the buffer empty.