Design Producer-Consumer problem from scratch
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-07-02
Problem Statement
Design and implement a thread-safe Producer-Consumer system from scratch that allows multiple producers to generate data and multiple consumers to process it concurrently, using a shared bounded buffer. Focus on synchronization, concurrency control, and efficiency.
Functional Requirements (FR)
Producers
•Multiple producer threads can produce items and place them into a shared buffer.
•If the buffer is full, producers should wait until space becomes available.
Consumers
•Multiple consumer threads can consume items from the shared buffer.
•If the buffer is empty, consumers should wait until items are produced.
Buffer Management
•Shared buffer should have a fixed capacity.
•Items should be consumed in the order they were produced (FIFO).
Synchronization
•Ensure thread-safe access to the shared buffer.
•Prevent race conditions, deadlocks, and starvation.
Non-Functional Requirements (NFR)
•Concurrency Safety → Correct handling of multiple producers and consumers.
•Efficiency → Minimize thread waiting and maximize throughput.
•Scalability → Support arbitrary number of producer and consumer threads.
•Extensibility → Easy to extend for priority items or multiple buffer types.
Core Entities & Attributes
Buffer / SharedQueue
•capacity → Maximum number of items.
•queue[] → FIFO data structure to store items.
•size → Current number of items.
Producer
•producerId, produceItem() → Method to produce data and add to buffer.
Consumer
•consumerId, consumeItem() → Method to remove and process data from buffer.
Synchronization Mechanism
•Locks / Monitors → Ensure mutual exclusion on buffer access.
•Condition variables / Semaphores → Signal waiting producers/consumers.
Core Components
BoundedBuffer Class
•Implements the shared buffer.
•Provides put(item) for producers and take() for consumers.
•Handles waiting and notification for full/empty conditions.
Producer Class
•Generates data.
•Calls put(item) on the buffer.
Consumer Class
•Processes data.
•Calls take() from the buffer.
Synchronization Module
•Ensures mutual exclusion and manages signaling between threads.
•Implements classic wait/notify or semaphore-based mechanism.
APIs / Operations
BoundedBuffer APIs
•put(item: T): void → Add item to buffer, block if full.
•take(): T → Remove item from buffer, block if empty.
•isFull(): boolean → Checks if buffer is full.
•isEmpty(): boolean → Checks if buffer is empty.
Producer / Consumer APIs
•produce(): void → Generate item and put into buffer.
•consume(): T → Retrieve item from buffer and process.
LLD Design Goals
•Class Diagram → BoundedBuffer, Producer, Consumer, Item.
Design Patterns:
•Observer / Wait-Notify → Producers/Consumers wait for buffer state changes.
•Singleton (Optional) → Single shared buffer instance.
•Concurrency Handling →1. Use mutex/lock for exclusive buffer access.2. Use condition variables or semaphores to block/wake threads.
•Trade-offs →1. Blocking threads vs. busy-waiting for performance.2. Fixed buffer size vs. dynamic resizing.