HTTP is statel-less,so what does it mean by keep-alive?
Asked Answered
V

5

19
Keep-Alive: 300
Proxy-Connection: keep-alive

As we know HTTP connection is closed when the request gets responded,so what does it mean by keep-alive,can someone elaborate this?

Vacuva answered 19/5, 2011 at 15:20 Comment(0)
S
16

This means it is OK to keep the connection open to ask for more resources like for example images and stylesheets.

Spit answered 19/5, 2011 at 15:21 Comment(4)
so it's wrong to say HTTP is state-less,it can be state-full in this case,right?Vacuva
No, HTTP is indeed stateless. Meaning that the data returned by the server does not depend on any previous actions by the user. Cookies and other non-HTTP methods is what makes the web appear to be stateful, enabling user to for example log-in and out on websites. State in this sense has nothing to do with the connection.Spit
seems I'm understanding stateless wrongly all the time,I thought it the same as non-persistent connection....Can you give an example of stateful protocol then?Vacuva
TCP for example is a stateful protocol. Basically every protocol where some sort of handshaking is performed is stateful. See mama.indstate.edu/users/shri/State.htmlSpit
R
8

As we know HTTP connection is closed when the request gets responded

What is an HTTP connection? Actually, it's a socket connection over which HTTP is implemented. Only in HTTP1.0 does the connection get closed after each response. In order to save on the cost of setting up a TCP/IP connection, HTTP1.1 specifies that unless the client sends a header

Connection:close

or the server comes back with the same header, then the socket remains open. You can feed as many requests as you want into this socket and the responses will come back in the order that they were requested. This requires that the response is either sent with a chunked transfer encoding or contains a content-length header so that the end of each response can be detected/calculated.

The proxy-connection header is different again, and is related only to the conversation between client and proxy servers.

I'd recommend this page as an excellent guide to the protocol.

HTTP Made Really Easy

Renatorenaud answered 19/5, 2011 at 15:27 Comment(2)
how can it ensure the responses will come back in the order that they were requested if the connection is persistent? Probably the later request gets its response earlier,right?Vacuva
That's the servers responsibility. Given an order of requests on a single socket, then the server MUST respond in the same order.Renatorenaud
B
5

This question is already answered and accepted, but I would like to explain in details:

Keep-alive cannot maintain one connection forever; the application running in the server determines the limit with which to keep the connection alive for, and in most cases you can configure this limit.

In HTTP/1.1, Keep-alive is used by default. If clients have additional requests, they will use the same connection for them.

The term stateless doesn't mean that the server has no ability to keep a connection. It simply means that the server doesn't recognize any relationships between any two requests.

Bullhorn answered 14/4, 2015 at 14:45 Comment(0)
E
1

The protocol is indeed stateless, but keep-alive indicates that the connection should be kept open between the client and server.

Opening a TCP connection is a relatively heavyweight operation, and keeping that connection open avoids the setup and teardown cost associated with opening a new connection.

Excursion answered 19/5, 2011 at 15:26 Comment(2)
How can it be stateless if the connection is persistent?Vacuva
Yes the connection is quasi-persisted temporarily until the client's resources have finished being served. But once the server is done sending the resource, the connection closes, making it stateless.Icebreaker
P
1

Keep-alive has nothing to do with statefulness.

In networking, one of the costliest operation is repeatedly opening and closing connections. Modern HTML pages, however, technically ask you to do precisely that: First, get the page, then get each resource and repeat until you have everything. Since that would be incredibly slow, HTTP/1.1 allows agents to keep the connection alive until he ahs everything he wants from the server.

Keep-aliveis basically the web browser telling the server not to hang up yet.

Preacher answered 19/5, 2011 at 15:30 Comment(2)
then what has anything to do with statefulness?Vacuva
In HTTP, nothing, because HTTP is a stateless protocol. Each request in HTTP is made in a vacuum. Contrast TCP where each subsequent packet you send is a continuation of the previous packet. TCP needs to take note of how each packet is received so it can build a meaningful message for the higher protocols (like HTTP). That's what makes a protocol stateful: the fact that each request/packet is a part of a conversation rather than a single request/packetPreacher

© 2022 - 2024 — McMap. All rights reserved.