Redis Pub/Sub subscriber with reconnect-friendly loop

Pub/Sub consumers should assume connections will drop: Redis restarts, network blips, or idle timeouts happen. I keep the subscription loop simple: subscribe, range over the channel, and exit cleanly when ctx.Done() fires. If the subscription ends une

sqlc transaction wrapper that keeps call sites clean

When using sqlc, the generated query set usually has a WithTx method. I wrap that pattern so business logic can depend on an interface and still run inside a transaction. The key is to keep transaction boundaries explicit while avoiding passing *sql.T

Store unstructured JSON safely with json.RawMessage

Not every integration payload maps cleanly to a static struct. When I need to accept “mostly known” JSON but preserve unknown fields for auditing or forward compatibility, I use json.RawMessage. That allows me to decode the envelope (event name, versi

Enforce JSON Content-Type and method early in handlers

A lot of handler complexity disappears if you reject bad requests early. I enforce the HTTP method (POST, PUT, etc.) and require Content-Type: application/json before attempting to decode. This prevents confusing errors where clients send form-encoded

CIDR allowlist middleware using netip (trust boundary explicit)

For internal admin endpoints, I often add a network allowlist in addition to auth. The tricky part is deciding which IP to trust: if you’re behind a proxy, you might need X-Forwarded-For, but only if the proxy is controlled by you. The middleware belo

Inbound rate limiting middleware with token bucket

If you expose an API to the public internet, you need a basic guardrail against bursts. I like a token bucket with rate.NewLimiter because it’s easy to reason about: steady-state rate plus a burst capacity. The middleware checks Allow() and returns 42

Context-aware cache refresh with atomic.Pointer (Go 1.19+)

I often need a fast read path for small datasets (like a list of active plans or an allowlist) that updates periodically. Instead of locking on every read, I store a pointer to an immutable snapshot in atomic.Pointer. Reads are lock-free and safe; ref

Sampling logs to reduce noise during high-QPS incidents

Structured logs are great, but at high QPS they can become their own outage: too much IO, too much storage, and noisy dashboards. zap includes a sampler that allows you to keep early logs and then sample at a fixed rate. I like this for “successful re

Lazy init of expensive clients with sync.Once

Some dependencies are expensive to initialize (TLS-heavy clients, large config loads, SDKs that fetch metadata). I use sync.Once to ensure initialization happens exactly once, even under concurrent access. The important detail is capturing the initial

HTTPtrace to debug DNS/connect/TLS timing in production-like runs

When an outbound call is slow, it’s not always the server—it can be DNS, connection reuse, or TLS handshakes. net/http/httptrace lets you instrument a single request and see exactly where time is going. I attach a trace to the request context and reco

Run database migrations on startup (with a guard)

I generally prefer running migrations in a separate release step, but for smaller deployments it’s sometimes pragmatic to run them at startup. The failure mode to avoid is “every instance runs migrations at once.” The pattern here uses Postgres adviso

db/sql prepared statements with context and explicit Close

Prepared statements are useful when the same query runs in hot loops, but they come with lifecycle responsibilities. I prepare once (usually at startup), store the *sql.Stmt, and always close it on shutdown. The important detail is still using QueryRo