SRE Database Reliability

Databases hold the most valuable part of a service: its data. Servers can restart freely, while lost or corrupted data may never return. Database reliability work protects data, keeps queries fast, and makes changes safe.

Why Databases Need Special Care

Application servers hold no memory of past requests, so a new server replaces a broken one in seconds. A database holds state. Imagine a library. Staff can replace a reading lamp in a minute, yet a lost shelf of books takes years to rebuild. Teams treat databases with matching caution.

Replication

Replication copies data from a primary database to one or more replicas. Replicas serve read traffic and stand ready to take over.

 Writes → [Primary] ──copy──→ [Replica 1] ← Reads
                    └──copy──→ [Replica 2] ← Reads

Replication Lag

Replicas trail the primary by a small delay. A delay of two seconds can show a customer an old balance after a payment. Monitor lag and route sensitive reads to the primary.

Failover

Failover promotes a replica when the primary fails. Automatic failover shortens downtime, and careful rules prevent two databases from both acting as primary, a dangerous state called split brain. Rehearse failover on a schedule.

Replication Is Not a Backup

A replica copies mistakes instantly. A deleted table disappears from every replica within seconds. Backups keep older versions. Combine both: replicas for availability, backups for recovery.

ToolProtects AgainstDoes Not Protect Against
ReplicaMachine or zone failureAccidental deletes, bad updates
BackupDeletes, corruption, attacksShort downtime (restore takes time)
Point-in-time recoveryMistakes at a known momentLoss of the log archive itself

Connection Pooling

Each database connection uses memory. Thousands of application instances can exhaust the limit. A pooler keeps a fixed number of connections open and shares them among many clients, like a taxi rank that serves a long line of passengers with a limited number of cars.

Keeping Queries Fast

  • Add indexes for columns that queries filter or sort on.
  • Review the slowest queries every week.
  • Avoid loading huge result sets into memory.
  • Set timeouts so one slow query cannot block everything.
  • Cache results that rarely change.

Safe Schema Changes

Changing a table while users read it can lock the table and stop the service. The expand and contract pattern avoids that risk.

[1 Expand]   add the new column, keep the old one
[2 Migrate]  copy data in small batches
[3 Switch]   deploy code that reads the new column
[4 Contract] remove the old column after a safe waiting period

Each stage ships separately, and each stage can roll back.

Scaling Beyond One Machine

Read replicas scale reads. Sharding splits data across several primaries, such as by customer region, to scale writes. Sharding adds complexity, so teams try indexing, caching, and bigger machines first.

What to Monitor

  • Connections in use against the limit
  • Replication lag
  • Slow query count
  • Disk space and growth rate
  • Backup success and last restore test
  • Lock waits and deadlocks

Example: Full Disk

A reporting job writes huge temporary files, and the database disk climbs to 97 percent at midnight. An alert at 80 percent had warned the team a week earlier, so a planned expansion already exists. The team extends storage in ten minutes and moves the job to a replica. Users never notice.

Common Mistakes

  • Treating replicas as backups.
  • Running schema changes during peak hours with no rehearsal.
  • Ignoring disk growth until it hits 100 percent.
  • Giving every application full administrator rights.

Key Takeaways

Protect data with replicas plus tested backups, pool connections, tune queries, and change schemas in small reversible steps. Monitor lag, space, and connections to catch trouble early.