Golang Experiments: Classic Concurrency Problems

    Welcome to Golang Experiments! This series explores classic concurrency problems through the lens of Go’s powerful concurrency primitives. Each problem is a timeless synchronization challenge that teaches fundamental concepts you’ll use in production systems every day. What you’ll learn: Go’s concurrency features (goroutines, channels, sync primitives) How to recognize and solve common synchronization problems Patterns that appear in real-world distributed systems When to use different synchronization strategies Why Study Classic Problems? These aren’t just academic exercises! Each problem represents a pattern you’ll encounter in production: ...

    October 20, 2025 · 11 min · Rafiul Alam

    Cigarette Smokers: Resource Matching Problem

    The Cigarette Smokers Problem The Cigarette Smokers problem is a unique synchronization challenge that demonstrates resource matching and conditional waiting. Proposed by Suhas Patil in 1971, it’s known for not being solvable with only semaphores! The Scenario There are: 3 smokers 1 agent Each smoker needs 3 ingredients to smoke: Tobacco Paper Matches The setup: Smoker 1 has infinite tobacco Smoker 2 has infinite paper Smoker 3 has infinite matches The agent has infinite of ALL three The rules: ...

    October 16, 2025 · 8 min · Rafiul Alam

    The Roller Coaster: Multi-Phase Synchronization

    The Roller Coaster Problem The Roller Coaster Problem, introduced by Allen Downey in “The Little Book of Semaphores,” demonstrates multi-phase synchronization and cyclic barriers. It’s a perfect model for batch processing systems where work happens in coordinated phases. The Scenario A roller coaster ride has: A car with capacity C passengers Passengers continuously arriving and queuing The car cycles through: board → ride → unboard → repeat The rules: Car waits until exactly C passengers are ready Passengers can’t board until car is empty Car can’t run until full Passengers must unboard before new passengers board Process repeats indefinitely The Challenge: Three-Phase Coordination Each ride has three synchronized phases: ...

    October 7, 2025 · 10 min · Rafiul Alam

    The Search-Insert-Delete Problem: Three-Way Synchronization

    The Search-Insert-Delete Problem The Search-Insert-Delete Problem extends the classic Readers-Writers problem with three different access patterns instead of two. It demonstrates how to coordinate multiple operation types with different compatibility requirements - a common challenge in database systems and data structures. The Scenario Three types of goroutines access a shared list: Searchers - Read the list without modifying it Inserters - Add new elements to the list Deleters - Remove elements from the list Compatibility rules: ...

    October 5, 2025 · 10 min · Rafiul Alam

    Readers-Writers: Writers Preference

    The Writer Starvation Problem In the readers preference solution, we saw how continuous readers can starve writers. The writers preference solution fixes this by giving writers priority. Key idea: When a writer is waiting, no new readers are allowed to start, even if other readers are currently reading. Real-World Need for Writer Priority Some systems need writer priority: Databases with critical updates (billing, inventory) Real-time systems (sensor updates must not be delayed) Logging systems (log writes can’t be delayed) Configuration systems (updates must propagate quickly) Leader election (state changes need priority) The Solution Go’s sync.RWMutex uses readers preference, so we need to implement writer preference manually using additional synchronization: ...

    September 22, 2025 · 7 min · Rafiul Alam

    Producer-Consumer: The Bounded Buffer

    From Unbounded to Bounded In the previous article, we explored the unbounded buffer pattern where the queue could grow infinitely. This works until you run out of memory! The bounded buffer adds a crucial constraint: maximum queue size. This introduces backpressure - when the buffer is full, producers must wait for consumers to catch up. Why Bounded Buffers Matter Bounded buffers appear everywhere in production systems: TCP sliding windows (flow control) HTTP/2 stream flow control (prevents overwhelm) Message queue limits (RabbitMQ, Kafka partition limits) Thread pool queues (bounded task queues) Rate limiters (token buckets with finite capacity) Circuit breakers (limit concurrent requests) The key benefit: Bounded buffers provide natural backpressure and prevent resource exhaustion. ...

    September 19, 2025 · 8 min · Rafiul Alam

    Sleeping Barber: The Waiting Room Problem

    The Sleeping Barber Problem The Sleeping Barber is a classic synchronization problem proposed by Edsger Dijkstra in 1965. It elegantly demonstrates resource management, waiting, and signaling in concurrent systems. The Scenario A barbershop has: 1 barber 1 barber chair N waiting room chairs The rules: If no customers, the barber sleeps When a customer arrives: If barber is sleeping → wake him up If barber is busy → sit in waiting room (if space available) If waiting room is full → leave When barber finishes a customer → check waiting room Real-World Applications This pattern appears in many systems: ...

    September 10, 2025 · 7 min · Rafiul Alam

    Dining Philosophers: Five Forks and a Deadlock

    The Classic Dining Philosophers Problem The Dining Philosophers problem is one of the most famous concurrency problems, introduced by Edsger Dijkstra in 1965. It beautifully illustrates the challenges of resource sharing and deadlock in concurrent systems. The Setup: Five philosophers sit around a circular table. Between each pair of philosophers is a single fork (5 forks total). Each philosopher needs two forks to eat - one from their left and one from their right. ...

    August 24, 2025 · 6 min · Rafiul Alam

    Readers-Writers: Fair Solution

    The Fairness Problem We’ve seen two extremes: Readers preference: Writers can starve Writers preference: Readers can starve The fair solution ensures no starvation - everyone gets served in the order they arrive. The Solution: FIFO Ordering Key idea: Use a queue to serve requests in arrival order. This prevents both reader and writer starvation. Arrival Order: R1, R2, W1, R3, R4, W2 Execution: R1+R2 → W1 → R3+R4 → W2 (batch) (excl) (batch) (excl) Consecutive readers can still batch together, but writers don’t get skipped! ...

    August 16, 2025 · 7 min · Rafiul Alam

    Readers-Writers: Readers Preference

    The Readers-Writers Problem The Readers-Writers problem is fundamental in concurrent systems where data is shared between multiple threads: Readers: Can access data simultaneously (read-only, no conflicts) Writers: Need exclusive access (modifications can’t overlap) The challenge: Maximize concurrency while maintaining data integrity. Real-World Applications This pattern is everywhere: Databases: SELECT queries (readers) vs UPDATE/INSERT (writers) Caching: Cache reads vs cache updates Configuration: Reading config vs reloading config File systems: Multiple readers, exclusive writes Web servers: Read sessions vs update sessions Readers Preference Solution In this variant, readers get priority: ...

    August 13, 2025 · 7 min · Rafiul Alam