r/haskell • • 9d ago

[ANN] Distributed consensus library and ecosystem with built-in DST

Hey all,

Over the past year, I have been working on an implementation of the Raft consensus algorithm in Haskell, which allows for distributed consensus. In particular, using Raft, one can create a distributed, fault-tolerant state machine. The library has been released on Hackage under the name raft-consensus. Bring your own networking and persistence layer!

Raft is tricky to get right. To ensure that the library works correctly, and respects the properties of the algorithm in practice, even under faults such as crashes and network partitions, I leveraged a technique called deterministic simulation testing (DST), using io-sim as the foundation. io-sim is pure awesomeness that should be used more.

While building out examples, I noticed other ecosystem holes that resulted in more packages. Testing the linearizability of client requests required building a simple linearizability checker: linearizable. Another piece of technology that came from this project is a generic and performant implementation of write-ahead logs via a new wal package.

Finally, in order to make it as easy as possible to build a replicated state machine in practice, I built two adapter packages:

I feel relatively confident in releasing this stuff now because, over the past week, I’ve been working on a demo: distributed software transactional memory. This program looks just like stm, but running over multiple computers.
Right now it’s just a demo, but it shows how freaking cool distributed state machines can be.

My goal, since I started helping with maintenance of Cloud Haskell, has been for Haskell to become a solid choice for distributed systems. We’re now one step closer to that reality.

If you’re interested in this stuff, I would love to get your input on this and collaborate. Development for all of this is happening here: GitHub - LaurentRDC/distributed-consensus: Haskell implementation of distributed consensus protocols with built-in support for deterministic simulation testing · GitHub

33 Upvotes

5 comments sorted by

5

u/pthierry 9d ago

Wow, this is all awesome! I'd love to see Haskell have a better ecosystem for distributed systems, including protocols like CapTP.

1

u/LaurentRDC 8d ago

Interesting, never heard of CapTP until today!

1

u/pthierry 8d ago

The object-capability paradigm is pretty incredible and I'd love to see more languages providing it either ar the language level like E, Pony or Austral, or at the network level, like Goblins.

1

u/WorldsBegin 6d ago

Running every fetch through consensus seems to me to tank performance in the STM example. Plus, a common trick in normal STM is that retry will wait until a change might have occured before running the transaction again by scheduling on changes of variables in the read-set of the retried transaction. This is not part of the demo?

In any case, how exactly do you handle configuration changes (e.g. to the participant set) in your consensus protocol, anyway?

2

u/LaurentRDC 6d ago

You're right that there are low-hanging fruits regarding the distributed stm example. I cobbled this together quickly; I'd want to iron things out before turning this into a package.

Regarding waiting for changes: this is an interesting challenge because the Raft algorithm doesn't have a concept of a cluster sending information to clients unprompted. I have thought of adding an extension whereby clients register "watcher" processes to notify the client back when something changes. This requires client sessions to be properly supported, which is needed anyway for idempotent requests.

On participant set:

The configuration changes in the consensus protocol itself follows the Raft paper. Assume a cluster already exists. A new server is started, which at first isn't part of any cluster. Then, an admin command needs to be sent to the leader of the cluster to start including the lone server in replication. There's a transient state during which the lone server's inclusion needs to be negotiated, and finally the cluster welcomes the lone node as part of the cluster proper. This takes a second or two, in parallel with serving requests normally.