---
title: "When failover isn’t safe: Building high-availability PostgreSQL on Kubernetes"
description: "A staging gameday simulating zonal network latency left Datadog's Kubernetes-hosted PostgreSQL clusters stuck: with asynchronous replication, every standby exceeded Patroni's maximum_lag_on_failover, so no failover candidate could be promoted safely."
url: "https://swe.quest/articles/when-failover-isn-t-safe-building-high-availability-postgresql-on-kubernetes-ee0014d4b5265bb2"
canonical: "https://www.datadoghq.com/blog/engineering/postgresql-ha-kubernetes/"
source: "Datadog Engineering"
published: "2026-06-04"
difficulty: "medium"
kind: "Case study"
reading_minutes: 22
tags: ["postgres","replication","kubernetes","resilience","chaos-engineering"]
---

# When failover isn’t safe: Building high-availability PostgreSQL on Kubernetes

A post on [Datadog Engineering](https://swe.quest/sources/datadog.md), published 2026-06-04, 22 min read, Case study. Original: [https://www.datadoghq.com/blog/engineering/postgresql-ha-kubernetes/](https://www.datadoghq.com/blog/engineering/postgresql-ha-kubernetes/)

## Summary

A staging gameday simulating zonal network latency left Datadog's Kubernetes-hosted PostgreSQL clusters stuck: with asynchronous replication, every standby exceeded Patroni's maximum\_lag\_on\_failover, so no failover candidate could be promoted safely. The fix is a hybrid model in which leader-pool standbys replicate synchronously while read replicas stay asynchronous. Patroni is configured with synchronous\_mode, synchronous\_node\_count=1 and synchronous\_mode\_strict, and PostgreSQL with synchronous\_commit=remote\_apply, so a failover candidate is always current. pgbench TPC-B benchmarks across synchronous\_commit levels quantify the latency and throughput cost of each durability option.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Replication](https://swe.quest/tags/replication.md) (Distributed systems)
- [Kubernetes](https://swe.quest/tags/kubernetes.md) (Infrastructure)
- [Resilience](https://swe.quest/tags/resilience.md) (Distributed systems)
- [Chaos engineering](https://swe.quest/tags/chaos-engineering.md) (Reliability & operations)

## Prerequisites

- PostgreSQL streaming replication ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- Kubernetes basics ([easy posts on Kubernetes](https://swe.quest/tags/kubernetes.md?difficulty=easy))
- leader election with ZooKeeper

## Keywords

PostgreSQL, Patroni, ZooKeeper, pgbench, synchronous\_commit, TPC-B

## Related posts

- [Designing a planned switchover for minimum disruption](https://swe.quest/articles/designing-a-planned-switchover-for-minimum-disruption-c09f9387b5a40465.md): PlanetScale · 2026-10-29 · Hard · 8 min read · PostgreSQL, Replication, Resilience, Deployment
  Neki performs planned Postgres primary switchovers without stopping the old primary.
- [Production Postgres on Railway: From First Deploy to High Availability](https://swe.quest/articles/production-postgres-on-railway-from-first-deploy-to-high-availability-70a3bcba84354fc9.md): Railway · 2026-10-15 · Medium · 13 min read · PostgreSQL, Replication, Resilience, Cloud platforms
  Railway Postgres can grow from a single private-networked database to a production setup with volume backups, point-in-time recovery, PgBouncer connection pooling and Patroni-based high availability, demonstrated with an orders API that uses idempotency keys so retries stay safe.
- [PostgresBench: Measuring the impact of High Availability on Managed Postgres performance](https://swe.quest/articles/postgresbench-measuring-the-impact-of-high-availability-on-managed-postgres-f5f54e9ac5894971.md): ClickHouse · 2026-07-21 · Medium · 7 min read · PostgreSQL, Replication, Performance engineering, Cloud platforms
  PostgresBench now measures the performance cost of high-availability configurations across managed Postgres services, comparing shared-nothing designs (ClickHouse Managed Postgres, RDS, Crunchy Bridge) that use streaming standbys with shared-storage forks (Aurora, Neon) that replicate WAL to a storage layer.
- [Designing a reliable unplanned failover](https://swe.quest/articles/designing-a-reliable-unplanned-failover-ab0dd731f569d08c.md): PlanetScale · 2026-11-05 · Hard · 10 min read · PostgreSQL, Replication, Resilience, Distributed systems, Distributed databases
  Neki, PlanetScale's Postgres sharding system, declares a primary dead only when its control plane (Admin) cannot reach it and at least one reachable replica reports broken replication with none contradicting.
- [Managed Postgres on Railway](https://swe.quest/articles/managed-postgres-on-railway-38376e6923686bbc.md): Railway · 2026-11-05 · Medium · 9 min read · PostgreSQL, Replication, Cloud platforms, AI agents
  Railway's managed Postgres now covers the full database lifecycle: private-by-default services with reference-variable credentials, usage-billed vertical scaling, live volume resizing, and one-click PgBouncer pooling with a separate unpooled URL for migrations.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
