Guide
Premium
Intermediate
Real Projects

PostgreSQL Read Replicas and Replication Lag: Read Routing, Read-Your-Writes and WAIT FOR

A practical guide to serving reads from replicas without handing anyone the past: how a change actually travels from primary to replica and what each of the four clocks in pg_stat_replication measures (sent, write, flush and replay), why the seconds-based lag metric almost everyone ships first lies on an idle primary and the query that fixes it, Prometheus alerts built on your own postgres_exporter queries instead of metric names that change between versions, the five real causes of lag —single-process replay, replicas with fewer IOPS than the primary, recovery conflicts and the max_standby_streaming_delay that cancels your queries after 30 seconds, huge transactions, and a forgotten recovery_min_apply_delay—, the five levels of synchronous_commit and what each one costs, quorum commit with ANY and FIRST plus the two traps that cause real incidents (remote_apply with ANY 1 does not make every replica current, and an unsatisfiable quorum stops COMMITs with no timeout), the hot_standby_feedback trade-off between query cancellations on the replica and bloat on the primary, and read routing in the application —never by SQL text— with two pools, background health probing and lag-based eviction. It covers all four read-your-writes strategies: primary pinning with a TTL, LSN tokens with the new WAIT FOR command in PostgreSQL 19 and its four modes, the client-side polling fallback for PostgreSQL 18 and earlier (including the exact reason pg_wal_replay_wait was reverted before release), and lag-aware routing per read class. Includes the format validation that prevents SQL injection in a command that takes no bind parameters, the token max() without which concurrent requests move it backwards, why comparing LSNs as strings gives wrong answers, slots that fill pg_wal with max_slot_wal_keep_size and PostgreSQL 18 idle_replication_slot_timeout, what breaks on promotion and PostgreSQL 17 failover slot synchronisation, a complete write/read endpoint pair, eight recurring mistakes, a twelve-point production checklist and an FAQ. With production-ready SQL, Python, TypeScript, YAML and configuration.

32 minutes read
Josue Garcia
2 views

Verificando acceso...

Loading comments...

Related Resources

Guía
PREMIUM

asyncio in Production: Never Block the Event Loop — TaskGroups, Cancellation and Bounded Concurrency

A practical asyncio guide for Python services in production: why blocking the event loop degrades the whole process without raising a single exception, how to catch it by measuring loop lag and with Python 3.14 introspection, structured concurrency with TaskGroup and handling ExceptionGroup via except*, the task the garbage collector makes vanish, timeouts with a deadline budget propagated across services, correct cancellation with cleanup and shield, bounded concurrency with semaphores and backpressured queues, synchronization primitives, and what changes with eager tasks, python -m asyncio pstree and free-threading. With production-ready code and a deployment checklist.

Guía
PREMIUM

Cache-Aside in Production: TTLs, Invalidation, and How to Prevent Cache Stampedes

The complete guide to the cache-aside pattern with Redis: jittered TTLs, correct invalidation, and the three defenses against cache stampedes (distributed lock, single-flight, and XFetch). Expanded with stale-while-revalidate, fail-open and circuit breakers, two-tier caching with RESP3 invalidation, delayed double delete and CDC, hot keys, eviction and memory management, observability with Prometheus, testing, choosing an engine (Redis, Valkey, Memcached), and a complete TypeScript implementation. With production-ready code in Python and TypeScript.

Guía
PREMIUM

Circuit Breakers: How to Prevent Cascading Failures in Distributed Systems

Learn to implement the circuit breaker pattern so a failing dependency never drags down your whole system: the three states (closed, open, half-open), sliding failure windows, limited probes to avoid thundering herds, robust fallbacks, and how to combine it with timeouts, retries, and bulkheads. Includes distributed state in Redis, observability with Prometheus, pytest testing, circuit breaking in Envoy/Istio, a full case study, and production-ready code in Python and TypeScript.