We have moved several production PostgreSQL databases between two data centers, in different countries, without a maintenance window. Some operations are now up to 35 times faster, and we didn't touch a single query.
24 ms for every query
The Helmcode platform has a lot of telemetry, and what set this off was a latency alert.
Part of our infrastructure runs on Kubernetes, split across two data centers, one in Germany and one in Finland. The applications were already in one of them (zone B), but several PostgreSQL databases were still in the other (zone A). The round trip between the two is about 24 ms.
That doesn't sound like much, but our applications run several queries per request, and many of them are sequential: each one waits for the previous one, so each one pays its own trip between data centers. Move the slider to see how it adds up.
Network time a request adds up before the migration
Network only (n × 24 ms), not counting the time PostgreSQL takes to run each query.
A request with 3 queries spends about 72 ms on the network alone. With 10 or 12 queries it got close to 300 ms, and at p95 one operation was taking around a second. All that time went into traffic between data centers, not into PostgreSQL.
So we decided to move the databases into the data center where the applications already were, without stopping the services.
How we moved each database
We used CloudNativePG, the PostgreSQL operator we already ran on Kubernetes. Before starting, we checked how each application connected, because we needed to know which services could wait a few seconds during the switch and which couldn't. Then, for each database:
- We created two new replicas in the target data center. While they copied and caught up, the original database kept working as usual.
- Once the replicas were in sync, we removed the old replica and configured Kubernetes so the new instances could only run in the target data center.
- We switched the primary (a switchover): one of the new replicas became the primary instance. Before that, we checked it was fully in sync and that no backups, deployments or jobs could get in the way.
- We removed the old primary. Each database ended up with its primary and one replica in the new data center.
The five phases of moving each database
Primary and replica in zone A. The applications, in zone B, cross 24 ms on every query.
Nothing moves in the primary switch: a replica that already lives in zone B gets promoted. Doing it in this order means connections only have to drop once per database, for about 10–16 seconds.
For the services that could wait, we paused PgBouncer during the switch so requests queued up. In one of the switches, requests waited 12 seconds instead of failing.
The authentication problem after the switchover
Something made us stop the migration. After switching the primary of one database, PgBouncer started rejecting connections with authentication errors.
The cause was in how PostgreSQL stores passwords. With SCRAM it doesn't keep the password, but a verifier derived from it and a random salt:
SCRAM-SHA-256$<iterations>:<salt>$<StoredKey>:<ServerKey>
When CloudNativePG applied the password on the new primary, PostgreSQL generated a new salt. The password was the same, but the verifier changed, and PgBouncer kept trying to authenticate with the old one. You can reproduce it here: the verifiers are computed for real in your browser.
The SCRAM verifier after a primary switch
PgBouncer has stored
The primary has
Starting point: PgBouncer and the primary hold the same verifier.
The fix was simple: give CloudNativePG the precomputed SCRAM verifier instead of the password, so no new one gets generated.
Before applying it in production we tested it in an isolated environment, with planned primary switches and with a simulated primary failure. Without the change, more than half of the clients had authentication errors after the switchover. With it, none did. Then we applied it in production without restarting any instance and carried on with the migration.
This removed the authentication failures, not the 10–16 second cut of each switchover: during that window some requests failed or had to be retried.
Latency before and after
We compared real production latencies before and after the migration:
- An operation that runs 10–12 queries went from 273 ms to 7.7 ms, about 35 times faster.
- One that runs 3 queries went from 75 ms to 3 ms, 25 times faster.
- A credential validation operation went from 396 ms to 48 ms, more than 8 times faster.
These are median latencies (p50). The p95 improved too, though by different factors: the first operation went from 522 ms to 20 ms. Switch percentiles in the chart to compare them.
Production latency before and after, by operation
10–12 query operation
3 query operation
Credential validation
The problem was where the database ran
In one afternoon we moved three complete databases and the primary of a fourth, including the time we spent finding and fixing the authentication problem. We didn't schedule any maintenance window.
That doesn't mean there were no interruptions. Each primary switch took between 10 and 16 seconds and some requests failed or had to be retried, but the services kept running throughout the migration.
We didn't optimize any queries or change anything in PostgreSQL. 24 ms of network sounds like nothing, but a request that crosses that distance 10, 20 or 30 times pays for every one of them, and in our case moving the databases next to the applications was enough to make some operations 35 times faster.