Serf Documentation
repository·master·Indexed 27 days ago
https://github.com/hashicorp/serfSerf is a lightweight, decentralized, and masterless solution for service discovery and orchestration using a gossip protocol to manage cluster membership, detect node failures, and propagate events. It includes a Go client package for interacting with Serf agents via msgpack RPC, a CLI for agent management, and support for custom event handlers, cluster queries, and tag-based configuration.
What's inside Serf
- Serf is a decentralized, fault-tolerant, and highly available tool for cluster membership, failure detection, and orchestration. It is designed to be extremely lightweight, using only 5 to 10 MB of resident memory and communicating primarily via infrequent UDP messages. It supports Linux, Mac OS X, and Windows.
Use the Serf Client package to interact with Serf agents
masterTheclientpackage is the official reference implementation for interacting with a Serf agent via its msgpack RPC system. It is the same package used by the Serf CLI to execute commands. You can use this package to programmatically control Serf agents and participate in cluster operations.Understand Lifeguard Enhancements
masterLifeguard is an automatic enhancement (introduced in Serf 0.8) that makes the gossip protocol more robust against degraded nodes (nodes experiencing CPU or network exhaustion). It requires no manual configuration and works via two mechanisms:
- Nack Messages: If a probing node detects it is missing 'nack' messages, it recognizes it may be degraded and slows down its failure detector. It speeds back up as 'nack' messages arrive.
- Dynamic Suspicion Timeout: The suspicion timeout for a node is initially very long. If other nodes in the cluster confirm the suspicion, the timer accelerates. This allows a suspected node more time to refute its status if the cluster is experiencing widespread degradation.
Core features of Serf
masterSerf uses a gossip protocol to provide three primary capabilities:
- Membership: Maintains cluster membership lists and executes custom handler scripts when membership changes (e.g., notifying a load balancer when a web server node joins or leaves).
- Failure detection and recovery: Automatically detects failed nodes within seconds, notifies the cluster, and executes handler scripts. Serf attempts to recover failed nodes by periodically attempting to reconnect.
- Custom event propagation: Allows broadcasting custom events (fire-and-forget) and queries (real-time request/response) to the cluster for tasks like triggering deploys or propagating configuration.
Use Serf for membership-driven automation
masterSerf's masterless, fault-tolerant membership system can be used as a foundation for several automation tasks:
- Load Balancer Updates: Configure load balancers to listen for membership changes so they can automatically update their node lists when web servers join or leave the cluster.
- Cache Clustering: Use Serf to cluster nodes like Memcached or Redis, updating proxies (e.g., twemproxy) or application server lists in real-time.
- DNS Management: Use Serf membership events to update internal DNS records nearly instantly when nodes join, leave, or fail, avoiding stale records.
- Service Discovery: Build a service discovery layer on top of Serf by leveraging its ability to maintain an up-to-date list of online nodes, their addresses, and metadata.
Use the Serf RPC mechanism for programmatic control
masterThe Serf agent provides a complete RPC mechanism that allows you to control the agent programmatically. This mechanism is identical to the one used by the Serf CLI, enabling external applications to leverage Serf's capabilities without needing to embed the library directly. It also serves as a high-performance IPC mechanism for applications to receive events immediately, avoiding the overhead of the standard fork/exec model used by event handlers.Compare Serf with ZooKeeper, doozerd, and etcd
masterWhen choosing between Serf and strongly consistent systems like ZooKeeper, doozerd, or etcd, consider your requirements for consistency, scalability, and feature set:
Serf
- Architecture: Based on gossip protocols.
- Consistency Model: Embraces eventual consistency.
- Core Features: Membership, failure detection, and user events.
- Resilience: Designed to operate under network partitions.
- Use Case: Ideal as a tool for system administrators and application developers, particularly for service discovery layers where partition tolerance is critical.
ZooKeeper, doozerd, and etcd
- Architecture: Client/server architecture requiring a quorum (usually a simple majority) to operate.
- Consistency Model: Strongly consistent.
- Core Features: Expose primitives used via client libraries to build complex distributed systems (e.g., leader election).
- Limitations:
- Complexity: Cannot be used directly as a tool; requires application libraries.
- Scalability: Failure detection often relies on heartbeating, which scales linearly with the number of nodes and places demand on a fixed set of servers.
- Partition Sensitivity: If a majority of nodes are unavailable, writes are disallowed. Performance may degrade during network issues because failures are indistinguishable from slow responses.
Integration Strategy
Serf is not mutually exclusive with strongly consistent systems. You can use them in combination to build systems that are more scalable and fault-tolerant without sacrificing the coordination features provided by strong consistency.
Understand Serf Protocol Compatibility
masterSerf guarantees that every subsequent release will remain backwards compatible with at least one prior version. This allows for rolling upgrades of agents in large clusters without causing cluster disruption.
By default, Serf agents speak the latest protocol. To facilitate upgrades, agents can be configured to speak an earlier protocol, though new features may not be available when using an older protocol version.
Compare Serf and Fabric for service management
masterWhen choosing between Serf and Fabric for service management and system administration, consider the following trade-offs:
Serf
- Mechanism: Rapidly broadcasts messages to the entire cluster in a distributed fashion.
- Scalability: High parallelism and scalability; can propagate messages to an entire cluster within seconds.
- Membership: Membership is built directly into the tool (automatic node discovery).
- Limitations: Queries collect a limited amount of output and lack complex control flow.
Fabric
- Mechanism: Sends commands from a single box via SSH to a group of nodes.
- Capabilities: High capability for system administration; can collect unlimited command output and stop execution upon encountering errors.
- Limitations: Limited by execution speed and lacks native node discovery (requires a provided list of nodes).
Hybrid Approach
For optimal results, combine both tools: use Serf to query for active nodes and leverage message broadcasts for high-speed tasks, while using Fabric for direct SSH execution when detailed command output is required.
Understand Serf Network Coordinates
masterSerf uses a network tomography system based on the Vivaldi algorithm to compute network coordinates for nodes in a cluster. These coordinates allow for the estimation of network round-trip time (RTT) between any two nodes using a simple calculation.
Key characteristics:
- Low Overhead: Coordinates are obtained by adding a small amount of data to existing gossip protocol probe messages.
- Decentralized: The system relies only on observations between peers and does not require a central manager.
- Abstract Space: The resulting coordinates model an abstract space for RTT estimation rather than physical layout (e.g., they do not represent rack locations).
- Dimensions: A coordinate consists of an eight-dimensional Euclidean vector, plus single values for
Adjustment,Error, andHeight.
Understand the Serf Gossip Protocol
masterSerf uses a gossip protocol based on SWIM (Scalable Weakly-consistent Infection-style Process Group Membership Protocol) to broadcast messages and manage cluster membership.
Key characteristics include:
- Membership: New nodes join by performing a full state sync over TCP with an existing member and then gossiping their existence via UDP.
- Gossip Mechanism: Uses UDP with a fixed fanout and interval to ensure constant network usage regardless of cluster size.
- State Sync: Periodic full state exchanges are performed over TCP to ensure membership list convergence and recovery from network partitions.
- Failure Detection: Uses periodic random probing. If a probe fails, an indirect probe is attempted via random nodes. If both fail, the node is marked
suspicious. If the node does not dispute the suspicion within a configurable period, it is markeddead.
Understand the Serf RPC Protocol implementation
masterThe Serf RPC protocol uses MsgPack over TCP. All communication follows a request/response pattern where requests include a header and an optional body, and responses include a header and an optional body.
Request Header Format:
{"Command": "command_name", "Seq": 0}Response Header Format:
{"Seq": 0, "Error": ""}Key Concepts:
Command: Specifies the action to perform.Seq: A sequence number used to track requests. Responses use the sameSeqas the request, allowing for concurrent, non-FIFO processing. Do not reuseSeqvalues between different commands.Error: Responses may contain an error string.