Git Product home page Git Product logo

raft's Introduction

Raft

Go Report Card GoDoc

Raft is a well known consensus algorithm that is used in distributed systems where fault-tolerance and data consensus is important. This is a working implementation of that algorithm that enables clients to inject their own systems alongside the raft. Whether it be a key-value store or something more complicated.

At the center of the development of this project was the raft paper which thoroughly describes the intricacies of the algorithm, and it's many sub-problems. Spending time researching and getting a solid understanding of how each problem should be solved prior to coding the solution.

NOTE: This implementation is purely for educational purposes so that I could continue learning more about distributed systems. This is not intended for production use. If raft is required for production, then it's encouraged that Hashicorp's library is used instead.

How It's Used?

This raft implementation is designed with flexibility in mind. Providing a platform to inject your own finite state machine and your own persistence storage.

In the example use-case, we create a Key-Value store that implements the FSM interface. Now whenever the raft node needs to apply a task to the FSM, it will apply those tasks through the provided API.

// kvStore type implements all the methods required for the FSM.
type kvStore struct {
	r    *raft.Raft
	data map[string]string
}

The client also needs to create their own implementation the LogStore and StableStore. In the example use-case we used the provided In-Memory Store; however, it's important to note that in-memory solutions are NOT meant for actual production use.

memStore := raft.NewMemStore()
// add some data to that memory store if needed

All raft nodes are part of a cluster. A cluster is a group of nodes and their associated addresses. A cluster is initialized by using a configuration file. Configuration files are json formatted with a similar format of, "id": {id: int, addr: string}

{
  "1": {
    "id": 1,
    "addr": ":6001"
  }
}

Open the config file and use it to initialize a cluster.

f, err := os.Open("config.json")
if err != nil {
    log.Fatalln(err)
}
c, err := raft.NewClusterWithConfig(f)
if err != nil {
    log.Fatalln(err)
}

Once all of the dependencies are initialized, we can now create the raft node and start serving it on the cluster.

r, err := raft.New(raftID, cluster, option, FSM, memStore, memStore)
if err != nil {
	log.Fatalf(err)
}
go func() {
    if err := r.ListenAndServe(":6001"); err != nil {
    log.Println(err)
    }
}()

// regular operation of the application as the raft node runs in a different goroutine.

In Action (Demo)

At initial startup, all raft nodes will start their servers using a defined configuration file. In this example, there are three servers with IDs of 1-3. This startup demo shows how all three servers are capable of choosing a leader though an election.

raft-startup

A major part of the raft consensus algorithm is the ability for the majority of nodes to safely persist the same data on their finite state machines. This demo shows how the leader (when given a request), replicates the log & state across all other nodes.

raft-data-populate

Features

List of features that have been developed.

  • Raft cluster leader elections.
  • Log committing & replication.
  • Fault tolerance of N/2 failures. (With N being total # of nodes.)
  • Extendable log/stable store interface that lets the client define how they want the data to be persistently stored. (An In-Memory implementation is provided for testing.)
  • Log snapshots which compact a given number of logs into one log entry. Enabling state to be saved while also limiting the length of the log entries.

Connect & Contact

Email - [email protected]

Website - https://mathewestafanous.com

GitHub - https://github.com/Mathew-Estafanous

raft's People

Contributors

mathew-estafanous avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.