Adding peers in an etcd cluster adds network, CPU, and disk overhead to the leader since each one requires replication. Peers primarily provide resiliency in the event of a leader failure but the benefit of more failover nodes decreases as the cluster size increases. A lightweight alternative is the standby.
Standbys are a way for an etcd node to forward requests along to the cluster but the standbys are not part of the Raft cluster themselves. This provides an easier API for local applications while reducing the overhead required by a regular peer node. Standbys also act as standby nodes in the event that a peer node in the cluster has not recovered after a long duration.
There are three configuration parameters used by standbys: active size, remove delay and standby sync interval.
The active size specifies a target size for the number of peers in the cluster. If there are not enough peers to meet the active size, standbys will send join requests until the peer count is equal to the active size. If there are more peers than the target active size then peers are removed by the leader and will become standbys.
The remove delay specifies how long the cluster should wait before removing a dead peer. By default this is 30 minutes. If a peer is inactive for 30 minutes then the peer is removed.
The standby sync interval specifies the synchronization interval of standbys with the cluster. By default this is 5 seconds. After each interval, standbys synchronize information with cluster.
If find existing standby cluster info:
Goto standby loop
Find cluster as required
If determine to start peer server:
Goto peer loop
Else:
Goto standby loop
Peer loop:
Start peer mode
If running:
Wait for stop
Goto standby loop
Standby loop:
Start standby mode
If running:
Wait for stop
Goto peer loop
Fetch machine info
If cannot match version:
return false
If active size <= peer count:
return false
If it has existed in the cluster:
return true
If join request fails:
return false
return true
Note
Start raft server
Start other helper routines
When removed from the cluster:
Stop raft server
Stop other helper routines
Loop:
Sleep for some time
Sync cluster, and write cluster info into disk
Check active size and send join request if needed
If succeed:
Clear cluster info from disk
Return
Return '404 Page Not Found' always on peer address. This is because peer address is used for raft communication and cluster management, which should not be used in standby mode.
Serve requests from client:
Redirect all requests to client URL of leader
Note
If machine has existed in the cluster:
Return
If peer count < active size:
Add peer
Increase peer count
If machine exists in the cluster:
Remove peer
Decrease peer count
This is only run by current cluster leader.
Loop:
Sleep for some time
If peer count > active size:
Remove randomly selected peer
This is only run by current cluster leader.
Loop:
Sleep for some time
For each peer:
If peer last activity time > remove delay:
Remove the peer
Goto Loop
First few machines run in peer mode.
All the others check the status of the cluster and run in standby mode.
Machines with log data restart with join failure.
Machines in peer mode recover heartbeat between each other.
Machines in standby mode always sync the cluster. If sync fails, it uses the first address from data log as redirect target.
Leader of the cluster lose the connection with the peer.
When the time exceeds remove delay, it removes the peer from the cluster.
Machine in standby mode finds one available place of the cluster. It sends join request and joins the cluster.
Note
No change for the cluster.
New instance cannot join immediately after one peer is kicked out of the cluster, because the leader doesn't know the info about the standby instances.
It may introduce join collision
Cluster needs a good interval setting to balance the join delay and join collision.
Based on heartbeat miss and remove delay, standby could adjust its next check time.
Preregister the promotion target when heartbeat miss happens.
Get the estimated cluster size from the check happened in the sync interval, and adjust sync interval dynamically.
Accept join requests based on active size and alive peers.