tinyraft
v1.0.1
Published
Tiny & abstract Raft leader election algorithm
Downloads
71
Readme
TinyRaft
Raft leader election isolated from the rest of the algorithm.
TinyRaft doesn't know anything about replication and doesn't require you to implement it.
Actual network communication is also abstracted away and hidden behind a simple callback interface.
The only task of TinyRaft is to elect the leader and guarantee that there is only one leader at each moment.
TinyRaft can be used:
- As a simple leader election algorithm without replication at all
- As a building block for the standard Raft algorithm if you add log replication
- For other variations of "weaker consensus" if you add another method of replication
Some replication ideas for you:
- Log-less replication: Add a version number for each key in the database and make the leader synchronize follower databases by simply dumping all followers' databases with the newest term (followers with older terms should be ignored), comparing version numbers and making the newest version of each key win.
- Erasure coding: Suppose you store large values. You can split each value into N parts, add K parity parts to it using Reed-Solomon codes (ISA-L/jerasure) and store them on different nodes in the form of Raft-like logs or similar to the log-less replication with version numbers, and make master synchronize followers by reconstructing every original value.
Example Application
Usage
const node = new TinyRaft({
nodes: [ 1, 2, 3 ],
nodeId: 1,
electionTimeout: 5000,
heartbeatTimeout: 1000,
leadershipTimeout: 10000,
initialTerm: 0,
leaderPriority: 0,
send: function(to, msg)
{
// Function to send message <msg> to node with ID <to>
// msg.type is one of TinyRaft.VOTE_REQUEST, TinyRaft.VOTE, TinyRaft.PING, TinyRaft.PONG
// msg.leader is the leader ID or null
// msg.term is the term number
// msg.priority is the optional leadership priority if set in config
},
});
// Watch for election state
node.on('change', (st) =>
{
console.log(
'node '+node.nodeId+': '+(st.state == TinyRaft.FOLLOWER ? 'following '+st.leader : st.state)+
', term '+st.term+(st.state == TinyRaft.LEADER ? ', followers: '+st.followers.join(', ') : '')
);
});
// Start Raft node or start a new election round
node.start();
// Optional; may be called for a follower when it receives a message from a live leader,
// for example, a replication message, and causes the follower to move its round expiration forward
node.markAlive();
// Update cluster node list
node.setNodes([ 1, 2, 3 ]);
// Incoming messages should be fed to TinyRaft like this (from = ID of the sender):
node.onReceive(from, msg);
// Stop Raft node
node.stop();
Additional features
Leader expiration
Supports leader expiration like in NuRaft: https://github.com/eBay/NuRaft/blob/master/docs/leadership_expiration.md
When leader expiration is enabled, followers respond to leader heartbeats (pings) with "pong" messages and if the leader doesn't receive a quorum of replies in leadershipTimeout - it starts a new round of voting.
Leadership priorities
Also supports leader priorities, similar to NuRaft but even simpler: if a node receives a VoteRequest message with larger term but with smaller priority than its own, it immediately starts a new voting round.
It guarantees that a node with non-maximum priority can't become leader without being re-elected.
If all priorities are equal (or just zero), the election algorithm becomes identical to the basic algorithm without priorities.
Author and License
Author: Vitaliy Filippov, 2024
License: Mozilla Public License 2.0 or Vitastor Network Public License 1.1