Skip to main content

Python Code Vault

Hand-picked production patterns. Each entry pairs the canonical Python implementation with WHY it exists and WHEN to reach for it. Free, copy-ready, no signup.

Want a senior-review reflex too? Paste any Python at ✨ Refactor Coach, or replay a production incident at 🔥 Postmortem Replay.

18 of 18 patterns

Distributed systems

Token bucket rate limiter

N tokens refill at rate R; each request costs 1. Allows controlled bursts and steady-state throughput.

Idempotency-Key cache

Same key → same result; retry-safe across network flakes. Stripe / PayPal / Square use this verbatim.

Circuit breaker

Open after N failures, short-circuit to fallback while OPEN, probe-once via HALF_OPEN to recover.

Retry with exponential backoff + jitter

Wait 1s, 2s, 4s, 8s… with jitter; cap attempts and total wait. Survives transient flakes without thundering herd.

Cache-aside (lazy cache)

Read cache → miss → read DB → populate cache. The 90% case for application caching.

Concurrency & async

asyncio.gather with bounded concurrency

Run N coroutines but never more than K at once via Semaphore. Prevents flooding downstream.

Producer-consumer with queue.Queue

Producers push, workers pop, sentinel signals shutdown. Thread-safe, FIFO, no manual locking.

Timeout any awaitable

Bound a coroutine's runtime with asyncio.wait_for — never await something that has no time ceiling.

Data structures

Top-K with heapq

Maintain the k largest items in O(n log k) using a min-heap. Beats sort-then-slice on large streams.

Counter for frequency tables

Counter + .most_common(n) replaces 10 lines of dict-counting. Sorted by count, ties broken by insertion order.

Cursor pagination (not OFFSET)

Keyset / cursor pagination — the next page is built from the last row's sort key, not row N.

Production patterns

Structured logging

Emit JSON, not free-text. Datadog / Splunk / Grafana parse the fields directly — searchable, filterable.

Feature flag with overrides + bucket rollout

Override map for VIPs / kill-switches, deterministic hash-bucket for percentage rollouts. Sub-millisecond eval.

Graceful SIGTERM shutdown

Catch SIGTERM, stop accepting work, drain in-flight tasks before exit — survives k8s rolling deploys.

Security & privacy

Row-level multi-tenancy + leak audit

Always-applied tenant filter + audit_leaks() that pages SRE if a query forgets the WHERE clause.

PII scrubber (email / phone / card)

Three regex passes catch the obvious shapes before user-supplied text leaves your process boundary.

Performance

__slots__ for memory-dense classes

Skip per-instance __dict__; 40-50% memory cut for many-instance classes. Zero API change.

functools.lru_cache for memoization

Decorator turns any pure function into a memoised one. Hit rate visible via .cache_info().