Goal: concurrent code that is correct first and fast second.
Use for:
- parallelizing work or handling many requests
- reviewing shared state, locking, and async flow
- diagnosing races, deadlocks, or corruption
Workflow:
- Minimize shared mutable state; prefer message passing or immutability.
- Define clear ownership of each mutable resource.
- Protect shared state with the smallest necessary lock.
- Acquire locks in a consistent order to avoid deadlock.
- Set timeouts and cancellation on blocking operations.
- Verify with stress tests and a race detector.
Patterns:
- immutable data shared freely without locks
- queues/channels to hand off work
- bounded concurrency to limit resource use
- idempotent operations for safe retries
Rules:
- share by communicating, or protect shared state explicitly
- keep critical sections tiny
- a consistent lock order prevents deadlocks
- never assume ordering; test under contention