Skip to main content

A Trio implementation of Viewstamped Replication inspired by TigerBeetle's VSR protocol.

Project description

vsr

A Python Trio implementation of a Viewstamped Replication style replicated state machine, inspired by the VSR protocol shape used by TigerBeetle.

This is an educational implementation. It models protocol structure and now supports multi-process TCP nodes using tnetstring3 as the wire/WAL/config encoding. It is not TigerBeetle's production storage, networking, repair, pipelining, recovery, or verification machinery.

Development

Install dependencies:

uv sync

Build the package locally:

uv build

Install from PyPI:

uv pip install vsr-trio
python -m pip install vsr-trio

Install from a local checkout:

uv pip install .
python -m pip install .

Example applications live outside this package. During local development, use the sibling ../vsr_apps monorepo, where each packages/<name>/ directory is an independent Python package that depends on the vsr-trio distribution and imports the vsr package.

Multi-process cluster

Create a static cluster configuration:

uv run vsr init-cluster \
  --nodes n0=127.0.0.1:9000,n1=127.0.0.1:9001,n2=127.0.0.1:9002 \
  --storage file

Storage backends are pluggable: file keeps the educational framed file storage, sqlite stores the same logical blobs in SQLite, and lmdb stores them in LMDB.

The primary batches client requests into a single Prepare until --max-batch-size is reached or --max-batch-delay expires. It may keep up to --max-pipeline-depth uncommitted prepares in flight, while commits still advance only in operation-number order.

Start each node in a different terminal, directory, VM, or machine that has the same .vsr/cluster.tnet file. App clients use the generated .vsr/client.tnet file, which contains only client request authority. Install an app package, or run from the ../vsr_apps workspace, then pass the app package import name:

uv run vsr \
  --app vsr_banking \
  --app vsr_inventory \
  --app vsr_catalog \
  --app vsr_locks \
  start-node n0
uv run vsr \
  --app vsr_banking \
  --app vsr_inventory \
  --app vsr_catalog \
  --app vsr_locks \
  start-node n1
uv run vsr \
  --app vsr_banking \
  --app vsr_inventory \
  --app vsr_catalog \
  --app vsr_locks \
  start-node n2

Send client commands from the app workspace:

uv run vsr-bank add balance 10
uv run vsr-bank add balance 32
uv run vsr-bank get balance
uv run vsr-inventory stock sku-1 5
uv run vsr-inventory reserve sku-1 3 --client-id cart-1 --request-no 1
uv run vsr-inventory available sku-1

Run explicit maintenance flows:

uv run vsr recover-node n2 --from-node n1 --force
uv run vsr sync-node n2 --force
uv run vsr move-node n2 127.0.0.1:9012

recover-node is a single-source educational local copy and prints a warning. It can copy stale state. Prefer sync-node for the quorum-checked maintenance path.

Use a different cluster directory when needed:

uv run vsr --cluster-dir /tmp/vsr-a init-cluster
uv run vsr --cluster-dir /tmp/vsr-a --app vsr_banking start-node n0

Notes

  • Membership is static. A node "joins" by starting with the same cluster config and its own node name.
  • vsr-trio is the installable distribution. vsr is the import package and CLI command. Example apps are separate packages, developed in the sibling vsr_apps monorepo during local work; repeat --app to compose multiple installed packages in one cluster.
  • Each node creates its own directory under .vsr/nodes/<node-name>/.
  • Storage uses an append-only tnetstring WAL, redundant checksummed superblocks, checksummed metadata, and redundant checksummed checkpoints for committed app state and client replies. It is still not TigerBeetle's real journal/superblock/checkpoint design.
  • Consensus state is owned by a single Trio actor per replica. TCP handlers and outbound workers communicate with it through bounded Trio channels.
  • See docs/vsr/static_membership.md for the node lifecycle command matrix and why dynamic membership is out of scope.
  • See docs/vsr/storage.md for the WAL, metadata, checkpoint, and startup recovery model.
  • See docs/vsr/use_cases.md for banking, inventory reservation, and other replicated state machine scenarios.
  • See docs/vsr/implementation_boundaries.md for the explicit list of TigerBeetle subsystems this educational implementation does not model.
  • See docs/vsr/release.md for the PyPI release checklist.

License

vsr is distributed under the MIT License. See LICENSE.

Check and format

make check

Or run the individual tools:

uv run ty check
uv run ruff check
uv run ruff format
uv run pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vsr_trio-0.3.0.tar.gz (41.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vsr_trio-0.3.0-py3-none-any.whl (49.9 kB view details)

Uploaded Python 3

File details

Details for the file vsr_trio-0.3.0.tar.gz.

File metadata

  • Download URL: vsr_trio-0.3.0.tar.gz
  • Upload date:
  • Size: 41.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for vsr_trio-0.3.0.tar.gz
Algorithm Hash digest
SHA256 3c5182f14baf9fa78bbca0ab3845d0a5c7b31a9d5aa8891ed6903e7c38bc1208
MD5 1b862235520f56f9d519cde291a8791a
BLAKE2b-256 5ac4556e977eadc98f012c2f080e0b1fc850d6a0e4a3119bcf132b7b4fd2d502

See more details on using hashes here.

File details

Details for the file vsr_trio-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: vsr_trio-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 49.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for vsr_trio-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ec17e0eed9793a1de125502204265b159904488c149c1cabd1724b9cd37d7089
MD5 fa15b177e99e4d695e4475ecf6992cad
BLAKE2b-256 69ae525d2a4214a0367b7a15bb2c74869a465b92315395698258a7250aedb430

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page