Skip to content

How to replicate SQLite to another machine

Keep a readonly copy of your database on every machine that reads it, a few seconds behind, as an ordinary SQLite file.

Start a replica

Run it on a machine that joined your server; it returns once the first version is local.

// 1. Get your Litebackup server: litebackup.com/get-started
// 2. In your shell, with the token from Machines, Add machine in your console:
//      npm install @litebackup/sdk
//      export LITEBACKUP_TOKEN=YOUR_JOIN_TOKEN
// 3. On the machine that writes: backup("/var/lib/app/main.sqlite", { prefix: "app/" })

// 4. On each machine that reads:
import Database from "better-sqlite3";
import { replicate } from "@litebackup/sdk";

await replicate("app/main.sqlite", { to: "/var/lib/app/main.sqlite" });
const db = new Database("/var/lib/app/main.sqlite", { readonly: true });

Replicate every database under a prefix

sudo litebackup replicate --prefix app/ --to /var/lib/app/replicas

Each database lands at its name after the prefix, such as /var/lib/app/replicas/main.sqlite.

Read it with any driver

A replica is an ordinary SQLite file, so any driver in any language reads it, with no client library. It is usually a few seconds behind the database it copies.

Keep writes out

--write-protection When an app writes to the replica
lock (default) The write fails with SQLITE_BUSY, and reads carry on.
detect Litebackup notices the write and brings the replica back in sync.
none Nothing stops it, so open the file readonly (mode=ro).

In the SDK it is writeProtection (TypeScript) or WriteProtection (Go).

Manage replicas

litebackup replicate list shows each replica and its version. litebackup replicate stop app/main.sqlite stops one and leaves the file in place.

What replicas are for

  • Reads close to users. Serve reads from every server instead of one.
  • Reports and analytics. Run heavy queries on a copy, away from the database your app writes.
  • A warm standby. With failover, a replica takes over writes when the origin goes down.

Send writes to one machine

One machine writes each database, and the rest read. To move writes between machines, use failover, or acquire and release in the SDK for worker pools.

Next time

Get started

Install your Litebackup server and back up a first database in minutes: get started, or see pricing.