Skip to content

Commit a8d63b5

Browse files
committed
README
1 parent b22f3f1 commit a8d63b5

File tree

1 file changed

+128
-8
lines changed

1 file changed

+128
-8
lines changed

README.md

+128-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,137 @@
11
litestream
22
==========
33

4-
Streaming replication for SQLite.
4+
Litestream is a standalone streaming replication tool for SQLite. It runs as a
5+
background process and safely replicates changes incrementally from one or more
6+
SQLite databases. Litestream only communicates with SQLite through the SQLite
7+
API so it will not corrupt your database.
58

69

7-
## Questions
10+
## Usage
811

9-
- How to avoid WAL checkpointing on close?
12+
### Installation & configuration
1013

14+
You can download the binary from the [Releases page](https://github.com/benbjohnson/litestream/releases).
1115

12-
## Notes
16+
Once installed locally, you'll need to create a config file. By default, the
17+
config file lives at `/etc/litestream.yml` but you can pass in a different
18+
path to any `litestream` command using the `-config PATH` flag.
1319

14-
```sql
15-
-- Disable autocheckpointing.
16-
PRAGMA wal_autocheckpoint = 0
17-
```
20+
You can copy this configuration below and update the source path (`/path/to/db`)
21+
and the destination path (`/path/to/replica`) to where you want to replicate
22+
from and to.
23+
24+
```yaml
25+
databases:
26+
- path: "/path/to/db"
27+
replicas:
28+
- type: file
29+
path: /path/to/replica
30+
```
31+
32+
33+
### Replication
34+
35+
Once your configuration is saved, run the `litestream replicate` command:
36+
37+
```sh
38+
# Replicate using the /etc/litestream.yml configuration.
39+
$ litestream replicate
40+
41+
# Replicate using a different configuration path.
42+
$ litestream replicate -config /path/to/litestream.yml
43+
```
44+
45+
The `litestream` command will initialize and then wait indefinitely for changes.
46+
You should see your destination replica path is now populated with a
47+
`generations` directory. Inside there should be a 16-character hex generation
48+
directory and inside there should be snapshots & wal files. As you make changes
49+
to your source database, changes will be copied over to your replica incrementally.
50+
51+
52+
### Restoring a backup
53+
54+
Litestream can restore a previous snapshot and replay all replicated WAL files.
55+
By default, it will restore up to the latest WAL file but you can also perform
56+
point-in-time restores.
57+
58+
A database can only be restored to a path that does not exist so you don't need
59+
to worry about accidentally overwriting your current database.
60+
61+
```sh
62+
# Restore database to original path.
63+
$ litestream restore /path/to/db
64+
65+
# Restore database to a new location.
66+
$ litestream restore -o /tmp/mynewdb /path/to/db
67+
68+
# Restore database to a specific point-in-time.
69+
$ litestream restore -timestamp 2020-01-01T00:00:00Z /path/to/db
70+
```
71+
72+
Point-in-time restores only have the resolution of the timestamp of the WAL file
73+
itself. By default, litestream will start a new WAL file every minute so
74+
point-in-time restores are only accurate to the minute.
75+
76+
77+
### Validating a backup
78+
79+
Litestream can perform a consistency check of backups. It does this by computing
80+
a checksum of the current database file and then computing a checksum of
81+
a restored a backup. Litestream performs physical replication so backed up
82+
databases should be the same byte-for-byte. Be aware that this can incur some
83+
cost if you are validating from an external replica such as S3.
84+
85+
```sh
86+
$ litestream validate /path/to/db
87+
```
88+
89+
Note that computing the checksum of your original database does obtain a read
90+
lock to prevent checkpointing but this will not affect your read/write access
91+
to the database.
92+
93+
94+
## How it works
95+
96+
SQLite provides a WAL (write-ahead log) journaling mode which writes pages to
97+
a `-wal` file before eventually being copied over to the original database file.
98+
This copying process is known as checkpointing. The WAL file works as a circular
99+
buffer so when the WAL reaches a certain size then it restarts from the beginning.
100+
101+
Litestream works by taking over the checkpointing process and controlling when
102+
it is restarted to ensure that it copies every new page. Checkpointing is only
103+
allowed when there are no read transactions so Litestream maintains a
104+
long-running read transaction against each database until it is ready to
105+
checkpoint.
106+
107+
The SQLite WAL file is copied to a separate location called the shadow WAL which
108+
ensures that it will not be overwritten by SQLite. This shadow WAL acts as a
109+
temporary buffer so that replicas can replicate to their destination (e.g.
110+
another file path or to S3). The shadow WAL files are removed once they have
111+
been fully replicated.
112+
113+
Litestream groups a snapshot and all subsequent WAL changes into "generations".
114+
A generation is started on initial replication of a database and a new
115+
generation will be started if litestream detects that the WAL replication is
116+
no longer contiguous. This can occur if the `litestream` process is stopped and
117+
another process is allowed to checkpoint the WAL.
118+
119+
120+
121+
## Open-Source, not Open-Contribution
122+
123+
[Similar to SQLite](https://www.sqlite.org/copyright.html), litestream is open
124+
source but closed to contributions. This keeps the code base free of proprietary
125+
or licensed code but it also helps me continue to maintain and build litestream.
126+
127+
As the author of [BoltDB](https://github.com/boltdb/bolt), I found that
128+
accepting and maintaining third party patches contributed to my burn out and
129+
eventual archival of the project. Writing databases & low-level replication
130+
tools involves nuance and simple one line changes can have profound and
131+
unexpected changes in correctness and performance. Even small contributions
132+
typically required hours of my time to properly test and validate them.
133+
134+
I am grateful for community involvement and when folks report bugs or suggest
135+
features. I do not wish to come off as anything but welcoming, however, I've
136+
made the decision to keep this project closed to contribution for my own
137+
mental health and long term viability of the project.

0 commit comments

Comments
 (0)