Concurrency
concurrency
Code Snippets
Async Queue with Concurrency Limit
When you have hundreds of API calls but the upstream caps you at 5 in flight, naive `Promise.all` is a 429 storm waiting to happen. A concurrency-limited queue runs at most `n` tasks at once, draining a backlog as workers free up. This snippet starts with the minimal worker pool, adds per-task error isolation, then layers in cancellation and ordered results so the helper holds up in production.
Goroutine + Channel Fan-Out
Fan-out is the canonical Go concurrency pattern: a producer pushes jobs onto a channel, a fixed pool of worker goroutines pulls and processes them in parallel, and results flow back through a results channel. This snippet shows a minimal worker pool, the close + range idiom that signals completion, and a `sync.WaitGroup` variant for fire-and-forget. Use this whenever you need parallelism with a bounded number of workers.
Context-Driven Cancellation
`context.Context` is Go's standard mechanism for cancelling long-running operations: a deadline, a parent-child cancellation tree, and a request-scoped value bag. This snippet shows `context.WithTimeout` to bound a function's runtime, the `select { case <-ctx.Done(): ... }` pattern in workers, and how to attach a request-id value. Pass `ctx` as the first argument to every function that does I/O or has a chance of blocking.
Question Banks
Producer / Consumer and Locks
Mid-tier drills on bounded buffers, mutexes, semaphores, and deadlock detection. Code stems in Python and JavaScript; one trace involves the four conditions for deadlock.
Concurrency Edge Cases
Hard drills on race conditions, the ABA problem, lost notifications, and fairness. Mixed JS / Python / Java code stems with subtle bugs to spot and fix.
Community
Transaction Isolation Levels with Failing Examples
Read uncommitted, read committed, repeatable read, serializable. Each level explained with a runnable two-session SQL example showing exactly which anomaly it allows or prevents.
PostgreSQL MVCC and Isolation Level Deep Dive
A 5-question reference set on PostgreSQL's MVCC implementation: tuple versioning, READ COMMITTED vs REPEATABLE READ vs SERIALIZABLE, row-level locks, and the autovacuum machinery that keeps txid wraparound at bay.
asyncio Explained Without the Jargon
asyncio is a cooperative scheduler for I/O-bound code. async def marks a pause-able function, await is the pause point, and the event loop runs whichever coroutine is ready.
The GIL: What It Actually Blocks
The GIL blocks parallel Python bytecode and nothing else. I/O, well-behaved C extensions, and multiprocessing all sidestep it. Here is how I tell GIL-bound code from code that just feels slow.
Promises, await, and the Microtask Queue
What a microtask actually is, why Promise.resolve().then never runs synchronously, how await schedules continuations as microtasks, and the four-column trace I run when ordering surprises me.
The JavaScript Event Loop: Tasks, Microtasks, and Rendering
A trace-by-hand walkthrough of the event loop: pick a macrotask, drain microtasks to empty, then optionally render. Worked example interleaves setTimeout, Promise, queueMicrotask, await, and requestAnimationFrame.
Distributed Locks and Leases Deep Dive
A 5-question reference set on distributed mutual exclusion: Redis SETNX with fencing tokens, etcd leases, Raft leader election, optimistic vs pessimistic locking tradeoffs, and cross-region home-region pinning.
