Ra: a Raft Implementation for Erlang and Elixir

Build Status

What is This

Ra is a Raft implementation by Team RabbitMQ. It is not tied to RabbitMQ and can be used in any Erlang or Elixir project. It is, however, heavily inspired by and geared towards RabbitMQ needs.

Ra (by virtue of being a Raft implementation) is a library that allows users to implement persistent, fault-tolerant and replicated state machines.

Project Maturity

This library is maturing and is currently in a pre-1.0 phase. This means that the primary APIs (ra, ra_machine modules) and on disk formats are unlikely to change significantly until 1.0 is tagged but may need to be if deemed necessary.

Status

The following Raft features are implemented:

Supported Erlang/OTP Versions

Ra requires Erlang/OTP 20.3 or later.

Quick start

%% All servers in a Ra cluster are named processes.
%% Create some Server Ids to pass to the configuration
ErlangNodes = [ra@node1, ra@node2, ra@node3]
ServerIds = [{quick_start, N} || N <- ErlangNodes]

%% start a simple distributed addition state machine with an initial state of 0
{ok, ServersStarted, ServersNotStarted} = ra:start_cluster(quick_start, {simple, fun erlang:'+'/2, 0}, ServerIds),

%% Add a number to the state machine
%% Simple state machines always return the full state after each operation
{ok, StateMachineResult, LeaderId} = ra:process_command(hd(ServersStarted), 5),

%% use the leader id from the last command result for the next
{ok, 12, LeaderId1} = ra:process_command(LeaderId, 7),

"Simple" state machines like the above can only take you so far. See Ra state machine tutorial for how to write a state machine by implementing the ra_machine behaviour.

Design Goals

Use Cases

This library is primarily developed as the foundation for replication layer for replicated queues in a future version of RabbitMQ. The design it aims to replace uses a variant of Chain Based Replication which has two major shortcomings:

Documentation

Configuration

A directory name where ra will store it's data.

The maximum size of the WAL (Write Ahead Log). Default: 128Mb.

Indicate whether the wal should compute and validate checksums. Default: true

Example:

[{data_dir, "/tmp/ra-data"},
 {wal_max_size_bytes, 134217728},
 {wal_compute_checksums, true},
 {wal_write_strategy, default},
]

Copyright and License

(c) 2017-2018, Pivotal Software Inc.

Double licensed under the ASL2 and MPL1.1. See LICENSE for details.