Home

Awesome

Match Engine Raft

This is a folk from example-raft-kv project from openraft. It's a demostration of openraft storage for snapshot/log on disk.

For chinese guide please click here. It's more uptodated.

Example distributed key-value store built upon openraft.

It is an example of how to build a real-world key-value store with openraft. Includes:

Run it

There is a example in bash script and an example in rust:

if you want to compile the application, run:

cargo build

(If you append --release to make it compile in production, but we don't recommend to use this project in production yet.)

What the test script does

To run it, get the binary raft-key-value inside target/debug and run:

./raft-key-value --id 1 --http-addr 127.0.0.1:21001

It will start a node.

To start the following nodes:

./raft-key-value --id 2 --http-addr 127.0.0.1:21002

You can continue replicating the nodes by changing the id and http-addr.

After that, call the first node created:

POST - 127.0.0.1:21001/init

It will define the first node created as the leader.

Then you need to inform to the leader that these nodes are learners:

POST - 127.0.0.1:21001/add-learner '[2, "127.0.0.1:21002"]'
POST - 127.0.0.1:21001/add-learner '[3, "127.0.0.1:21003"]'

Now you need to tell the leader to add all learners as members of the cluster:

POST - 127.0.0.1:21001/change-membership  "[1, 2, 3]"

Write some data in any of the nodes:

POST - 127.0.0.1:21001/write  "{"Set":{"key":"foo","value":"bar"}}"

Read the data from any node:

POST - 127.0.0.1:21002/read  "foo"

You should be able to read that on the another instance even if you did not sync any data!

How it's structured.

The application is separated in 4 modules:

Where is my data?

The data is store inside state machines, each state machine represents a point of data and raft enforces that all nodes have the same data in synchronization. You can have a look of the struct ExampleStateMachine

Cluster management

The raft itself does not store node addresses. But in a real-world application, the implementation of RaftNetwork needs to know the addresses.

Thus, in this example application:

To add a node to a cluster, it includes 3 steps: