Skip to content

anidok/raftkv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RaftKV

RaftKV is a distributed key-value store that uses the Raft consensus algorithm for replication and fault tolerance. It provides a simple HTTP API for key-value operations and supports automatic leader election and cluster membership management.

Features

  • Distributed key-value store with Raft consensus
  • HTTP API for key-value operations
  • Automatic leader election
  • Cluster membership management
  • Fault tolerance and data replication
  • Multiple storage backends:
    • BoltDB (default)
    • SQLite
  • Snapshot support for log compaction

Prerequisites

  • Go 1.24 or later
  • Make (for using the Makefile)
  • SQLite development libraries (if using SQLite storage)

Installing SQLite Dependencies

Ubuntu/Debian:

sudo apt-get install sqlite3 libsqlite3-dev

macOS:

brew install sqlite3

Windows:

SQLite is included with the Go SQLite driver.

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/raftkv.git
cd raftkv
  1. Install dependencies:
go mod download

Building

Build the project using make:

make build

This will create the raftkv binary in the bin directory.

Running the Cluster

The project includes a Makefile with targets to run a 3-node cluster locally. Each node runs on different ports:

  • Node 1: HTTP API on 8080, Raft on 8001
  • Node 2: HTTP API on 8081, Raft on 8002
  • Node 3: HTTP API on 8082, Raft on 8003

Storage Options

You can choose between two storage backends using the STORAGE environment variable:

  1. BoltDB (default):
make run-cluster
# or
STORAGE=bolt make run-cluster
  1. SQLite:
STORAGE=sqlite make run-cluster

The same storage option can be used with individual node commands:

# Run a single node with SQLite
STORAGE=sqlite make run-node1

# Run a single node with BoltDB (default)
make run-node1

Note: The Raft logs and snapshots will always use BoltDB for storage, regardless of the KV store backend chosen.

Option 1: Start Individual Nodes

You can start each node separately in different terminal windows:

  1. Start the first node (leader):
make run-node1
  1. Start the second node:
make run-node2
  1. Start the third node:
make run-node3

Option 2: Start Entire Cluster

Alternatively, you can start all nodes with a single command:

make run-cluster

This will start all three nodes in the background. You can check their status using:

make status

Managing the Cluster

To stop all nodes:

make stop-cluster

To clean up all node data:

make clean-data

API Usage

Key-Value Operations

  1. Set a value:
curl -X PUT -d "value1" http://localhost:8080/v1/keys/key1
  1. Get a value:
curl http://localhost:8080/v1/keys/key1
  1. Delete a key:
curl -X DELETE http://localhost:8080/v1/keys/key1

Cluster Status

Check the status of any node:

curl http://localhost:8080/v1/status

The response will include:

  • leader: The address of the current leader
  • state: The current state of the node (Leader/Follower)

Configuration

Each node is configured using a YAML file in the configs directory. The configuration includes:

  • Node ID
  • Bind address for Raft communication
  • Advertise address for Raft communication
  • Data directory for persistent storage
  • HTTP API address

Example configuration (node1.yaml):

node_id: "node1"
bind_addr: "127.0.0.1:8001"
advertise_addr: "127.0.0.1:8001"
data_dir: "data/node1"
http_addr: "127.0.0.1:8080"

Project Structure

.
├── bin/                    # Compiled binaries
├── cmd/                    # Command-line applications
│   └── raftkv/            # Main application
├── configs/               # Configuration files
├── data/                  # Data directory (created at runtime)
├── pkg/                   # Core packages
│   ├── api/              # HTTP API implementation
│   ├── raft/             # Raft consensus implementation
│   └── storage/          # Storage implementation
│       ├── bolt_store.go # BoltDB storage implementation
│       └── sqlite_store.go # SQLite storage implementation
├── Makefile              # Build and run targets
└── README.md            # This file

Development

Building

make build

Running Tests

make test

Cleaning

make clean

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors