SQLite Needs Reliable File Locks, So Its Database Belongs on Local Disk, Not on NFS

In progress Kubernetes Linux

SQLite has no server: every process that opens the database file coordinates through file locks on that file. On a local disk the kernel enforces those locks. On a network filesystem they depend on the NFS server, the client and the network all honouring them, which they often don't do reliably. The result is database is locked errors, and in the worst case a corrupted database. Self-hosted apps that keep their state in SQLite therefore need their data directory on local storage.

How It Fails on NFS

  • Stale locks after a crash: a client that dies holding a lock can leave it in place. After a power cut, an app restarted on the same share reports database is locked although nothing else is using the file.
  • Slow and unsafe writes: each transaction is several small synchronous writes and lock round trips over the network, so the app's UI lags, and two clients briefly writing at once can damage the file.

The Split That Works

Put each app's small, write-heavy state (its /config with the SQLite file) on a local volume, and keep only large shared files on NFS:

Data Storage Why
App database, settings local disk (local-path) real locks, fast synchronous writes
Media, downloads NFS (ReadWriteMany) shared by several apps across nodes

The cost is that a local-path volume ties the pod to its node: if that node dies, the pod can't start elsewhere until the claim is recreated (see Kubernetes Storage Failures Usually Start on the Host or the NAS). That is a known, recoverable failure, unlike a corrupted database.

References

Review cards

Flashcard Why does SQLite depend on reliable file locks?

It has no server: every process using the database coordinates through locks on the file itself, which only a local disk enforces reliably.

Flashcard A self-hosted app keeps its SQLite file on an NFS share. What two failures should you expect?

database is locked errors from stale or unreliable locks, and in the worst case a corrupted database when two writers overlap.

Quiz After a power cut, an app whose SQLite file is on NFS reports `database is locked`, though nothing else uses the file. What is going on?
  • The disk is full
  • A lock held by the client that crashed was never released
  • Another copy of the app is writing to the file
  • The database is corrupted beyond repair

A client that dies holding a lock can leave it on the share. Stopping the app may let it expire, but the cause stays as long as the database lives on NFS.