Design a producer-consumer pipeline with a bounded buffer. What do you do when the buffer is full?
A producer-consumer bounded buffer applies backpressure to the producer instead of letting an unbounded queue absorb overload until the process dies. Deciding what a full buffer does - block, drop, or reject - is the decision that determines how the system fails.
Answer
Short answer
In a producer-consumer bounded buffer, the queue capacity is the backpressure mechanism. When the buffer is full, the producer must either block, drop work, or reject the item immediately; choosing among those policies is part of the design, not an implementation detail. A correct implementation uses a library bounded queue or waits on conditions in a while loop, and it defines shutdown and poison-item handling explicitly.
Was this answer helpful?
Likely follow-ups
- Why must a wait on a condition variable be inside a while loop rather than an if?
- Your consumers are stuck and the buffer is full. How would you tell that apart from a slow producer?
- How do you shut this down cleanly without losing items already accepted?
- One item throws every time it is processed. What happens to the pipeline?
Related questions
- One global lock makes your reservation system correct and slow. How do you split it without deadlocking?hardAlso on concurrency5 min
- How would you design a thread-safe component, and why is adding synchronized to every method not a design?hardAlso on concurrency7 min
- Build an in-memory pub-sub with topics, many subscribers per topic and at-least-once delivery. How does a message get redelivered?hardAlso on concurrency6 min
- Design an in-memory job scheduler that runs one-off, delayed and recurring jobs. Write the core classes.hardAlso on concurrency6 min