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

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>/.
  • The WAL is a simple append-only tnetstring log. It models durable-before-ack, but it is not TigerBeetle's 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/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.

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.1.0.tar.gz (28.7 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.1.0-py3-none-any.whl (35.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: vsr_trio-0.1.0.tar.gz
  • Upload date:
  • Size: 28.7 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.1.0.tar.gz
Algorithm Hash digest
SHA256 405370501fcecf76025665db6be39a5809683f649be2de33d1ae22c7844e0a5d
MD5 171adfbe40c3fd3fc8eb9cd96d055636
BLAKE2b-256 c1b80f557c42319879eab60a68c2fdda287d2e2c0deae2c86acbc1ab8e618ca4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: vsr_trio-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 35.8 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a393fca982897500e823a95113bda2b710ac8bddde67526ad727244e8eb363f4
MD5 a4c660d210f364dc936be4568c4500b5
BLAKE2b-256 980c1f0ae5649752e8cba8e461178645defdc258cabcb006378112a742ad4771

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