Cheat Sheet
A single-page reference for all 46 patterns. Print it, bookmark it, or ctrl-F your way through it.
Pick by Problem
Not sure which pattern you need? Start here.
| I need to... | Reach for | Why |
|---|---|---|
| Limit concurrent access | Semaphore | Counter-based, proven in OS kernels |
| Handle slow consumers | Backpressure | Don't drop — push back |
| Cache with eviction | LRU Cache | O(1) get/put, auto-evict coldest |
| Fast prefix lookup | Trie | O(k) by key length, not collection size |
| Probably-in-set check | Bloom Filter | Zero false negatives, tiny memory |
| Sorted range queries | B+ Tree or Skip List | B+ Tree for disk, Skip List for memory |
| Crash-safe writes | WAL + Checkpointing | Log-then-apply + periodic snapshots |
| Stop cascading failure | Circuit Breaker | Fail fast, recover gradually |
| Retry failed calls | Retry with Backoff | Exponential delay + jitter |
| Control throughput | Rate Limiter | Token bucket, constant refill |
| Verify data integrity | Merkle Tree | O(log n) proof with hash chain |
| Reduce memory via sharing | Flyweight or Interning | Share immutable values |
| Avoid GC pressure | Object Pool or Arena | Reuse or bulk-free |
| Detect changes cheaply | Dirty Flag | Skip recomputation if clean |
| Order distributed events | Logical Clock | Lamport or vector clocks |
| Lazy evaluation | Iterator | Pull-based, zero intermediate alloc |
| Handle multiple types | Tagged Union or Vtable | Tag for closed set, vtable for open set |
| Write-heavy workload | LSM Tree | Buffer → flush → merge |
| Compose middleware | Middleware Chain | Onion model, each handler wraps next |
| Balance work across threads | Work Stealing | Idle steals from busy |
| Track multiple flags | Bitmask | N flags in one integer |
| Schedule by priority | Min Heap | O(1) peek, O(log n) push/pop |
| Fixed-size FIFO | Ring Buffer | Wraps around, zero alloc |
| Minimal diff between states | Diff / Patch | Compute + apply changes |
| Decouple producers/consumers | Observer | Subscribe model |
| Distribute keys across nodes | Consistent Hashing | Add/remove node remaps ~1/n |
| Build order from dependencies | Dependency Graph | DAG + topological sort |
| Atomic state transitions | State Machine | Explicit states, impossible transitions unrepresentable |
| Soft-delete with later cleanup | Tombstone | Mark deleted, compact later |
| Copy-on-mutation sharing | Copy-on-Write | Share until someone writes |
| Deterministic cleanup | Reference Counting | Free at rc=0, no GC pause |
| Register/discover services | Registry | Name → handler map |
| Atomic swap of state | Double Buffering | Write to back, swap to front |
| Non-blocking reads | MVCC | Versioned snapshots |
| Responsive main thread | Cooperative Scheduling | Yield between chunks |
| Single-thread I/O | Event Loop | Multiplex without threads |
| Accumulate then flush | Batch Processing | Amortize per-op overhead |
| Actor-style isolation | Actor Model | Private state + message passing |
| Tree traversal dispatch | Visitor | Type-specific callbacks |
| O(1) alloc from freed slots | Free List | Linked list of free blocks |
| Merge sorted streams | Merge Iterator | K-way merge via min-heap |
Complexity Reference
Data Structures
| Pattern | Insert | Lookup | Delete | Space | Key Tradeoff |
|---|---|---|---|---|---|
| Bitmask | O(1) | O(1) | O(1) | O(1) | Limited to word-size flags |
| Min Heap | O(log n) | O(1) peek | O(log n) | O(n) | Only peek-min is fast |
| Ring Buffer | O(1) | O(1) | O(1) | O(n) fixed | Fixed capacity |
| Trie | O(k) | O(k) | O(k) | O(SIGMA * n) | Memory-hungry for sparse keys |
| Skip List | O(log n) avg | O(log n) avg | O(log n) avg | O(n) avg | Probabilistic, simpler than trees |
| Bloom Filter | O(k) | O(k) | N/A | O(m) bits | False positives possible |
| LRU Cache | O(1) | O(1) | O(1) | O(n) | Evicts on capacity |
| B+ Tree | O(log n) | O(log n) | O(log n) | O(n) | Disk-optimized, high fan-out |
| Tagged Union | N/A | O(1) dispatch | N/A | O(max variant) | Closed set of types |
| Merkle Tree | O(log n) | O(log n) | O(log n) | O(n) | Verification, not search |
| Merge Iterator | N/A | O(log k) next | N/A | O(k) | k = number of streams |
System Patterns
| Pattern | Throughput | Latency | Failure Mode |
|---|---|---|---|
| Circuit Breaker | Normal when closed | +0 closed, fail-fast open | Blocks all calls when open |
| Rate Limiter | Capped at token rate | +0 if tokens available | Rejects excess (429) |
| Retry with Backoff | Reduced during retries | Exponential increase | Amplifies if no jitter |
| WAL | Sequential write speed | +1 write (log first) | Safe — replay from log |
| Batch Processing | Higher (amortized) | Higher (waits for batch) | Loses batch on crash |
| Consistent Hashing | Same as underlying | +hash computation | ~1/n keys remap on node change |
Memory Patterns
| Pattern | Alloc | Free | Overhead | Best For |
|---|---|---|---|---|
| Object Pool | O(1) amortized | O(1) return | Pool size | High-churn same-type objects |
| Arena Allocator | O(1) bump | O(1) bulk free | Alignment waste | Phase-based lifetimes |
| Free List | O(1) | O(1) | Per-slot next pointer | Fixed-size blocks |
| Flyweight | O(1) lookup | Shared, not freed | Lookup table | Many identical small objects |
| Copy-on-Write | O(1) share | O(n) on first write | Ref count per page | Read-heavy shared data |
| Reference Counting | O(1) clone | O(1) at rc=0 | Per-object counter | Deterministic cleanup |
| Interning | O(k) first, O(1) after | Pooled | Hash table | String/symbol deduplication |
Pattern Combos
Patterns rarely appear alone. These are the most common production combos:
| Combo | Used In | Why Together |
|---|---|---|
| WAL + Checkpointing | PostgreSQL, etcd | WAL for safety, checkpoint to bound replay |
| Bloom Filter + LSM Tree | LevelDB, RocksDB | Skip unnecessary disk reads |
| Min Heap + Merge Iterator | LevelDB compaction | Efficiently merge K sorted runs |
| Circuit Breaker + Retry | gRPC, Hystrix | Retry transient failures, break on persistent |
| Rate Limiter + Backpressure | API gateways | Limit ingress, signal overload |
| Ring Buffer + Event Loop | libuv, io_uring | Fixed-size queue for I/O events |
| Object Pool + Free List | Go runtime | Pool manages slabs, free list tracks slots |
| MVCC + B+ Tree | PostgreSQL | Versioned rows in disk-optimized index |
| Dirty Flag + Double Buffering | React Fiber | Mark dirty, batch into next frame |
| Bitmask + State Machine | React reconciler | Flags encode state, transitions via bitwise ops |
| Consistent Hashing + Registry | Service mesh | Hash to locate, registry to discover |
| Trie + Interning | Compilers | Intern strings, look up by prefix |
Decision Trees
"Which cache?"
Need eviction?
- YesNeed O(1) ops?
- NoNeed probabilistic filter?
- YesBloom FilterNot a cache, but saves cache misses
- NoHash mapA hash map is fine
"Which memory strategy?"
All objects same size?
- YesObject Pool / Free List
- NoPhase-based lifetime?
- YesArena Allocator
- NoShared immutable?
"Which concurrency model?"
Shared state?
- NoActor ModelMessage passing
- YesRead-heavy?
- YesMVCCReaders never block
- NoNeed limit on concurrency?
- YesSemaphore
- NoNeed to split work?