This page shows how to establish a persistent connection to a server using the Connection and Keep-Alive headers. The -H "Connection: keep-alive" request header indicates that the client requires a persistent connection. The -H "Keep-Alive: timeout = 5, max = 100" request header sets the timeout value. In this Keep-Alive Connection example, we have sent a request to the ReqBin echo URL. Click Send to execute the request online and see the result. The Java code was automatically generated for the Keep Alive Connection example.
Hypertext Transfer Protocol (HTTP) is a data transfer protocol that enables the communication between HTTP clients and servers. HTTP is based on a "request-response" protocol between a client and a server. The client makes a request and initiates a connection, and the server receives the request and then executes it and sends the result to the client. Each HTTP message consists of a request line, HTTP headers, and a message body.
What is HTTP Keep-Alive?
What is Connection header?
The Connection header controls whether the current connection remains open after the transaction. Persistent connections allow sending multiple requests without opening a new connection for every single transaction. The links are persistent by default for HTTP/1.1 clients. For HTTP/1.0 connections, you must pass the -H "Connection: keep-alive" request header to indicate that you explicitly want constant contact.
How to connect Keep-Alive?
An example of setting up a persistent connection to the server using the Connection and Keep-Alive headers:
Keep-Alive Connection Example
GET /echo HTTP/1.1
Keep-Alive: timeout=5, max=100
What are the benefits of a Keep-Alive connection?
The HTTP Keep-Alive header maintains the connection between the client and the server, reducing its time to serve files. A persistent connection also reduces the number of TCP and SSL/TLS connection requests. This results in shorter round-trip times (RTT). Establishing TCP connections first requires exchanging SYN and ACK packets between the client and server before transmitting data. When the Keep-Alive header is used, it means there is no need to keep this process running. This results in:
Conserving network resources: Using one connection for each client reduces the load on network resources.
Reduced network congestion - Fewer TCP connections between your servers and clients can result in less network congestion.
Reduced Latency - Reducing the number of 3-way handshakes can result in less site latency. This is especially true for SSL/TLS connections, which require additional round-trip cycles to encrypt and validate connections.
What is TCP?
The Transmission Control Protocol (TCP) allows applications and computing devices to exchange messages over a network. TCP is designed to send packets over the internet and ensure the successful delivery of data and messages over the web. TCP combines data to be transferred between server and client. It guarantees the integrity of the information being communicated over a network. Before it sends data, TCP establishes a connection between a source and its destination, ensuring it remains life until communication begins.