FOUNDING PRICING · 40% OFFfor the first 1,000 members · from ₹95/month
OutageGitLab11 min read

GitLab's 2017 Database Outage: When Five Backups Failed

How GitLab.com lost about six hours of database changes on 31 January 2017, why none of its backup methods worked, and the database theory behind it.

ShareXLinkedInWhatsApp
Rows of tower servers on metal shelving, lit by blue LEDs, in the server room of the hosting company BalticServers; an illustrative photograph, not GitLab's infrastructure.
Rows of tower servers on metal shelving, lit by blue LEDs, in the server room of the hosting company BalticServers; an illustrative photograph, not GitLab's infrastructure. Photo: BalticServers.com, CC BY-SA 3.0, via Wikimedia Commons.

On 31 January 2017, an engineer at GitLab who was trying to repair a broken database replica deleted the data directory of the primary database instead. GitLab.com was down for about 18 hours, and when it came back, roughly six hours of database changes were gone for good. The deletion is the part everyone remembers, but the more useful lesson is what happened next: the team went looking for a backup, and one mechanism after another turned out to be empty, switched off, or never meant for the job.

Diagram of the incident: the standby database falls behind the primary, the primary's data directory is removed by mistake during the rebuild, four recovery options are checked and three are unusable, and the site is restored from a six-hour-old LVM snapshot on staging.

What users lost

GitLab.com is the hosted version of GitLab. Its PostgreSQL database holds the metadata around code: projects, issues, merge requests, comments, snippets and user accounts. The Git repositories themselves and the wikis live on separate file storage and were not touched.

GitLab's post-mortem says changes made to the database between about 17:20 and 23:30 UTC on 31 January were lost. Its best estimate was at least 5,000 projects, 5,000 comments and roughly 700 new user accounts. Customers running their own GitLab installations were not affected.

The setup before the incident

GitLab.com ran one PostgreSQL primary and one secondary, on hosts the company refers to as db1 and db2. The secondary was a hot standby used only for failover, so the primary carried the entire load and was, in GitLab's own words in the post-mortem, a single point of failure. The team knew this. On the day of the incident an engineer was setting up several PostgreSQL servers in the staging environment to try out pgpool-II, a tool that would spread read queries across more hosts.

To make staging realistic, the engineer took an LVM snapshot of the production database at about 17:20 UTC and loaded it into staging. That copy normally happened automatically once every 24 hours. Taking it by hand that afternoon turned out to be the reason GitLab lost six hours of data and not twenty-four.

Timeline

Times are UTC and approximate, as given in GitLab's post-mortem. The company's first public post, written during the incident, puts some of these events slightly earlier.

Time (31 Jan)Event
17:20Manual LVM snapshot of production taken and loaded into staging
19:00Database load climbs, suspected to be spam; users cannot post comments
23:00The secondary falls too far behind and replication breaks; engineers start rebuilding it
23:30The primary's data directory is removed by mistake; about 300 GB is gone before the command is stopped
After 23:30Engineers search for backups and find the regular ones do not exist
1 Feb, 17:00Database restored from the staging snapshot, without webhooks
1 Feb, 18:00Webhooks restored, final checks done, service back
Timeline from 17:20 UTC on 31 January to 18:00 UTC on 1 February 2017: a manual LVM snapshot at 17:20, load climbing at 19:00, replication breaking at 23:00, the primary data directory removed at 23:30, the database restored at 17:00 and service back at 18:00, with about six hours of changes lost and about 18 hours of downtime marked as two spans.
The loss window and the downtime window on one timeline

Root cause: a stalled replica and a manual rebuild

The evening started with a load problem. GitLab suspected spam, and later found a second cause: a background job was hard-deleting the account and data of a GitLab employee whose account had been reported for abuse and was mistakenly scheduled for removal. Both ran at once and the primary was writing far more than usual.

To see why that breaks a replica, you need one fact about PostgreSQL. Every change is first appended to the write-ahead log (WAL), which is stored as a series of segment files. A streaming replica stays current by receiving that log from the primary and replaying it. The primary does not keep WAL segments for ever. It recycles old ones once it no longer needs them for its own recovery. If a replica is slow and the primary is writing quickly, the primary can recycle a segment the replica has not yet received.

That is what happened. Once the needed segments were gone from the primary, the secondary could not continue. A database with WAL archiving would have had those segments copied somewhere safe and could have fetched them. GitLab.com was not using WAL archiving, so the only way back was to empty the secondary's data directory and take a fresh full copy from the primary with pg_basebackup.

Schematic of the primary's write-ahead log as a row of segment files: the oldest segments have been recycled while a slow standby still needs one of them, so it cannot continue; without WAL archiving the only fix is a full copy with pg_basebackup, and with archiving the standby could fetch the missing segments.
Why a lagging standby gets stuck once the primary recycles WAL

An engineer wiped the secondary's data directory and ran pg_basebackup. It hung with no useful output. After some attempts it reported that the primary had no free replication connections, a limit set by max_wal_senders. The team raised that setting from 3 to 32, which needs a restart, and PostgreSQL then refused to start, complaining about semaphores. The cause was max_connections, which had been set to 8000 nearly a year earlier. They reduced it to 2000 and PostgreSQL came back.

pg_basebackup still appeared to do nothing. Running it under strace showed only that it was waiting in a poll call. GitLab later learned that this is normal: the tool waits silently until the primary begins sending data. That behaviour was not written down in the team's runbooks, and it was explained afterwards by an engineer who had not been present that night.

At about 23:30, one engineer guessed that earlier attempts had left files behind in the secondary's data directory and decided to clear it again. The command was run in a terminal connected to the primary. The engineer noticed and stopped it within a second or two. By then around 300 GB had been deleted, and GitLab's first post says only about 4.5 GB remained.

It is worth being precise about the human part. This was late at night, hours into an incident, on two hosts with near-identical names, following a procedure that was manual and undocumented. One of the first fixes GitLab listed was making shell prompts show clearly which host and environment you are on. Its own analysis treats the missing automation and documentation as the cause, not the individual.

Why the safeguards did not help

With the primary gone, the question became which copy of the data to restore from. GitLab's first post counted five backup or replication techniques and said none was working reliably or set up at all. The post-mortem walks through the main ones.

MechanismWhat it was forWhat the team found
Daily pg_dump uploaded to S3The standard backupThe bucket was empty. The job ran pg_dump 9.2 against a 9.6 database and failed every time
Streaming replication to db2Failoverdb2 had just been wiped for the rebuild, and it would have replayed any mistake anyway
Azure disk snapshotsWhole-disk recoveryEnabled for the NFS servers, never enabled for the database servers
Daily LVM snapshotRefreshing stagingNot designed for recovery; the automatic one was about 24 hours old

The pg_dump failure deserves a closer look, because it is two bugs stacked on top of each other. The backup ran on an application server. GitLab's Omnibus package ships both PostgreSQL 9.2 and 9.6 and decides which tools to use by looking for a version file in the database's data directory. Application servers have no data directory, so the package fell back to 9.2. pg_dump refuses to dump a server newer than itself, so the job exited with an error each night.

A failing cron job should have sent an email. It did, and the receiving mail server rejected those messages because, as GitLab put it, they were not signed using DMARC. So the backup failed, the alarm about the backup also failed, and nobody was assigned to check either. The post-mortem states that no one owned the backup procedure and so no one tested it.

A charred tower server standing on an office floor, its front panel and drive bays melted by fire.
A server destroyed in a fire in Oklahoma in 2005, shared by its photographer as a reminder to keep backups and test restores. Illustrative; not GitLab hardware. Photo: John from USA, CC BY 2.0, via Wikimedia Commons.

Azure disk snapshots had been left off for the database hosts on the assumption that the other methods were enough.

The recovery, and what GitLab changed

That left the LVM snapshot taken by hand at 17:20, which was already loaded into the staging database. GitLab chose it over the automatic snapshot because the alternative meant losing close to 24 hours.

The restore was slow for a mundane reason. Staging ran on cheaper Azure classic storage in a different region, and GitLab says the disks were throttled to around 60 Mbps. Copying the data to the production host took about 18 hours. After the copy, the team bumped every database sequence by 100,000 so that new rows could not reuse an ID handed out during the lost window, then brought the site back in stages. The staging refresh process deletes webhooks, so those were recovered separately from a second copy of the snapshot.

GitLab ran the recovery in public. The team kept a live document of its notes, posted updates on Twitter, and streamed the work on YouTube, where the post-mortem says the audience peaked at around 5,000 viewers. The notes initially named the engineer involved. GitLab said it would redact names in future incidents.

The post-mortem lists the follow-up work as public issues. The main items:

  • Shell prompts on every host changed to show the host and environment clearly.
  • LVM snapshots of production taken hourly instead of daily. This was already in place when the post-mortem was published.
  • Monitoring of backups in Prometheus, plus a public dashboard, so that a missing backup raises an alert instead of relying on email.
  • Automated testing that a PostgreSQL backup can actually be restored.
  • Work on WAL archiving and point-in-time recovery, including evaluating WAL-E and Barman.
  • Several secondaries with load balancing, written runbooks for replication, and a named owner for data durability.
  • A change so that removing a user flagged for spam is no longer an immediate hard delete.

The computer science underneath

Write-ahead logging. A database makes a change durable by appending it to a log before touching the data pages. After a crash, it replays the log to rebuild whatever was not yet on disk. Our lesson on write-ahead logging and crash recovery covers the mechanism. The same log is what replication ships to a standby, which is why a standby that misses a stretch of WAL cannot simply skip ahead: every record assumes the ones before it were applied.

Replication is not a backup. A replica protects you when a machine dies. It does not protect you from a bad command, because a DELETE or a dropped table is replicated just as faithfully as a good write. Replication and failover explains lag and what a standby can and cannot promise. In this incident the replica was not even available, because fixing lag meant emptying it first. For a short while GitLab had exactly one copy of its database.

Backups, RPO and RTO. The recovery point objective is how much recent data you can afford to lose. The recovery time objective is how long you can afford to be down. A daily dump gives an RPO of up to 24 hours. A base backup plus archived WAL allows point-in-time recovery, where you restore the base and replay the log up to a chosen moment, which cuts the RPO to minutes. GitLab's actual figures that night were about six hours of loss and about 18 hours of downtime, and both were decided by luck and disk speed, not by design. See backups, point-in-time recovery and planning for disaster.

A dim view down the inside of a robotic tape library, with walls of tape cartridges in slots on both sides and a StorageTek robot arm in the foreground.
Inside a StorageTek tape library at NERSC, one way large sites keep older copies of data. Illustrative; GitLab did not use this system. Photo: Derrick Coetzee, CC0, via Wikimedia Commons.

Silent failure. A check that reports only when something goes wrong cannot tell "all fine" apart from "the reporter is broken". The safer pattern is to alert on the absence of success: record when the last good backup finished and page someone if that timestamp gets old. Our lesson on observability covers alerting on symptoms. The email detail is its own topic, covered in SPF, DKIM and DMARC: a receiving server is entitled to reject mail that fails the sender domain's authentication policy.

What an interviewer could ask from this

Why can a replica not replace a backup? Replication copies every change, including destructive ones, within seconds. A backup is a copy frozen at an earlier time, so it still contains what was deleted.

A standby reports that a required WAL segment has been removed. What happened and how do you prevent it? The primary recycled a segment before the standby received it. Prevent it by archiving WAL so the standby can fetch old segments, by using a replication slot so the primary retains what a standby still needs, or by keeping more WAL on the primary. Slots need disk monitoring, because a dead standby makes the primary hoard WAL.

How would you know your backups work? Restore them on a schedule into a scratch database, run sanity queries, and alert if the restore fails or the newest backup is older than the RPO. A backup that has never been restored is an assumption.

Why did GitLab increase all sequences after restoring? IDs issued during the lost six hours may already appear in emails, URLs and external systems. Skipping ahead ensures a new row never takes an ID that once meant something else.

How do you reduce the chance of running a destructive command on the wrong host? Make hosts visibly different in the prompt, script the dangerous procedure so it checks the server's role before acting, and require a second person for irreversible steps in production.

Takeaways

  • The deletion took seconds. The outage took 18 hours because recovery had never been rehearsed.
  • Count your backups by restores that have succeeded, not by jobs that are configured.
  • Replication gives availability. Only a separate, older copy gives protection from mistakes.
  • Alert when success stops arriving, not only when failure is reported.
  • GitLab's openness, down to publishing its own list of failures, is why this incident can be studied at all.

Sources

  1. Postmortem of database outage of January 31 (GitLab Blog, 10 February 2017)
  2. GitLab.com database incident (GitLab Blog, 1 February 2017)

Written by the Codevizag Academy team

An independent explainer built from the public sources listed above. Codevizag Academy is not affiliated with GitLab. Found a mistake? Write to support@codevizag.com.

More case studies

Understood it? Now prove it.

The lessons behind this case study are free, and 20 problems a month are judged on real test cases at no cost.

Start solving free