# A cloned SQLite server is split-brain until you kill one copy

> Cloning a server with an embedded SQLite file leaves two writable copies that drift silently. A five-minute check proves each DB file has exactly one owner.

- Author: Chad Priest
- Published: 2026-10-14
- Canonical URL: https://blog.awe.run/a-cloned-sqlite-server-is-split-brain-until-you-kill-one-copy/
- Tags: sqlite, devops, aws, debugging

---

On 2026-10-11 I cloned our web server so a new domain could have its own box. Three days later my notes held a line I did not enjoy writing: the new server had "its own drifting copy of the SQLite DBs." That is split-brain. You get it any time you fork a server by copying a file-based database. You end up with two writable copies, no replication between them, no single owner, and nothing that reports the problem. It looks the same in a self-hosted Rails or Django app on SQLite, in PocketBase, or in a PHP app on a single VPS, which is what ours is.

## A clone is a fork, and every integration still pointed at the old copy

I was wrong about what a clone is. I treated it as a move. The old box is a t2.micro running nginx and php-fpm. It holds two SQLite files: one for users, sessions and MFA, and one for usage, quotas and Stripe state. I launched a t3.micro from its image and pointed the new domain, app.awe.run, at it. The old box stayed up, because it still serves app.awe.run and two other sites. It still had the same app and the same database files.

Here is the part I missed. DNS was never the leak. Nothing was cached and nothing was misrouted. The old box kept taking writes because every integration I had ever configured names the old hostname in its own settings: the SMS relay, the LLM proxy, the Mac clients, the Stripe webhooks, the OAuth redirect URIs, and the session cookies. None of those read my DNS change, because none of them were looking up the new name. Meanwhile, signups through app.awe.run were writing to the new box. Both copies were taking real writes, and each one was internally fine.

That is why corruption checks are no help. `PRAGMA integrity_check;` looks inside one file and answers `ok` for a file that is internally consistent. Divergence lives between two files, and no single-file check can see it.

If you are planning a cutover, first list every system that stores your hostname in its own config: webhook dashboards, OAuth apps, shipped clients, cron jobs on other machines, and other sites on the same host that share the DB. Each one keeps writing to the old copy until you edit it.

## A copied database file stays a fork until one copy cannot write

**Every writable database file has exactly one process, on one host, that can open it for writing. Every other copy is deleted or read-only.**

### Step 1: prove which hosts are still being written to

Don't use `lsof` for this. php-fpm opens the SQLite file on each request and closes it when the request ends, and Rails connection reaping, cron scripts and per-request workers behave the same way. `lsof` shows no holder on either host, even while both hosts are writing, so it reports a PASS for exactly the failure you are checking for. Look for evidence of writes.

On each host, check who is POSTing after the cutover:

```bash
grep '"POST ' /var/log/nginx/access.log | grep -v -e health -e status | tail -20
grep '"POST ' /var/log/nginx/access.log | grep -v -e health -e status | awk '{print $1}' | sort | uniq -c | sort -rn
```

The first command gives you timestamps. Anything after your cutover on the host that should be retired is a live writer. The second command lists those writers by client IP, which is how you find the webhook or client you forgot about.

Then confirm that the file itself is changing. Sample its mtime twice, a few minutes apart:

```bash
DB=/srv/app/data/app.db   # your path
for f in "$DB" "$DB-wal"; do
  [ -e "$f" ] || { echo "$f: absent"; continue; }
  stat --format='%n %y' "$f" || stat -f '%N %Sm' "$f"   # GNU, then BSD/macOS
done
```

Or, on Linux, watch it for a minute: `timeout 60 inotifywait -m -e modify "$DB" "$DB-wal"`.

Pass: only one host shows post-cutover POSTs and a moving mtime. Fail: both do.

### Step 2: measure the drift

Run this on both hosts. It walks every table in `sqlite_master`, so you don't need to know the schema:

```bash
DB=/srv/app/data/app.db
for t in $(sqlite3 -readonly "$DB" "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"); do
  sqlite3 -readonly "$DB" "SELECT '$t', count(*), max(rowid) FROM \"$t\""
done
sqlite3 -readonly "$DB" "PRAGMA integrity_check;"
```

Errors are not suppressed, on purpose. A `WITHOUT ROWID` table will error on `max(rowid)`. Read the error, then count that table by its primary key.

Counts are not proof. If one side deleted rows and the other inserted the same number, the counts match while the rows differ. Rowid reuse after deletes also makes `max(rowid)` unreliable. To compare content, pull consistent copies to one machine and diff them:

```bash
ssh old-host "sqlite3 $DB '.backup /tmp/old.db'" && scp old-host:/tmp/old.db .
ssh new-host "sqlite3 $DB '.backup /tmp/new.db'" && scp new-host:/tmp/new.db .
sqldiff old.db new.db
```

`sqldiff` ships with SQLite. Pass: it prints nothing. Fail: it prints the `INSERT`, `UPDATE` and `DELETE` statements that would turn the first file into the second. Each statement is one row that diverged. If you can't move the files, hash the rows past the fork point on each host: `sqlite3 -readonly "$DB" "SELECT * FROM users WHERE rowid > N ORDER BY rowid" | sha256sum`. Use `shasum -a 256` on macOS. N is the table's highest rowid at clone time.

I have not run Step 2 against our two pairs of files yet, so I can't print our numbers. That is a hole in this post, and I'd rather leave it visible than fill it with a guess. What I do know is the shape: the old box holds everything that came in through app.awe.run and its integrations since the 11th, and the new box holds the awe.run signups.

### Step 3: enforce the invariant, in this order

On the host that loses:

```bash
sudo systemctl stop php-fpm            # 1. stop every writer first (your app service)
sqlite3 app.db "PRAGMA wal_checkpoint(TRUNCATE);" && mv app.db app.db.frozen-2026-10-14   # 2. fold the WAL in, then rename
chmod 444 app.db.frozen-2026-10-14     # 3. read-only
```

The order matters. If you rename while the service runs, the next request opens `app.db`, finds nothing, and SQLite creates an empty database at that path. Now you have three copies. If you rename without checkpointing, committed transactions stay behind in `app.db-wal`. Also, `PRAGMA query_only` doesn't help here. It is per-connection and only restrains the connection that sets it, so it does nothing to the file.

## Reconciling: pick the winner by its integrations, not by its newness

CRDT merge tools like [cr-sqlite](https://github.com/vlcn-io/cr-sqlite/) merge forks you designed for, and nobody designed this one, so they don't apply.

Choosing the winning copy was not a close call. The old copy wins because it is the one Stripe, OAuth, the relay and the clients actually talk to. Picking the new box would mean re-pointing all of them and replaying whatever they had already sent to the old one. The losing copy's own rows are the awe.run signups. The plan is to take `sqldiff old.db new.db`, keep only the `INSERT` statements for those accounts, check each one for an email or ID collision with a row on the winner, and apply them by hand. Then point app.awe.run at the winning data and run Step 3 on the loser. Until that is done, this is not reconciled. It is a list of what I owe.

My rule now: a cutover is not finished when DNS moves. It is finished when the old copy can no longer write, and every system that stores your hostname has been told.

---

Source: [A cloned SQLite server is split-brain until you kill one copy](https://blog.awe.run/a-cloned-sqlite-server-is-split-brain-until-you-kill-one-copy/) by Chad Priest, from Building awe in Public.
