Content Length, KeepAlive, and Connection Close
10 May 2024 02:46 PM


MarkLogic application servers will keep a connection open after completing and responding to a request, waiting for another new request, until the Keep Alive timeout expires. However, there is an exception scenario where the connection will close regardless of timeout settings when the content is unknown before the reply is started. This article is intended to provide further insight into connection close with respect to payload size.

HTTP Header


In general, application servers communicating in HTTP send the Content-Length header as part of their response HTTP Headers to indicate how many bytes of data the client application should expect to receive. For example,

HTTP/1.1 200 OK
Content-type: application/sparql-results+json; charset=UTF-8
Server: MarkLogic
Content-Length: 1264
Connection: Keep-Alive
Keep-Alive: timeout=5

This requires application servers to know the length of the entire response data before the very first bytes (Response HTTP Headers) are put on to the wire. For small amounts of data, the time to calculate the content-length is fast; for large amounts of content, the calculation may be time consuming with the extreme being that the client finds the server unresponsive due to the delay in calculating the entire response length. Additionally, the server may need to bring the entire content into memory, putting a further burden on server resources.

A related situation occurs when the response is compressed.  The final compressed length isn't known up front.


To allow servers to begin transmitting dynamically generated content before knowing the total size of that content, HTTP 1.1 supports chunked encoding. This technique is widely used in music and video streaming and other industries. Chunked encoding eliminates the need of knowing the entire content length before sending a portion of the data, thus making the server looks more responsive.

MarkLogic Server 11 adds both compression and chunking capabilities; see HTTP Compression and Chunking.

Connection Close

In MarkLogic Server v7 and v8, MarkLogic Server closes the connection after transmitting content greater 1 MB, which allows MarkLogic to avoid calculating content length in advance. The client will not then see Content-Length Header for larger (>1 MB) content in the HTTP response from MarkLogic. Instead it will receive a Connection Close header in the HTTP response. After sending the entire content, MarkLogic Server will terminate the connection, to indicate to Client that the end of content has been reached.

Closing the existing connection for content larger then 1 MB is an exception to the Keep-Alive configuration. This may result in unexpected behavior on clients that relying on MarkLogic Server respecting the Keep-Alive configuration, so this behavior should be accounted while designing Client Application Connection Pool.

Client Applications may have to send TCP SYN again to establish new connection to send subsequent request, which will add overhead of TCP 3 way handshake before sending next request. However, in the context of the data transfer for larger payload (>1MB), where many more round trips are added in overall communication, overhead of TCP 3 way handshake is very nominal.

Similarly, if the client accepts gzip compression, the length is unknown and the connection will be closed after the reply.  Turning on chunking will create a reply where the entire length of the response does not need to be known ahead of time, and so keep-alive can be maintained.

Further Reading

(0 vote(s))
Not helpful

Comments (0)