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

    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

    Go Concurrency Pattern: The Bank Account Drama

    ← Login Counter | Series Overview | Ticket Seller → The Problem: Money Vanishing Into Thin Air Two people share a bank account with $100. Both check the balance at the same time, see $100, and both withdraw $100. The bank just lost $100. This isn’t a hypothetical-race conditions in financial systems have caused real monetary losses. The bank account drama illustrates the fundamental challenge of concurrent programming: read-modify-write operations are not atomic. What seems like a simple operation actually involves multiple steps, and when multiple goroutines execute these steps concurrently, chaos ensues. ...

    January 18, 2025 · 7 min · Rafiul Alam

    Go Memory Model Explained

    Go Concurrency Patterns Series: ← Context Propagation | Series Overview | Graceful Shutdown → What is the Go Memory Model? The Go Memory Model specifies the conditions under which reads of a variable in one goroutine can be guaranteed to observe values produced by writes to the same variable in a different goroutine. Understanding this model is crucial for writing correct concurrent code without data races. Core Concepts: Happens-Before: Ordering guarantees between memory operations Memory Visibility: When writes in one goroutine are visible to reads in another Synchronization: Mechanisms that establish happens-before relationships Data Races: Concurrent memory accesses without proper synchronization Real-World Impact Correctness: Prevent subtle bugs in concurrent code Performance: Understand when synchronization is necessary Debugging: Diagnose race conditions and memory visibility issues Optimization: Make informed decisions about lock-free algorithms Code Review: Identify potential concurrency bugs The Happens-Before Relationship Definition A happens-before relationship guarantees that one event occurs before another in program order, and that effects of the first event are visible to the second. ...

    June 25, 2024 · 12 min · Rafiul Alam