Running SQLite in Production: Notes From My Projects
When an embedded SQLite database in WAL mode is enough for a small backend, with the Go setup and PRAGMAs I use and a benchmark from my own server.
· 3 min read · Erik Kubica
When a Separate Database Server Is More Than You Need
A pattern I see often (and have followed myself) is to write a simple CRUD API for a client with 6,000 registered users, and before writing the first endpoint, spin up a managed PostgreSQL instance on AWS RDS or DigitalOcean for 45 € a month, configure VPC peering, set up connection pools, and add an in-memory Redis cluster.
The usual reason is "we might need to scale horizontally later."
On a number of projects I have replaced setups like this with something simpler. Many of the business backends I've worked on are read-heavy catalogs, billing dashboards, or internal booking systems, with a read-to-write ratio around 99:1.
If your whole database fits comfortably in memory on a single server, running it in the same process is worth considering.
The Cost of the Network Hop
When your backend queries an external database over TCP, even on localhost, you pay for:
- Kernel socket buffers and context switches.
- Binary serialization and deserialization over the wire.
- TCP packet roundtrips (usually 0.8ms to 2.5ms even within the same datacenter).
With SQLite, the database is an embedded library linked into your application binary. When my Go backend executes a SELECT, there is no socket and no network serialization. The query engine reads from local memory pages mapped from NVMe storage.
// Opening SQLite in Go with production PRAGMAs
package main
import (
"database/sql"
"log"
_ "modernc.org/sqlite"
)
func openDB(path string) (*sql.DB, error) {
db, err := sql.Open("sqlite", path)
if err != nil {
return nil, err
}
// Set production concurrency parameters
pragmas := []string{
"PRAGMA journal_mode = WAL;",
"PRAGMA synchronous = NORMAL;",
"PRAGMA busy_timeout = 5000;",
"PRAGMA cache_size = -64000;", // 64 MB page cache
"PRAGMA foreign_keys = ON;",
}
for _, p := range pragmas {
if _, err := db.Exec(p); err != nil {
return nil, err
}
}
return db, nil
}
The Three PRAGMAs That Matter
By default, SQLite locks the whole database file during writes, which is why many people assume it can't handle web traffic. Three PRAGMA commands change that a lot:
PRAGMA journal_mode = WAL;(Write-Ahead Logging). Readers no longer block writers, and writers no longer block readers. You can have 50 concurrent HTTP goroutines reading from the database while a background worker inserts telemetry rows.PRAGMA synchronous = NORMAL;In WAL mode,NORMALsyncs the file at checkpoints rather than on every write, which keeps the database consistent after a crash without hammering disk IOPS. You can lose the last few transactions on power loss, so check that this is acceptable for your data.PRAGMA busy_timeout = 5000;If a transaction needs a write lock, it waits up to 5,000ms instead of immediately returningSQLITE_BUSY.
A Benchmark From My Setup
I benchmarked this on a modest server (2 vCPU, 4 GB RAM, NVMe SSD), using simple indexed key-value lookups:
- PostgreSQL 16 over TCP localhost: ~12,400 req/sec, averaging 1.1ms latency per request.
- SQLite 3 in-process with WAL: 105,200 req/sec, averaging 0.04ms latency.
That is roughly 8x the throughput and a much lower latency on that machine. It is a narrow test (simple lookups, no real application logic, a single box), so the gap on a real workload will be smaller.
What about backups? You don't need to stop the service. You run:
-- Online non-blocking snapshot backup
VACUUM INTO '/backups/app_backup.db';
Then rsync the single file to an offsite S3 bucket.
SQLite does not fit every project. If you need several application servers writing to the same database, or heavy write concurrency, Postgres or something distributed is the better choice. For an API with a modest number of users and one server, it has been enough for me, and it saved some monthly hosting cost.