Token bucket rate limiter
N tokens refill at rate R; each request costs 1. Allows controlled bursts and steady-state throughput.
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
N tokens refill at rate R; each request costs 1. Allows controlled bursts and steady-state throughput.
Same key → same result; retry-safe across network flakes. Stripe / PayPal / Square use this verbatim.
Open after N failures, short-circuit to fallback while OPEN, probe-once via HALF_OPEN to recover.
Wait 1s, 2s, 4s, 8s… with jitter; cap attempts and total wait. Survives transient flakes without thundering herd.
Read cache → miss → read DB → populate cache. The 90% case for application caching.
Run N coroutines but never more than K at once via Semaphore. Prevents flooding downstream.
Producers push, workers pop, sentinel signals shutdown. Thread-safe, FIFO, no manual locking.
Bound a coroutine's runtime with asyncio.wait_for — never await something that has no time ceiling.
Maintain the k largest items in O(n log k) using a min-heap. Beats sort-then-slice on large streams.
Counter + .most_common(n) replaces 10 lines of dict-counting. Sorted by count, ties broken by insertion order.
Keyset / cursor pagination — the next page is built from the last row's sort key, not row N.
Emit JSON, not free-text. Datadog / Splunk / Grafana parse the fields directly — searchable, filterable.
Override map for VIPs / kill-switches, deterministic hash-bucket for percentage rollouts. Sub-millisecond eval.
Catch SIGTERM, stop accepting work, drain in-flight tasks before exit — survives k8s rolling deploys.
Always-applied tenant filter + audit_leaks() that pages SRE if a query forgets the WHERE clause.
Three regex passes catch the obvious shapes before user-supplied text leaves your process boundary.
Skip per-instance __dict__; 40-50% memory cut for many-instance classes. Zero API change.
Decorator turns any pure function into a memoised one. Hit rate visible via .cache_info().