DEV Community

Grace Evans
Grace Evans

Posted on

Cheap & Cheerful High Availability: Replicating SQLite with Litestream

Turn a single‑file database into a fault‑tolerant backend that can survive server crashes and scale reads, all without leaving the comfort of SQLite.


Why care about SQLite replication?

  • Zero maintenance: no DBA required, no cluster to babysit

  • Tiny footprint: runs great on a $5 VPS

  • Transactional guarantees: WAL mode plus point‑in‑time restore

  • Lower cost: S3 object storage instead of multi‑node Postgres

If you have a side project or internal tool that fits on one machine, Litestream keeps it safe and highly available.


What is Litestream?

Litestream is an open‑source replication tool written in Go. It streams SQLite's WAL (Write‑Ahead Log) to cloud storage such as S3, Backblaze B2, or Azure Blob while your app is running. You get:

  • Continuous off‑site backups every few seconds

  • Point‑in‑time recovery with a single command

  • Read‑only replicas for scaling analytics or dashboards


Demo architecture

┌─────────────┐      WAL pages      ┌────────────┐
│  VPS (app)  │ ───────────────────▶│   S3 bucket│
│  FastAPI    │                     └────────────┘
│+ Litestream │
└─────────────┘

Enter fullscreen mode Exit fullscreen mode
  • A FastAPI app writes to db.sqlite3.

  • Litestream tails the WAL and pushes deltas to S3 every 5 seconds.

  • If the VPS dies, spin up a new one and run litestream restore to the latest commit or any timestamp.


Step 1: install Litestream

Ubuntu example:

curl -fsSL https://litestream.io/install.sh | sudo bash

Enter fullscreen mode Exit fullscreen mode

Verify:

litestream version

Enter fullscreen mode Exit fullscreen mode

Step 2: create an S3 bucket and IAM user

  1. Create a bucket called my-sqlite-backups.

  2. Make it private; enable versioning.

  3. Create an IAM user with PutObject, GetObject, and ListBucket permissions on that bucket.

  4. Copy the access key and secret.


Step 3: add a Litestream config

/etc/litestream.yml

dbs:
  - path: /home/ubuntu/app/db.sqlite3
    replicas:
      - url: s3://my-sqlite-backups/db
        access-key-id: YOUR_KEY
        secret-access-key: YOUR_SECRET
        sync-interval: 5s

Enter fullscreen mode Exit fullscreen mode

Step 4: run Litestream alongside your app

Systemd unit /etc/systemd/system/litestream.service

[Unit]
Description=Litestream replication service
After=network.target

[Service]
ExecStart=/usr/local/bin/litestream replicate -config /etc/litestream.yml
Restart=always
User=ubuntu
Group=ubuntu

[Install]
WantedBy=multi-user.target

Enter fullscreen mode Exit fullscreen mode

Enable and start:

sudo systemctl enable --now litestream

Enter fullscreen mode Exit fullscreen mode

Check logs:

journalctl -u litestream -f

Enter fullscreen mode Exit fullscreen mode

You should see synced 4.2 KB to replica.


Step 5: verify backups

List generations:

litestream snapshots -config /etc/litestream.yml

Enter fullscreen mode Exit fullscreen mode

Restore locally:

litestream restore -o restored.sqlite3 -config /etc/litestream.yml

Enter fullscreen mode Exit fullscreen mode

Open the file with sqlite3 and confirm your data is intact.


Scaling reads with read‑only replicas

Some workloads need heavy SELECT queries for dashboards. Launch a second VPS, restore once, and run Litestream in replica‑only mode:

litestream restore -o db.sqlite3\
  -config /etc/litestream.yml\
  -timestamp now

litestream replicate -config /etc/litestream.yml -exec "/usr/bin/python read_only_api.py"

Enter fullscreen mode Exit fullscreen mode

Point your analytics service to this node. Writes still hit the primary; reads can scale horizontally.


Disaster recovery drill

  1. Primary VPS explodes.

  2. Deploy a fresh VPS with the same app code.

  3. Install Litestream.

  4. Run litestream restore -o db.sqlite3 -config /etc/litestream.yml -timestamp max.

  5. Start your application.

Downtime is the time it takes for DNS or load balancer to switch IPs plus the restore command (usually seconds).


Cost breakdown

Resource Monthly cost
1 × 1 vCPU VPS USD 5.00
S3 storage (5 GB) USD 0.12
S3 PUT requests USD 0.01
Total ≈ 5.13

Cheaper than running even a single‑node managed Postgres.


Limitations

  • Single writer -- SQLite's write lock means only one process should write at a time.

  • Big blobs grow the WAL fast; consider separating object storage.

  • Not ideal for multi‑region write workloads.


Conclusion

Litestream upgrades humble SQLite into a resilient datastore:

  • Continuous off‑site backups

  • Point‑in‑time restore

  • Read replicas for cheap horizontal scaling

For many SaaS side projects and internal dashboards, this setup delivers "good enough" high availability without the complexity or cost of full‑blown clusters. Give it a spin and sleep better tonight.

Top comments (0)