Request Pipeline
Improve latency by sending multiple requests on the connection without waiting for the response of the previous requests.
Problem
Communicating between servers within a cluster using Single-Socket Channel can cause performance issues if requests need to wait for responses to previous requests. To achieve better throughput and latency, the request queue on the server should be filled enough to make sure the server capacity is fully utilized. For example, when Singular Update Queue is used within a server, it can always accept more requests, until the queue fills up, while it’s processing a request. If only one request is sent at a time, most of the server capacity is unnecessarily wasted.
Solution
Nodes send requests to other nodes without waiting for responses from previous requests. This is achieved by creating two separate threads, one for sending requests over a network channel and the other for receiving responses from the network channel ( Figure 32.1 ).
Figure 32.1 Request pipeline
The sender node sends the requests over the socket channel, without waiting for response:
Click here to view code image
class SingleSocketChannel...
public void sendOneWay(RequestOrResponse request) throws IOException {
var dataStream = new DataOutputStream(socketOutputStream);
byte[] messageBytes = serialize(request);
dataStream.writeInt(messageBytes.length);
dataStream.write(messageBytes);
}
A separate thread is started to read responses:
Click here to view code image
class ResponseThread...
class ResponseThread extends Thread implements Logging {
private volatile boolean isRunning = false;
private SingleSocketChannel socketChannel;
public ResponseThread(SingleSocketChannel socketChannel) {
this.socketChannel = socketChannel;
}
@Override
public void run() {
try {
isRunning = true;
logger.info("Starting responder thread = " + isRunning);
while (isRunning) {
doWork();
}
} catch (IOException e) {
getLogger().error(e); //thread exits if stopped or there is IO error
}
}
public void doWork() throws IOException {
RequestOrResponse response = socketChannel.read();
logger.info("Read Response = " + response.getRequestId());
processResponse(response);
}
The response handler can immediately process the response or submit it to a Singular Update Queue.
There are two issues with the request pipeline.
If requests are continuously sent without waiting for the response, the node accepting the request can be overwhelmed. For this reason, there is an upper limit on how many requests can be kept in-flight at a time. Any node can send up to the maximum number of requests to other nodes. Once the maximum inflight requests are sent without receiving a response, no more requests are accepted and the sender is blocked. A very simple strategy to limit maximum inflight requests is to use a blocking queue to keep track of requests. The queue is initialized with the number of requests which can be in-flight. Once the response is received for a request, it’s removed from the queue to create room for more requests. Here, the maximum of five in-flight requests are accepted per socket connection:
Click here to view code image
class RequestLimitingPipelinedConnection...
private Map<InetAddressAndPort, ArrayBlockingQueue<RequestOrResponse>>
inflightRequests = new ConcurrentHashMap<>();
private int maxInflightRequests = 5;
public void send(InetAddressAndPort to,
RequestOrResponse request) throws InterruptedException {
var requestsForAddress = inflightRequests.get(to);
if (requestsForAddress == null) {
requestsForAddress = new ArrayBlockingQueue<>(maxInflightRequests);
inflightRequests.put(to, requestsForAddress);
}
requestsForAddress.put(request);
The request is removed from the in-flight request queue once the response is received.
Click here to view code image
class RequestLimitingPipelinedConnection...
private void consume(SocketRequestOrResponse response) {
var correlationId = response.getRequest().getCorrelationId();
var requestsForAddress = inflightRequests.get(response.getAddress());
var first = requestsForAddress.peek();
if (correlationId != first.getCorrelationId()) {
throw new RuntimeException(
"First response should be for the first request");
}
requestsForAddress.remove(first);
responseConsumer.accept(response.getRequest());
}
Handling failures while maintaining ordering guarantees becomes tricky to implement. Let’s say there are two in-flight requests. The first request failed and retried. The server might have processed the second request before the retried first request reaches the server.
Servers need some mechanism to make sure out-of-order requests are rejected. Otherwise, there’s always a risk of messages getting reordered in case of failures and retries. For example, Raft always sends the previous log index that is expected with every log entry. If the previous log index does not match, the server rejects the request. Kafka can allow max.in.flight.requests.per.connection to be more than one, with its idempotent producer implementation assigning a unique identifier to each message batch that is sent to the broker. The broker can then check the sequence number of the incoming request and reject it if the requests are out of order.
Examples
-
All consensus algorithms, such as Zab [ Reed2008 ] and Raft [ Ongaro2014 ], support request pipelines.
-
Apache Kafka encourages clients to use request pipelining to improve throughput.