A highly customable, adaptable, runtime agnostic and WASM/WASI friendly decentralized solution for service discovery and orchestration that is lightweight, highly available, and fault tolerant.
Port and improve HashiCorp's serf to Rust.
English | 简体中文
serf is a decentralized solution for service discovery and orchestration that is lightweight, highly available, and fault tolerant.
The use cases for such a library are far-reaching: all distributed systems require membership, and serf is a re-usable solution to managing cluster membership and node failure detection.
serf 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.
serf is WASM/WASI friendly, all crates can be compiled to wasm-wasi
and wasm-unknown-unknown
(need to configure the crate features).
-
By using
TCP/UDP
,TLS/UDP
transportserf = { version = "0.3", 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`, `async-std` "tokio", # Enable one tls implementation. This is optional. # Users can just use encryption feature with plain TCP. # # "tls", ] }
-
By using
QUIC/QUIC
transportFor
QUIC/QUIC
transport, as QUIC is secure and reliable, so enable checksum or encryption makes no sense.serf = { version = "0.3", 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`, `async-std` "tokio", # Enable one of the QUIC implementation # Builtin support is `quinn` "quinn", ] }
See examples/toyconsul, a toy eventually consistent distributed registry.
serf is based on "SWIM: Scalable Weakly-consistent Infection-style Process Group Membership Protocol". 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 serf 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", along with the serf source.
-
Does Rust's serf implemenetation compatible to Go's serf?
No! Rust implementation use protobuf-like encoding/decoding whereas Go's implementation uses message pack.
-
If Go's serf adds more functionalities, will this project also support?
Yes! And this project may also add more functionalities whereas the Go's serf does not have. e.g. wasmer support, bindings to other languages and etc.
Unlike the original Go implementation, Rust's serf use highly generic and layered architecture, users can easily implement a component by themselves and plug it to the serf. Users can even custom their own Id
and Address
.
Here are the layers:
-
Transport Layer
By default, Rust's serf provides two kinds of transport --
QuicTransport
andNetTransport
.-
Runtime Layer
Async runtime agnostic are provided by
agnostic
's Runtime trait,tokio
,async-std
andsmol
are supported by default. Users can implement their ownRuntime
and plug it into the serf. -
Address Resolver Layer
The address resolver layer is supported by
nodecraft
's AddressResolver trait. -
Builtin stream layers for
NetTransport
: -
QUIC transport is an experimental transport implementation, it is well tested but still experimental.
Builtin stream layers for
QuicTransport
:
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 Serf. All the methods must be thread-safe, as they can and generally will be called concurrently.
Here are the sub delegate traits:
-
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.
-
ReconnectDelegate
Used to custom reconnect behavior, users can implement to allow overriding the reconnect timeout for individual members.
-
-
CompositeDelegate
CompositeDelegate is a helpful struct to split the
Delegate
into multiple small delegates, so that users do not need to implement fullDelegate
when they only want to custom some methods in the Delegate.
-
agnostic
: Helps you to develop runtime agnostic cratesagnostic-mdns
: Simple and lightweight mDNS client/server library for any async runtime.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
: Crafting seamless node operations for distributed systems, which provides foundational traits for node identification and address resolution.peekable
: Peekable reader and async reader.memberlist
: A highly customable, adaptable, runtime agnostic and WASM/WASI friendly Gossip protocol which helps manage cluster membership and member failure detection.
serf
is under the terms of the MPL-2.0 license.
See LICENSE for details.
Copyright (c) 2025 Al Liu.
Copyright (c) 2013 HashiCorp, Inc.