<div align="center">

<img src="https://raw.githubusercontent.com/al8n/memberlist/main/art/logo.png" height = "200px">

<h1>Memberlist</h1>


</div>
<div align="center">

A highly customable, adaptable, runtime agnostic and WASM/WASI friendly Gossip protocol which helps manage cluster membership and member failure detection.

Port and improve [HashiCorp's memberlist](https://github.com/hashicorp/memberlist) to Rust.

[<img alt="github" src="https://img.shields.io/badge/github-al8n/memberlist-8da0cb?style=for-the-badge&logo=Github" height="22">][Github-url]
<img alt="LoC" src="https://img.shields.io/endpoint?url=https%3A%2F%2Fgist.githubusercontent.com%2Fal8n%2Fd29ceff54c025fe4e8b144a51efb9324%2Fraw%2Fmemberlist" height="22">
[<img alt="Build" src="https://img.shields.io/github/actions/workflow/status/al8n/memberlist/coverage.yml?logo=Github-Actions&style=for-the-badge" height="22">][CI-url]
[<img alt="codecov" src="https://img.shields.io/codecov/c/gh/al8n/memberlist?style=for-the-badge&token=6R3QFWRWHL&logo=codecov" height="22">][codecov-url]

[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-memberlist-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="20">][doc-url]
[<img alt="crates.io" src="https://img.shields.io/crates/v/memberlist?style=for-the-badge&logo=data:image/svg+xml;base64, " height="22">][crates-url]
[<img alt="crates.io" src="https://img.shields.io/crates/d/memberlist?color=critical&logo=data:image/svg+xml;base64, &style=for-the-badge" height="22">][crates-url]
<img alt="license" src="https://img.shields.io/badge/License-MPL%202.0-blue.svg?style=for-the-badge&fontColor=white&logoColor=ffffff&logo=data:image/svg+xml;base64, " height="22">

[<img alt="github" src="https://img.shields.io/discord/835936528140206122?style=for-the-badge&logo=discord&logoColor=white&label=Discord&color=7289da" height="22">][discord]

English | [简体中文][zh-cn-url]

</div>

## Introduction

memberlist is a rust crate that manages cluster membership and member failure detection using a gossip based protocol.

The use cases for such a library are far-reaching: all distributed systems require membership, and memberlist is a re-usable solution to managing cluster membership and node failure detection.

memberlist is eventually consistent but converges quickly on average. The speed at which it converges can be heavily tuned via various knobs on the protocol. Node failures are detected and network partitions are partially tolerated by attempting to communicate to potentially dead nodes through multiple routes.

memberlist is WASM/WASI friendly, all crates can be compiled to `wasm-wasi` and `wasm-unknown-unknown` (need to configure the crate features).

## Installation

- By using `TCP/UDP`, `TLS/UDP` transport

  ```toml
  memberlist = { version = "0.7", features = [
    "tcp",
    # Enable a checksum, as UDP is not reliable.
    # Built in supports are: "crc32", "xxhash64", "xxhash32", "xxhash3", "murmur3"
    "crc32",
    # Enable a compression, this is optional,
    # and possible values are `snappy`, `brotli`, `zstd` and `lz4`.
    # You can enable all.
    "snappy",
    # Enable encryption, this is optional,
    "encryption",
    # Enable a async runtime
    # Builtin supports are `tokio`, `smol`
    "tokio",
    # Enable one tls implementation. This is optional.
    # Users can just use encryption feature with plain TCP.
    #
    # "tls",
  ] }
  ```

- By using `QUIC/QUIC` transport

  For `QUIC/QUIC` transport, as QUIC is secure and reliable, so enable checksum or encryption makes no sense.

  ```toml
  memberlist = { version = "0.7", features = [
    # Enable a compression, this is optional,
    # and possible values are `snappy`, `brotli`, `zstd` and `lz4`.
    # You can enable all.
    "snappy",
    # Enable a async runtime
    # Builtin supports are `tokio`, `smol`
    "tokio",
    # Enable one of the QUIC implementation
    # Builtin support is `quinn`
    "quinn",
  ] }
  ```

## Examples

- [serf](https://github.com/al8n/serf): A decentralized solution for service discovery and orchestration that is lightweight, highly available, and fault tolerant.
- [examples/toydb](https://github.com/al8n/memberlist/tree/main/examples/toydb): a toy eventually consistent distributed key-value database.

## Protocol

memberlist is based on ["SWIM: Scalable Weakly-consistent Infection-style Process Group Membership Protocol"](http://ieeexplore.ieee.org/document/1028914/). However, Hashicorp developers extends the protocol in a number of ways:

Several extensions are made to increase propagation speed and convergence rate.
Another set of extensions, that Hashicorp developers call Lifeguard, are made to make memberlist more robust in the presence of slow message processing (due to factors such as CPU starvation, and network delay or loss).
For details on all of these extensions, please read Hashicorp's paper ["Lifeguard : SWIM-ing with Situational Awareness"](https://arxiv.org/abs/1707.00788), along with the memberlist source.

## Design

Unlike the original Go implementation, Rust's memberlist use highly generic and layered architecture, users can easily implement a component by themselves and plug it to the memberlist. Users can even custom their own `Id` and `Address`.

Here are the layers:

- **Transport Layer**

  By default, Rust's memberlist provides two kinds of transport -- [`QuicTransport`](https://docs.rs/memberlist-quic/struct.QuicTransport.html) and [`NetTransport`](https://docs.rs/memberlist-net/struct.NetTransport.html).

  - **Runtime Layer**

    Async runtime agnostic are provided by [`agnostic`'s Runtime](https://docs.rs/agnostic/trait.Runtime.html) trait, `tokio`, and `smol` are supported by default. Users can implement their own [`Runtime`](https://docs.rs/agnostic/trait.Runtime.html) and plug it into the memberlist.

  - **Address Resolver Layer**

    The address resolver layer is supported by [`nodecraft`'s AddressResolver](https://docs.rs/nodecraft/latest/nodecraft/resolver/trait.AddressResolver.html) trait.

  - **[`NetTransport`](https://docs.rs/memberlist-net/struct.NetTransport.html)**

    Builtin stream layers for `NetTransport`:

    - [`Tcp`](https://docs.rs/memberlist-net/stream_layer/tcp/struct.Tcp.html): based on TCP and UDP
    - [`Tls`](https://docs.rs/memberlist-net/stream_layer/tls/struct.Tls.html): based on [`rustls`](https://docs.rs/rustls) and UDP

  - **[`QuicTransport`](https://docs.rs/memberlist-quic/struct.QuicTransport.html)**

    QUIC transport is an experimental transport implementation, it is well tested but still experimental.

    Builtin stream layers for `QuicTransport`:
    - [`Quinn`](https://docs.rs/memberlist-quic/stream_layer/quinn/struct.Quinn.html): based on [`quinn`](https://docs.rs/quinn)

  Users can still implement their own stream layer for different kinds of transport implementations.

- **Delegate Layer**
  
  This layer is used as a reactor for different kinds of messages.

  - **`Delegate`**
  
    Delegate is the trait that clients must implement if they want to hook into the gossip layer of Memberlist. All the methods must be thread-safe, as they can and generally will be called concurrently.

    Here are the sub delegate traits:

    - **`AliveDelegate`**

      Used to involve a client in processing a node "alive" message. When a node joins, either through a packet gossip or promised push/pull, we update the state of that node via an alive message. This can be used to filter a node out and prevent it from being considered a peer using application specific logic.

    - **`ConflictDelegate`**

      Used to inform a client that a node has attempted to join which would result in a name conflict. This happens if two clients are configured with the same name but different addresses.

    - **`EventDelegate`**

      A simpler delegate that is used only to receive notifications about members joining and leaving. The methods in this delegate may be called by multiple threads, but never concurrently. This allows you to reason about ordering.

    - **`MergeDelegate`**

      Used to involve a client in a potential cluster merge operation. Namely, when a node does a promised push/pull (as part of a join), the delegate is involved and allowed to cancel the join based on custom logic. The merge delegate is NOT invoked as part of the push-pull anti-entropy.

    - **`NodeDelegate`**

      Used to manage node related events. e.g. metadata

    - **`PingDelegate`**

      Used to notify an observer how long it took for a ping message to complete a round trip. It can also be used for writing arbitrary byte slices into ack messages. Note that in order to be meaningful for RTT estimates, this delegate does not apply to indirect pings, nor fallback pings sent over promised connection.

  - **`CompositeDelegate`**

    CompositeDelegate is a helpful struct to split the `Delegate` into multiple small delegates, so that users do not need to implement full `Delegate` when they only want to custom some methods in the Delegate.

## Q & A

- ***Does Rust's memberlist implemenetation compatible to Go's memberlist?***

  No but yes! Rust's memberlist impelementation use a protobuf-like forward and backward compatible encoding/decoding, Go's implementation
  use message pack. It is possible in theory, users need to implement their own transport layer, but not recommand, you are facing
  expensive overhead!

- ***If Go's memberlist adds more functionalities, will this project also support?***
  
  Yes! And this project may also add more functionalities whereas the Go's memberlist does not have. e.g. wasmer support, bindings to other languages and etc.

## Related Projects

- [`agnostic`](https://github.com/al8n/agnostic): helps you to develop runtime agnostic crates
- [`getifs`](https://github.com/al8n/getifs): A bunch of cross platform network tools for fetching interfaces, multicast addresses, local ip addresses, private ip addresses, public ip addresses and etc.
- [`nodecraft`](https://github.com/al8n/nodecraft): crafting seamless node operations for distributed systems, which provides foundational traits for node identification and address resolution.
- [`peekable`](https://github.com/al8n/peekable): peekable reader and async reader

### Which crates are using memberlist?

- [`serf`](https://github.com/al8n/serf): A decentralized solution for service discovery and orchestration that is lightweight, highly available, and fault tolerant.

#### License

`memberlist` is under the terms of the MPL-2.0 license.

See [LICENSE](./LICENSE) for details.

Copyright (c) 2025 Al Liu.

Copyright (c) 2013 HashiCorp, Inc.

[Github-url]: https://github.com/al8n/memberlist/
[CI-url]: https://github.com/al8n/memberlist/actions/workflows/ci.yml
[doc-url]: https://docs.rs/memberlist
[crates-url]: https://crates.io/crates/memberlist
[codecov-url]: https://app.codecov.io/gh/al8n/memberlist/
[zh-cn-url]: https://github.com/al8n/memberlist/tree/main/README-zh_CN.md
[discord]: https://discord.gg/f3EtWBM7ke
