LiteFS Documentation

repository·main·Indexed 26 days ago

https://github.com/superfly/litefs

LiteFS is an asynchronous replication system for SQLite designed for ephemeral deployments. It utilizes a FUSE-based file system to intercept transactions and replicate them via an HTTP API to replica nodes. The system includes a CLI for importing and exporting databases, a YAML-based configuration for managing FUSE mounts, leader election via Consul or static modes, and a frame-based protocol for streaming data.

Tokens
13.4K
Snippets
24
Records
108
Agent score
89%

What's inside LiteFS

  1. Understand LiteFS Architecture components

    main

    LiteFS is composed of three primary components that enable asynchronous replication for SQLite databases:

    1. FUSE file system: Intercepts file system calls to record transactions. On the primary node, it intercepts write transactions transparently. On replica nodes, it prevents writes and provides information about the current primary via a .primary file.
    2. Leader election: Uses Consul sessions and a time-based lease system to ensure only one node acts as the primary at any given time. This is designed for ephemeral environments like Fly.io or Kubernetes.
    3. HTTP server: An API used by replica nodes to communicate with the primary node. Replicas provide their current transaction ID and a rolling checksum to request missing transaction data.
  2. Understand LiteFS consistency and split-brain handling

    main

    LiteFS provides asynchronous replication, which means there is a sub-second window where transactions are only durable on the primary node. A catastrophic crash of the primary during this window may result in data loss.

    Split-Brain Protection

    To prevent database corruption during a network partition (split-brain) where an old primary reconnects to a new leader:

    • LiteFS uses a rolling checksum representing the entire database state at every transaction.
    • If an old primary reconnects and detects that its checksum differs from the new leader (even if the TXIDs match), it will automatically resnapshot the database from the new primary to ensure consistency.
  3. Replica communication and synchronization via HTTP

    main

    Replica nodes synchronize with the primary node using an HTTP-based protocol:

    1. Connection: The replica connects to the primary and specifies its current replication position using its transaction ID and a rolling checksum of its entire database.
    2. Incremental Sync: The primary sends transaction data starting from the replica's reported position.
    3. Snapshot Fallback: If the primary no longer has the requested transaction data available, it will send a full snapshot of the current database to the replica, and replication will resume from that point.
  4. Configure LiteFS via YAML

    main

    LiteFS is configured using a YAML file (typically litefs.yml). The configuration supports environment variable expansion, including boolean expressions for conditional logic.

    LiteFS searches for the configuration file in the following order:

    1. The current working directory (./litefs.yml)
    2. The current user's home directory (~/litefs.yml)
    3. The system configuration directory (/etc/litefs.yml)

    You can also specify an explicit path to a configuration file when running the LiteFS process.

  5. Use the litefs mount command

    main

    The litefs mount command mounts a LiteFS directory via FUSE and begins communicating with the LiteFS cluster. The mount becomes accessible once the node becomes the primary or connects and syncs with the primary.

    LiteFS searches for a litefs.yml configuration file in the following order:

    1. The present working directory
    2. The current user's home directory
    3. /etc/litefs.yml

    You can also specify an exec command (the application you want to run) by using a double dash -- to separate LiteFS arguments from the command arguments.

  6. Initialize a LiteFS HTTP Client

    main
    To interact with a LiteFS HTTP server (for operations like Promote, Handoff, Import, or Export), use NewClient(). This returns a *Client configured with an http2.Transport that supports h2c (HTTP/2 over cleartext).
  7. Use Environment Variables in LiteFS Config

    main

    LiteFS supports environment variable expansion within the YAML configuration file. It supports standard expansion as well as equality (==) and inequality (!=) expressions for conditional logic.

    Supported formats:

    • ${VAR}: Standard expansion.
    • ${VAR} == 'value': Returns true if VAR equals 'value'.
    • ${VAR} != 'value': Returns true if VAR does not equal 'value'.
    • ${VAR1} == ${VAR2}: Returns true if both variables are equal.
  8. Initialize and run the LiteFS HTTP Server

    main

    The Server struct provides an HTTP API for LiteFS node communication, including replication streaming, database management, and cluster coordination.

    To use the server:

    1. Create a new server instance using NewServer with a *litefs.Store and an address string.
    2. Call Listen() to start the TCP listener.
    3. Call Serve() to begin handling requests.
    4. Call Close() to gracefully shut down the server and its listener.

    You can configure SnapshotTimeout on the Server instance to prevent slow snapshot downloads from backing up the primary node.

  9. Configure the LiteFS FUSE section

    main

    The fuse section configures the FUSE file system layer used to intercept SQLite transactions.

    Key options:

    • dir: (Required) The mount directory where applications access their SQLite databases.
    • allow-other: If true, allows non-root users to access the mount. Note: You must first enable user_allow_other in /etc/fuse.conf on the host system.
    • debug: Enables debug logging for all FUSE API calls. This produces high log volume and is not recommended for production.
    fuse:
      dir: "/litefs"
      allow-other: false
      debug: false
  10. Configure LiteFS Store parameters

    main

    The Store struct provides several configuration fields that can be set before calling Open(). Key parameters include:

    FieldTypeDefaultDescription
    ReconnectDelaytime.Duration1sTime to wait after disconnecting from primary before retrying.
    DemoteDelaytime.Duration10sTime to wait after manual Demote() before attempting to become primary again.
    Retentiontime.Duration10mLength of time to retain LTX files.
    RetentionMonitorIntervaltime.Duration1mInterval between checks for LTX retention.
    HaltAcquireTimeouttime.Duration10sMax time to hold/acquire HALT lock.
    BackupDelaytime.Duration1sDelay after a change before it is sent to the backup service (for batching).
    BackupFullSyncIntervaltime.Duration10sInterval to re-fetch the position map from the backup server.
    CompressboolfalseIf true, LTX files are compressed using LZ4.
    DatabaseFilter[]stringnilSpecifies a subset of databases to replicate from the primary.
    StrictVerifyboolfalseIf true, computes/verifies checksum of the entire DB after every transaction (testing only).
  11. Configure SnapshotTimeout for the LiteFS Server

    main
    The SnapshotTimeout field on the Server struct defines the maximum time allowed to write a single LTX snapshot in a stream. This prevents slow snapshot downloads from backing up the primary node. If not set, it defaults to the store.Retention value.