An example of using Litestream's live read replication feature.

Litestream Read Replica Example

This repository is an example of how to setup and deploy a multi-node SQLite database using Litestream's live read replication feature. For more information, please see the Read Replication Guide on the Litestream documentation site.

This example provides a Dockerfile and deployment instructions for fly.io, however, any hosting provider can be used.

Deployment

Docker

To build the Docker image, run:

docker build -t litestream-read-replica-example .

Deploy to fly.io

To deploy to fly.io, you'll need to create an application, volumes, and configure your primary region.

# Application names are unique on fly.io so you'll need to pick one.
export APPNAME=...

# Create a new application but don't deploy yet.
fly launch --name "$APPNAME" --region ord --no-deploy

# Create a volume in our primary region first.
fly volumes create --region ord --size 1 data

We'll also add [env] and [mounts] sections to the fly.toml file. This tells our application which region should be the primary and also attaches the persistent data volume to each instance.

[env]
  FLY_PRIMARY_REGION = "ord"

[mounts]
  source="data"
  destination="/data"

Finally, we'll deploy our application:

fly deploy

Once we confirm that our primary is successfully running, we'll deploy our replicas:

# Create volumes for each replica.
fly volumes create --region lhr --size 1 data
fly volumes create --region syd --size 1 data

# Scale the app so we have one instance for each volume.
fly scale count 3

Once you are done with your application, you can tear it down with the following:

fly apps destroy "$APPNAME"

Usage

The included application only has two endpoints:

  1. POST / - creates a new record
  2. GET / - returns a list of all records

First, we'll add some records:

curl -X POST https://${APPNAME}.fly.dev/ -d 'foo'
curl -X POST https://${APPNAME}.fly.dev/ -d 'bar'
curl -X POST https://${APPNAME}.fly.dev/ -d 'baz'

Then we can retrieve a list of those records:

curl https://${APPNAME}.fly.dev/

This should return the following:

Record #1: "foo"
Record #2: "bar"
Record #3: "baz"

Viewing replica data

Fly.io automatically routes requests to the nearest instance so we'll need to log into our instances if we want to verify the replicated data. First, you'll need to set up access to your private network over Wireguard:

https://fly.io/docs/reference/private-networking/#private-network-vpn

Once set up, we can cURL against a specific region's server:

curl lhr.${APPNAME}.internal:8080

You can also SSH into the node and verify with the SQLite CLI. Be sure to use the -readonly flag to ensure you do not accidentally alter the database on the replica.

fly ssh console lhr.${APPNAME}.internal

sqlite3 -readonly /data/db

sqlite> SELECT * FROM records;
1|foo
2|bar
3|baz
Similar Resources

Dgraph Backup and Restore (cloud). Read-only mirror.

dgbrx Dgraph Backup and Restore X dgbrx is a Go commandline tool which helps to do a backup, restore or clean on a Dgraph Cloud (aka slash / managed)

Oct 28, 2021

Repo CRUD - write e read in mongoDB

Repo CRUD - write e read in mongoDB

Meli User - userwrite Este repo é responsável por um cadastro onde teremos somen

Jan 3, 2022

Nydus-snapshotter - A containerd snapshotter with capability of on-demand read

Nydus Snapshotter Nydus-snapshotter is a non-core sub-project of containerd. Pul

Dec 14, 2022

Godart - Amazon Alexa skill in Go to read train times out loud

GODART Alexa skill to have DART times for the requested station. build and deplo

Apr 13, 2022

An example of Kubernetes' Horizontal Pod Autoscaler using costume metrics.

An example of Kubernetes' Horizontal Pod Autoscaler using costume metrics.

Kubernetes Autoscaling Example In this project, I try to implement Horizontal Pod AutoscalerHPA provided by Kubernetes. The Horizontal Pod Autoscaler

Dec 1, 2022

Simple example using Git actions + Argo CD + K8S + Docker and GO lang

CICD-simple_example Simple example using Git actions + Argo CD + K8S + Docker and GO lang Intro Pre reqs Have an ArgoCD account and Installed. Docker

Oct 28, 2021

The example shows how to build a simple multi-tier web application using Kubernetes and Docker

The example shows how to build a simple multi-tier web application using Kubernetes and Docker

Guestbook Example This example shows how to build a simple multi-tier web application using Kubernetes and Docker. The application consists of a web f

Nov 15, 2021

MTLS - Golang mTLS example,mTLS using TLS do both side authentication & authorization

MTLS - Golang mTLS example,mTLS using TLS do both side authentication & authorization

mTLS Golang Example mTLS Golang Example 1. What is mutual TLS (mTLS)? 2. How doe

Dec 21, 2022

MTLS - Golang mTLS example,mTLS using TLS do both side authentication & authorization

MTLS - Golang mTLS example,mTLS using TLS do both side authentication & authorization

mTLS Golang Example mTLS Golang Example 1. What is mutual TLS (mTLS)? 2. How doe

Jan 1, 2022
Kubegres is a Kubernetes operator allowing to create a cluster of PostgreSql instances and manage databases replication, failover and backup.

Kubegres is a Kubernetes operator allowing to deploy a cluster of PostgreSql pods with data replication enabled out-of-the box. It brings simplicity w

Dec 30, 2022
Asynchronous data replication for Kubernetes volumes

VolSync VolSync asynchronously replicates Kubernetes persistent volumes between clusters using either rsync or rclone. It also supports creating backu

Jan 1, 2023
Pulumi-k8s-operator-example - OpenGitOps Compliant Pulumi Kubernetes Operator Example

Pulumi GitOps Example OpenGitOps Compliant Pulumi Kubernetes Operator Example Pr

May 6, 2022
A live-updating version of the UNIX wc command.
A live-updating version of the UNIX wc command.

lwc A live-updating version of the UNIX wc command. Installation You can get a prebuilt binary for every major platform from the Releases page. Just e

Jul 26, 2022
This project will help you to create Live img.shields.io Badges which will Count YouTube Stats (Subscriber, Views, Videos) without YouTube API
This project will help you to create Live img.shields.io Badges which will Count YouTube Stats (Subscriber, Views, Videos) without YouTube API

Free YouTube Stats Badge This project will help you to create Live img.shields.io Badges which will Count YouTube Stats (Subscriber, Views, Videos) wi

Oct 11, 2022
🔎 gowatch Live reload for go apps.

Watch ?? gowatch Live reload for go apps Motivation I had no app to live reload my Go programs. Usage Install go install github.com/gelfand/gowatch ◆

Dec 29, 2021
A Go library for the Linux Landlock sandboxing feature

Go landlock library The Go landlock library provides an interface to Linux 5.13's Landlock kernel sandboxing features. The library provides access to

Dec 27, 2022
🔮 ✈️ to integrate OPA Gatekeeper's new ExternalData feature with cosign to determine whether the images are valid by verifying their signatures

cosign-gatekeeper-provider To integrate OPA Gatekeeper's new ExternalData feature with cosign to determine whether the images are valid by verifying i

Dec 8, 2022
Cli tool to save useful links to read it later
Cli tool to save useful links to read it later

lnk - Cli tool to save useful links still in development Usage lnk <new|list> [url] - new: creates a new link url: required params, which i

Jun 23, 2022
Write controller-runtime based k8s controllers that read/write to git, not k8s

Git Backed Controller The basic idea is to write a k8s controller that runs against git and not k8s apiserver. So the controller is reading and writin

Dec 10, 2021