Raft Consensus Algorithm
    Preparing search index...

    Raft Consensus Algorithm

    raft-consensus-algorithm

    A TypeScript implementation of the Raft consensus algorithm, structured as a monorepo.

    Raft is a protocol for building fault-tolerant distributed systems. A cluster of nodes elects a leader, replicates a log of commands across all nodes, and applies those commands to a state machine — even when some nodes crash or become unreachable. This implementation covers leader election, log replication, snapshots, pre-vote, and joint-consensus cluster membership changes.

    Package Description
    @maboke123/raft-core The core library — transport and storage agnostic
    @maboke123/raft-grpc gRPC transport adapter for real network deployments
    App Description
    example-cluster A runnable 3-node cluster with a WebSocket server for the devtools UI
    devtools A React visualizer that shows live cluster state, elections, and message flow
    packages/
    raft-core/ the npm libraryzero production dependencies
    raft-grpc/ gRPC transport adapter
    apps/
    example-cluster/ runnable demo server (depends on raft-core + raft-grpc)
    devtools/ React devtools UI (connects to example-cluster via WebSocket)
    scripts/
    GenerateCerts.sh generates self-signed TLS certificates for local testing
    • Node.js 18+
    • npm 8+ (workspaces support)
    npm install
    

    This installs dependencies for all packages and apps in one step.

    npm run build
    

    Or build a single package:

    cd packages/raft-core
    npm run build
    npm run test
    

    Or test a single package:

    cd packages/raft-core
    npm test

    The example cluster starts three Raft nodes and a WebSocket server that streams events to the devtools UI.

    cd apps/example-cluster
    npm run dev

    Then in a separate terminal, start the devtools UI:

    cd apps/devtools
    npm run dev

    Open http://localhost:5173 to see the cluster visualizer.

    For running the gRPC transport with mutual TLS locally:

    bash scripts/GenerateCerts.sh
    

    This generates a CA and node certificates under certs/. Do not commit the generated .key files.

    Install from npm — you do not need to clone this repository to use the library:

    npm install @maboke123/raft-core

    # Optional: gRPC transport for real network deployments
    npm install @maboke123/raft-grpc

    See the raft-core README for the full usage guide.

    • Pre-vote — nodes run a pre-vote phase before starting a real election, preventing disruptions from nodes rejoining after a partition
    • Joint consensus — cluster membership changes use the joint consensus approach from the Raft paper, allowing safe addition and removal of nodes without downtime
    • Snapshots — the leader periodically takes snapshots of the application state machine and sends them to lagging followers via InstallSnapshot RPC
    • Pluggable storage — storage is split into four focused interfaces (MetaStorage, LogStorage, SnapshotStorage, ConfigStorage) so you can implement exactly the backend you need
    • Pluggable transport — the Transport interface is minimal so you can bring any communication layer

    MIT