litestream

package module
v0.0.0-...-836cc10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2025 License: MIT Imports: 14 Imported by: 0

README

Litestream lightweight read-replicas

This package implements the EXPERIMENTAL "litestream" SQLite VFS that offers Litestream lightweight read-replicas.

See the example for how to use.

To improve performance, increase PollInterval (and MinLevel) as much as you can, and set PRAGMA cache_size=N (or use _pragma=cache_size(N)).

Documentation

Overview

Package litestream implements a Litestream lightweight read-replica VFS.

Index

Examples

Constants

View Source
const DefaultPollInterval = 1 * time.Second

The default poll interval.

Variables

This section is empty.

Functions

func NewReplica

func NewReplica(name string, client litestream.ReplicaClient, options ReplicaOptions)

NewReplica creates a read-replica from a Litestream client.

Example
package main

import (
	"log"
	"time"

	"github.com/benbjohnson/litestream/s3"
	"github.com/ncruces/go-sqlite3/driver"
	"github.com/ncruces/go-sqlite3/litestream"

	_ "github.com/ncruces/go-sqlite3/embed"
)

func main() {
	client := s3.NewReplicaClient()
	client.Bucket = "test-bucket"
	client.Path = "fruits.db"

	litestream.NewReplica("fruits.db", client, litestream.ReplicaOptions{
		PollInterval: 5 * time.Second,
	})

	db, err := driver.Open("file:fruits.db?vfs=litestream")
	if err != nil {
		log.Fatalln(err)
	}
	defer db.Close()

	for {
		time.Sleep(time.Second)
		rows, err := db.Query("SELECT * FROM fruits")
		if err != nil {
			log.Fatalln(err)
		}

		for rows.Next() {
			var name, color string
			err := rows.Scan(&name, &color)
			if err != nil {
				log.Fatalln(err)
			}
			log.Println(name, color)
		}

		log.Println("===")
		rows.Close()
	}
}

func RemoveReplica

func RemoveReplica(name string)

RemoveReplica removes a replica by name.

Types

type ReplicaOptions

type ReplicaOptions struct {
	// Where to log error messages. May be nil.
	Logger *slog.Logger
	// Minimum compaction level to track.
	MinLevel int
	// Replica poll interval. Must be less than the compaction interval
	// used by the replica at MinLevel+1.
	PollInterval time.Duration
}

ReplicaOptions represents options for NewReplica.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL