Skip to main content

A distributed key-value store in Python using Raft consensus

Project description

Python Key-Value Store with Raft Consensus

A distributed key-value store in Python that uses the Raft consensus algorithm implemented in Go via an HTTP bridge.

Performance

Throughput: ~1000 ops/sec

Run benchmarks:

python tests/benchmark.py -n 100
python tests/benchmark_async.py -n 500 -t 20

Installation

Prerequisites

  • Python 3.8+ - Download
  • Go 1.23+ - Download (required to build the Raft bridge)
  • pip - Usually comes with Python

Install from PyPI

pip install python-raft-kv

Install from Source

# 1. Clone the repository
git clone https://github.com/atharvagasheTAMU/python-raft-kv.git
cd python-raft-kv

# 2. Install the package
pip install -e .

# Or install as a regular package
pip install .

This will:

  • Install the python_kv Python package
  • Install dependencies (requests, psutil)
  • Make the raft-kv-start command available

Note: The Go bridge will be automatically built on first use when you start the cluster.

Quick Start

Start the Cluster

# Start the 3-node cluster
raft-kv-start

This will:

  • Build the Go bridge if needed (requires Go to be installed)
  • Start 3 Raft nodes on ports 8080, 8081, 8082
  • Connect them together
  • Wait for leader election

Usage

from python_kv import KVStore

# Connect to the leader (check which port is leader after starting)
kv = KVStore("http://localhost:8080", server_id=0)

# Put a value
prev_value, was_found = kv.put("hello", "world")

# Get a value
value, found = kv.get("hello")
print(value)  # "world"

# Compare-and-swap
old_value, was_found = kv.cas("key", "old", "new")

# Check if leader
is_leader = kv.is_leader()

HTTP API

The bridge exposes HTTP endpoints on ports 8080, 8081, 8082 (for nodes 0, 1, 2).

Check if leader

GET http://localhost:8080/is_leader
# Response: {"is_leader": true}

Submit a command (PUT)

POST http://localhost:8080/submit
Content-Type: application/json

{
  "kind": "put",
  "key": "hello",
  "value": "world"
}

# Response: {"log_index": 0, "is_leader": true}

Submit a command (GET)

POST http://localhost:8080/submit
Content-Type: application/json

{
  "kind": "get",
  "key": "hello"
}

# Response: {"log_index": 1, "is_leader": true}

Submit a command (CAS)

POST http://localhost:8080/submit
Content-Type: application/json

{
  "kind": "cas",
  "key": "hello",
  "compare_value": "world",
  "value": "newvalue"
}

# Response: {"log_index": 2, "is_leader": true}

Wait for commit

POST http://localhost:8080/wait_commit
Content-Type: application/json

{
  "log_index": 0,
  "timeout_ms": 30000
}

# Response: {
#   "index": 0,
#   "term": 1,
#   "command": {"kind": "put", "key": "hello", "value": "world", "id": 0}
# }

Get commit by index

GET http://localhost:8080/get_commit?index=0

# Response: {
#   "index": 0,
#   "term": 1,
#   "command": {"kind": "put", "key": "hello", "value": "world", "id": 0}
# }

Get commits since index

GET http://localhost:8080/get_commits_since?since_index=-1

# Response: {
#   "commits": [...],
#   "count": 5
# }

Testing

# Basic functionality
python tests/test_setup.py
python tests/test_3node.py

# Fault tolerance
python tests/test_fault_tolerance.py
python tests/test_data_persistence.py

# Performance benchmarks
python tests/benchmark.py -n 100
python tests/benchmark_async.py -n 500 -t 20

Architecture

Python KV Store ←→ HTTP API ←→ Go Raft Bridge ←→ Raft Implementation

The project includes:

  • python_kv/ - Python key-value store client
  • raft-bridge/ - Go HTTP bridge service
  • raft/ - Raft consensus implementation (from eliben/raft)

Stopping the Cluster

Windows:

Get-Process -Name raft-bridge | Stop-Process

Linux/Mac:

pkill -f raft-bridge

Or simply close the node windows.

Credits

This project uses the Raft implementation from eliben/raft by Eli Bendersky.

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

python_raft_kv-0.1.1.tar.gz (32.7 kB view details)

Uploaded Source

Built Distribution

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

python_raft_kv-0.1.1-py3-none-any.whl (26.2 kB view details)

Uploaded Python 3

File details

Details for the file python_raft_kv-0.1.1.tar.gz.

File metadata

  • Download URL: python_raft_kv-0.1.1.tar.gz
  • Upload date:
  • Size: 32.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for python_raft_kv-0.1.1.tar.gz
Algorithm Hash digest
SHA256 25ec20fede30730f5411ff71f0ff14ab6cfd496205a8936b18b38a1a70f3f7bd
MD5 af0331f6137242e2d871a7683b6f9db4
BLAKE2b-256 ad9b2711e53eea0ec874bb3f5b7132154269a6ad52f40fb8eeb0224c140c28f4

See more details on using hashes here.

File details

Details for the file python_raft_kv-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: python_raft_kv-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 26.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for python_raft_kv-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0b993e0dea9f3b77d27265a7a4532bfc8fb66e2ebe3a9193b54e3f9910ff461c
MD5 6e5c29fe58adf2b8062239b0b6d3c4f0
BLAKE2b-256 cf6f41e96e0187be71f239283e05d972446cc3618dbde9748c7bd4c554da72fd

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