Consistent Core
Maintain a smaller cluster providing stronger consistency to allow the large data cluster to coordinate server activities without implementing quorum-based algorithms.
Problem
When a cluster needs to handle a lot of data, it uses more and more servers. For a cluster of servers, there are some common requirements, such as selecting a server to be the master for a particular task, managing group membership information, mapping data partitions to servers, and so on. This functionality requires a strong consistency guarantee of linearizability. The implementation also needs to be fault-tolerant. A common approach is to use a fault-tolerant consensus algorithm based on Majority Quorum . However, in quorum-based systems, throughput degrades with the size of the cluster.
Solution
Implement a smaller cluster of three to five nodes which provides linearizability guarantee as well as fault tolerance. A separate data cluster can use the small consistent cluster to manage metadata and for taking cluster-wide decisions with primitives like Lease ( Figure 25.1 ). This way, the data cluster can grow to a large number of servers but still be able to do certain actions that need strong consistency guarantees using the smaller metadata cluster.
Figure 25.1 Consistent core
Providing linearizability along with fault tolerance needs consensus algorithms like Raft [ Ongaro2014 ], Zab [ Reed2008 ], or Paxos to be implemented.
While a consensus algorithm is an essential requirement to implement a consistent core, there are various aspects of client interaction—such as how a client finds the leader or how duplicate requests are handled—which are important implementation decisions. There are also important considerations regarding safety and liveness. Paxos defines only the consensus algorithm, but these other implementation aspects are not well documented in the Paxos literature. Raft very clearly documents various implementation aspects, includes a reference implementation, 85 and is the most widely used algorithm today.
-
[https://github.com/logcabin/logcabin](https://github.com/logcabin/logcabin)
Since the entire cluster depends on the consistent core, it is critical to be aware of the details of the consensus algorithm used. Consensus implementations can run into liveness issues in some tricky network partition situations. For example, a Raft cluster can be disrupted by a partitioned server that can continuously trigger leader election unless special care is taken.
A typical interface of consistent core looks like this:
Click here to view code image
public interface ConsistentCore {
CompletableFuture put(String key, String value);
List<String> get(String keyPrefix);
CompletableFuture registerLease(String name, long ttl);
void refreshLease(String name);
void watch(String name, Consumer<WatchEvent> watchCallback);
}
At a minimum, consistent core provides a simple key-value storage mechanism. It is used to store metadata.
Metadata Storage
The storage is implemented using consensus algorithms such as Raft. It is an implementation of Replicated Log where replication is handled by Leader and Followers and High-Water Mark is used to track successful replication using Majority Quorum .
Supporting Hierarchical Storage
Consistent core is generally used to store data for things like group membership or task distribution across servers. A common usage pattern is to scope the type of metadata with a prefix. For example, for group membership, the keys will look like /servers/1 , servers/2 , and so on. For tasks assigned to servers, the keys can be /tasks/task1 , /tasks/task2 . This data is generally read using all the keys with a specific prefix. For example, to get information about all the servers in the cluster, all the keys with the prefix /servers are read.
Here’s an example usage.
Each server registers itself with the consistent core by creating its own key with the prefix /servers .
Click here to view code image
client1.setValue("/servers/1", "{address:192.168.199.10, port:8000}");
client2.setValue("/servers/2", "{address:192.168.199.11, port:8000}");
client3.setValue("/servers/3", "{address:192.168.199.12, port:8000}");
Clients can then get to know about all the servers in the cluster by reading keys with the prefix /servers :
Click here to view code image
assertEquals(client1.getValue("/servers"),
Arrays.asList("{address:192.168.199.12, port:8000}",
"{address:192.168.199.11, port:8000}",
"{address:192.168.199.10, port:8000}"));
Because of this hierarchical nature of data storage, Apache ZooKeeper and Chubby [ Burrows2006 ] provide a filesystem-like interface with the concept of parent and child nodes. Users can create directories and files, or nodes, inside the directories. etcd3 has a flat key space with the ability to get a range of keys.
Handling Client Interactions
One of the key features of consistent core is how a client interacts with the core. The following aspects are critical for clients to work with the consistent core.
Finding the Leader
Serializability and Linearizability
When read requests are handled by follower servers, it is possible that clients can get stale data, as the latest commits from the leader have not yet reached the followers. The order in which the updates are received by the client is still maintained but the updates might not be the most recent. This is the serializability guarantee as opposed to linearizability. Linearizability guarantees that every client gets the most recent updates. Clients can work with the serializability guarantee when they just need to read metadata and can tolerate stale metadata for a while. For operations like Lease , linearizability is strictly needed.
If the leader is partitioned from the rest of the cluster, clients can get stale values from the leader. Raft describes a mechanism to provide linearizable reads (see, for example, the etcd implementation of readIndex ). YugabyteDB uses a technique called leader lease to achieve the same guarantee as described in the section “ Bypassing the Log for Read Requests ” of the Replicated Log pattern.
A similar situation can happen when followers are partitioned. A follower that’s partitioned might not return the latest values to the client. To make sure the followers are not partitioned and are up-to-date with the leader, they need to query the leader and wait till they receive the latest updates before responding to the client. See the proposed Kafka design [ Chen2020 ] as an example.
It’s important that all the operations are executed on the leader, so a client library needs to find the leader server first. There are two approaches to do this.
-
The follower servers in the consistent core know about the current leader, so if a client connects to a follower, the latter can return the address of the leader. The client can then directly connect to the leader identified in the response. It should be noted that the servers might be in the middle of a leader election when the client tries to connect. In that case, servers cannot return the leader address and the client needs to wait and try another server.
-
Servers can implement a forwarding mechanism and forward all the client requests to the leader. This allows clients to connect to any server. Again, if servers are in the middle of a leader election, clients will need to retry until the leader election is successful and a legitimate leader is established.
Products like ZooKeeper and etcd implement this approach because they allow some read-only requests to be handled by the follower servers. This avoids a bottleneck on the leader when a large number of clients are read-only. This also reduces complexity in the clients as they don’t need to choose to connect to either leader or follower based on the type of the request.
A simple mechanism to find the leader is to try to connect to each server and send a request. The server responds with a redirect response if it’s not the leader.
Click here to view code image
private void establishConnectionToLeader(List<InetAddressAndPort> servers) {
for (var server : servers) {
try {
var socketChannel = new SingleSocketChannel(server,
10);
logger.info("Trying to connect to " + server);
var response = sendConnectRequest(socketChannel);
if (isRedirectResponse(response)) {
var redirectResponse =
deserialize(response.getMessageBody(),
RedirectToLeaderResponse.class);
redirectToLeader(redirectResponse);
break;
} else if (isLookingForLeader(response)) {
logger.info("Server is looking for leader. Trying next server");
continue;
} else { //we know the leader
logger.info("Found leader. Establishing a new connection.");
newPipelinedConnection(server);
break;
}
} catch (IOException e) {
logger.info("Unable to connect to " + server);
//try next server
}
}
}
private boolean isLookingForLeader(RequestOrResponse requestOrResponse) {
return requestOrResponse.getRequestId() == RequestId.LookingForLeader;
}
private void redirectToLeader(RedirectToLeaderResponse redirectResponse) {
newPipelinedConnection(redirectResponse.leaderAddress);
logger.info("Connected to the new leader "
+ redirectResponse.leaderServerId
+ " " + redirectResponse.leaderAddress
+ ". Checking connection");
}
Just establishing TCP connection is not enough—we need to know if the server can handle our requests. So clients send a special connection request for the server to acknowledge if it can serve requests or else redirect to the leader server.
Click here to view code image
private RequestOrResponse
sendConnectRequest(SingleSocketChannel socketChannel)
throws IOException {
try {
var request =
new RequestOrResponse(new ConnectRequest(),
nextRequestNumber.getAndIncrement());
return socketChannel
.blockingSend(request);
} catch (IOException e) {
resetConnectionToLeader();
throw e;
}
}
If an existing leader fails, the same technique is used to identify the newly elected leader from the cluster.
Once connected, the client maintains a Single-Socket Channel to the leader server.
Handling Duplicate Requests
In cases of failure, clients may try to connect to the new leader, resending the requests. But if those requests were already handled by the failed leader prior to failure, that might result in duplicates. Therefore, it’s important to have a mechanism on the servers to ignore duplicate requests. The Idempotent Receiver pattern is used to implement duplicate detection.
Coordinating tasks across a set of servers can be done by using Lease . The same can be used to implement group membership and a failure detection mechanism.
State Watch is used to get notifications of changes to the metadata or time-bound leases.
Examples
-
Google is known to use the Chubby lock service for coordination and metadata management.
-
Apache Kafka uses Apache ZooKeeper to manage metadata and take decisions, such as a leader election for cluster master. The proposed architecture change [ McCabe2020 ] in Kafka will replace ZooKeeper with its own Raft-based controller cluster.
-
Apache BookKeeper uses ZooKeeper to manage cluster metadata.
-
Kubernetes uses etcd for coordination, managing cluster metadata, and group membership information.
-
All the big data storage and processing systems like HDFS, 86 Apache Spark, 87 Apache Flink 88 use Apache ZooKeeper for high availability and cluster coordination.
-
[https://hadoop.apache.org](https://hadoop.apache.org) -
[https://spark.apache.org](https://spark.apache.org) -
[https://flink.apache.org](https://flink.apache.org)