npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

AntiEtcd

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