A data centre aisle with two server racks: one switches to amber lights, the other stays steady.
Rollout and operationsDeep dive

A database server fails. What happens in the next minute?

In an enterprise system the database is the heart. If it stops, order entry, invoicing and the warehouse stop with it. Using a fictional failure, we show how our production PostgreSQL is built: two instances on two servers, automatic failover, connection pooling, and a separate database cluster for each customer.

  • DataEdgePlatform engineer

7 min read
Updated

Every operations team knows the question the CEO asks sooner or later: what happens if the server goes down? The honest answer is that servers do go down: a power supply, a memory fault, a reboot after a kernel update. The right question is how long it takes, and whether any data is lost along the way.

Here we show how our production databases are built, through a fictional failure.

Two instances, on two separate servers

Production PostgreSQL databases are managed by CloudNativePG, a Kubernetes operator built specifically for running PostgreSQL clusters. Customers' production business databases run as two instances:

  • the primary accepts writes and reads;

  • the replica continuously follows the primary's changes.

The two instances cannot run on the same server. A mandatory placement rule ensures this: if there is no free second server, the replica does not even start. So the two instances can never quietly end up on one machine.

YAML
# the essentials of a production database cluster definition (illustration)
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: minta-ugyfel-db
spec:
  instances: 2                    # primary + replica
  affinity:
    podAntiAffinityType: required # the two instances on separate servers
  primaryUpdateMethod: switchover # on update, switch over first
  monitoring:
    enablePodMonitor: true
  bootstrap:
    initdb:
      localeCollate: hu_HU.UTF-8  # Hungarian alphabetical order
      localeCType: hu_HU.UTF-8

Illustration, fictional name. No sizes or resource values.

The collation setting looks like a detail, but it is not. In this Hungarian example it is what puts "Cs" after "C" and "Ö" in its proper place in the customer list, the partner search and every alphabetical report.

A fictional Tuesday morning

Suppose that at half past ten on a Tuesday morning, the server running the primary instance of a customer's database goes down because of a hardware fault. This is what happens:

Time

Event

10:30:00

the server goes down; the primary instance is unreachable

a few seconds

the operator detects that the primary is not responding

next

it promotes the replica to primary

at the same time

the write endpoint points to the new primary

then

applications reconnect and work carries on

when the server comes back

the old primary rejoins as a replica

Fictional scenario. The exact timings depend on the nature of the fault and the load.

At best, all a user sees is that a save failed once and worked the second time. The replica has been following the primary continuously, so the data is on the second server too. Failover is automatic; nobody needs to be awake at two in the morning for it.

Planned maintenance without downtime

Failures are the rarer case. Planned maintenance is more common: a PostgreSQL patch, an operator update, a server reboot. In these cases the operator does not stop the primary first. It updates the replica first, then switches over: the updated replica becomes the primary, and only then is the old primary dealt with. The write endpoint stays available throughout, interrupted only briefly at the moment of switchover.

The connection pool

A web system receives many short requests, and each wants a database connection. If every request opens its own connection, the database spends much of its time setting up connections. So applications connect to a PgBouncer connection pool, which shares a few long-lived connections among many requests.

The connection pool has two paths:

  • write path: always points to the current primary, including after failover;

  • read path: directs traffic to the replica where that is needed, for example for heavier queries.

The connection pool itself runs as several instances on different servers, so it does not become a single point of failure.How it runs · databaseQueue-based syncs use locking that skips rows already taken. Several workers can therefore run at once without picking up the same item twice.

What is the application doing meanwhile?

Database failover is only worth something if the applications handle it well. The services are stateless: they keep no data in memory that would be lost on reconnecting. If a request happened to be running at the moment of failover, it gets an error, and the user or the calling system retries.

Background syncs, such as passing orders to the warehouse system, work from a queue. If a run is interrupted by the failover, the item stays on the queue and the next run picks it up again. Because the queue hands an item to only one worker at a time, a repeated run does not process anything twice either.

A separate database for each customer

Every customer runs in its own environment: its own namespace, its own database cluster and its own configuration. This has two advantages. One is security: one customer's data is not even physically in the same database as another's. The other is operations: one customer's maintenance, a large import or a slow query does not slow down the rest.

On the network, too, only those we have explicitly allowed can reach the database. In each customer environment only that customer's own services can reach it; another environment would try in vain.

We see it before it becomes a problem

Prometheus collects metrics from the database clusters and the connection pools: replication lag, number of connections, query times, storage. Application logs go to Loki in structured form and can be searched in Grafana. Alerts can be set on these, so a slowdown or filling storage shows up before users notice.

The connection pool helps here too. The application connects to the pool's address, and that address does not change after failover. The pool itself knows where the new primary is, so nothing in the application's settings has to change, and nothing has to be restarted. After failover the first few requests may get an error; the following ones already reach the new primary.

Scheduled jobs in the database

Many business tasks run on a schedule: an overnight reconciliation, a view refresh, a daily summary. We run these where the data is, with a scheduler built into the database. A scheduled job therefore runs in the same environment as the data, and after failover it continues on the new primary.

And backups?

For the finance module, a full database backup is taken before every release, together with a tested restore script. If it turns out after a release that something is not working as it should, the previous state can be restored. Improving the backup regime is ongoing work. What we have not yet introduced for every database, we do not promise here.

Related: Signed images, encrypted secrets, a closed network · Why is there no server in the store?