SlimCluster has the Raft distributed consensus algorithm implemented in .NET. Additionally, it implements the SWIM cluster membership list (where nodes join and leave/die).
- Membership list is required to maintain what micro-service instances (nodes) constitute a cluster.
- Raft consensus helps propagate state across the micro-service instances and ensures there is a designated leader instance performing the coordination of work.
The library goal is to provide a common groundwork for coordination and consensus of your distributed micro-service instances. With that, the developer can focus on the business problem at hand. The library promises to have a friendly API and pluggable architecture.
The strategic aim for SlimCluster is to implement other algorithms to make distributed .NET micro-services easier and not require one to pull in a load of other 3rd party libraries or products.
This a relatively new project!
The path to a stable production release:
- ✅ Step 1: Implement the SWIM membership over UDP + sample.
- ✅ Step 2: Documentation on Raft consensus.
- ✅ Step 3: Implement the Raft over TCP/UDP + sample.
- ⬜ Step 4: Documentation on SWIM membership.
- ⬜ Step 5: Other extensions and plugins.
Check out the Samples folder on how to get started.
Setup membership discovery using the SWIM algorithm and consensus using Raft algorithm:
@:cs
Then somewhere in the micro-service, the ICluster
can be used:
// Injected, this will be a singleton representing the cluster the service instances form.
ICluster cluster;
// Gives the current leader
INode? leader = cluster.LeaderNode;
// Gives the node representing current node
INode self = cluster.SelfNode;
// Provides a snapshot collection of the current nodes discovered and alive/healthy forming the cluster
IEnumerable<INode> nodes = cluster.Nodes;
// Provides a snapshot collection of the current nodes discovered and alive/healthy forming the cluster excluding self
IEnumerable<INode> otherNodes = cluster.OtherNodes;
The IClusterMembership
can be used to understand membership changes:
@:cs
- The service references SlimCluser NuGet packages and configures MSDI.
- Nodes (service instances) are communicating over UDP/IP and exchange protocol messages (SWIM and Raft).
- Cluster membership (nodes that form the cluster) is managed (SWIM).
- Cluster leader is elected at the beginning and in the event of failure (Raft).
- Logs (commands that chage state machine state) are replicated from leader to followers (Raft).
- State Machine in each Node gets logs (commands) applied which have been replicated to majority of nodes (Raft).
- Clients interact with the Cluster (state mutating operations are executed to Leader or Followers for reads) - depends on the use case.
cd src
dotnet build
dotnet pack --output ../dist
NuGet packaged end up in dist
folder
To run tests you need to update the respective appsettings.json
to match your cloud infrastructure or local infrastructure.
Run all tests:
dotnet test
Run all tests except integration tests which require local/cloud infrastructure:
dotnet test --filter Category!=Integration