benbjohnson / litestream-library-example

Example repository for embedding Litestream in a Go application.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Litestream as Library

This repository is an example of embedding Litestream as a library in a Go application. The Litestream API is not stable so you may need to update your code in the future when you upgrade.

Install

To install, run:

go install .

You should now have a litestream-library-example in $GOPATH/bin.

Usage

This example application uses AWS S3 and only provides a -bucket configuration flag. It will pull AWS credentials from environment variables so you will need to set those:

export AWS_ACCESS_KEY_ID=xxxxxxxxxxxxxxxxxxxx
export AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

You'll need to setup an S3 bucket and use that name when running the app.

litestream-library-example -dsn /path/to/db -bucket YOURBUCKETNAME

On your first run, it will see that there is no snapshot available so the application will create a new database. If you restart the application then it will see the local database and use that.

If you remove the database:

rm /path/to/db* /path/to/.db-litestream

Then when you restart the application, it will fetch the latest snapshot and replay all WAL files up to the latest position.

Synchronous replication

This repository provides an example of confirming that the replica syncs to S3 before returning to the caller. Replicating to S3 can be slow so you may end up waiting several hundred milliseconds before the sync returns.

About

Example repository for embedding Litestream in a Go application.

License:Apache License 2.0


Languages

Language:Go 100.0%