107

I was asked to build a site , and one of the co-developer told me That I would need to include the keep-alive header.

Well I read alot about it and still I have questions.

msdn ->

The open connection improves performance when a client makes multiple requests for Web page content, because the server can return the content for each request more quickly. Otherwise, the server has to open a new connection for every request

Looking at

enter image description here

  • When The IIS (F) sends keep alive header (or user sends keep-alive) , does it mean that (E,C,B) save a connection which is only for my session ?
  • Where does this info is kept ( "this connection belongs to "Royi") ?
  • Does it mean that no one else can use that connection
  • If so - does it mean that keep alive-header - reduce the number of overlapped connection users ?
  • if so , for how long does the connection is saved to me ? (in other words , if I set keep alive- "keep" till when?)

p.s. for those who interested :

clicking this sample page will return keep alive header

Community
  • 1
  • 1
Royi Namir
  • 131,490
  • 121
  • 408
  • 714
  • 2
    Pfff, I've seen this in a lecture, but I'm not quite sure. I thought the keep-alive was only on the server and the user. After all, everything in between shouldn't even know it's HTTP, let alone look at the headers. – Noctua Dec 27 '13 at 09:46
  • The statement quoted from MSDN is drivel. It is the *client* that has to open a new connection if there is no keep-alive. – user207421 Aug 03 '16 at 10:00
  • And if you're building a site,not a Web server or client, the keepalive header is already done for you. – user207421 Feb 07 '17 at 00:49

1 Answers1

145

Where is this info kept ("this connection is between computer A and server F")?

A TCP connection is recognized by source IP and port and destination IP and port. Your OS, all intermediate session-aware devices and the server's OS will recognize the connection by this.

HTTP works with request-response: client connects to server, performs a request and gets a response. Without keep-alive, the connection to an HTTP server is closed after each response. With HTTP keep-alive you keep the underlying TCP connection open until certain criteria are met.

This allows for multiple request-response pairs over a single TCP connection, eliminating some of TCP's relatively slow connection startup.

When The IIS (F) sends keep alive header (or user sends keep-alive) , does it mean that (E,C,B) save a connection

No. Routers don't need to remember sessions. In fact, multiple TCP packets belonging to same TCP session need not all go through same routers - that is for TCP to manage. Routers just choose the best IP path and forward packets. Keep-alive is only for client, server and any other intermediate session-aware devices.

which is only for my session ?

Does it mean that no one else can use that connection

That is the intention of TCP connections: it is an end-to-end connection intended for only those two parties.

If so - does it mean that keep alive-header - reduce the number of overlapped connection users ?

Define "overlapped connections". See HTTP persistent connection for some advantages and disadvantages, such as:

  • Lower CPU and memory usage (because fewer connections are open simultaneously).
  • Enables HTTP pipelining of requests and responses.
  • Reduced network congestion (fewer TCP connections).
  • Reduced latency in subsequent requests (no handshaking).

if so , for how long does the connection is saved to me ? (in other words , if I set keep alive- "keep" till when?)

An typical keep-alive response looks like this:

Keep-Alive: timeout=15, max=100

See Hypertext Transfer Protocol (HTTP) Keep-Alive Header for example (a draft for HTTP/2 where the keep-alive header is explained in greater detail than both 2616 and 2086):

  • A host sets the value of the timeout parameter to the time that the host will allows an idle connection to remain open before it is closed. A connection is idle if no data is sent or received by a host.

  • The max parameter indicates the maximum number of requests that a client will make, or that a server will allow to be made on the persistent connection. Once the specified number of requests and responses have been sent, the host that included the parameter could close the connection.

However, the server is free to close the connection after an arbitrary time or number of requests (just as long as it returns the response to the current request). How this is implemented depends on your HTTP server.

CodeCaster
  • 131,656
  • 19
  • 190
  • 236
  • Define "overlapped connections" ----> I mean simultaneously. ( and I think the number of simultaneous connection will be reduced because as you said : "connection X is reserved for John cause it uses keep-alive header."....am I right ? – Royi Namir Dec 27 '13 at 11:23
  • Yes, that's correct, a client will make less simultaneous connections when using keep-alive, it will fire the requests in serial, not parallell. :) – CodeCaster Dec 27 '13 at 11:24
  • 1
    So what you're saying is that if the server can handle 100 connections at a time , and all those connections uses keep-alive , then the 101'st connection will be dumped ??? – Royi Namir Dec 27 '13 at 11:25
  • When an HTTP server is configured to a maximum of 100 TCP connections, then 100 TCP connections can be made at a time. Whether HTTP requests issued over those connections use keep-alive or not is irrelevant. How the 101st connection will be dropped or accepted depends on the HTTP server and its configuration. – CodeCaster Dec 27 '13 at 11:26
  • ok thank you. off-question please : I saw your article about http://i.stack.imgur.com/43313.png . my question is what part on the MVC allows the view to know the model ? I mean : where the reference to the model is made ? (which allows you to write `Model.Model.CustomItems` {new to mvc} ) – Royi Namir Dec 27 '13 at 11:29
  • You put that in a `@model` directive on top of your view. The http://www.asp.net/mvc/ site has some great MVC tutorials, and I've got to finish those follow-up articles... – CodeCaster Dec 27 '13 at 11:30
  • I'm sorry but I didnt understand this point : _a client will make less simultaneous connections when using keep-alive_..... are you saying that a client can only make 1 keep-alive connection ? can u elaborate please ? – Royi Namir Dec 27 '13 at 17:12
  • 1
    @Royi no, I don't know how many keep-alive connections a browser makes to a given host and I didn't mean to say a browser will only open one. The amount of requests made simultaneously is [limited and varies per browser](http://stackoverflow.com/questions/985431/). I meant that if a browser uses keep-alive connections, it may instead of fire `N` requests over `N` connections (as by default the connection gets closed after every response), for example fire `N` requests over `N / M` or even just `M` connections, because it can fire multiple requests over each opened connection, so can use less. – CodeCaster Dec 27 '13 at 17:17
  • @Royi no, I'm sorry. What is your question? – CodeCaster Dec 27 '13 at 19:26
  • I still dont understand this : _a client will make less simultaneous connections when using keep-alive_. ( and the parallel and serial stuff). If im a client and I open a keep aplive connection , to me , it is a normal connection. all the connections of the html resources can use that connection ....no ? (im not talking about the simulationous connection for FF,IE etc)- they can open a new keep alive connection. so i dont see where _a client will make less simultaneous connections when using keep-alive_ fits in – Royi Namir Dec 27 '13 at 19:29
  • I now see what you're referring to, and I think instead of _"will"_ I [should've said](http://stackoverflow.com/questions/20592698/keep-alive-header-clarification/20799796?noredirect=1#comment31182747_20799796) _"can"_. It all **depends on how the server and browser implement it**, none of this is described by the HTTP RFC. Like I linked, browsers keep an arbitrary connection limit per host. If any given HTML page requires only a handful resources (CSS, JS, images), the browser _can_ choose to download those over the initial TCP connection when using keep-alive. But it might as well open more. – CodeCaster Dec 27 '13 at 19:38
  • is my assumption is right ? : ----> if a server can handler 100 connections ( simultaneously) , and 100 users opened a connection ( keep alive) , and **nothing is going through the wire for 5 minutes** ------ at those five minutes -- **no** new (other) connections can be established....right ? to the server it's just like : " this connection is already kept for john , paul ,.... (although they not transmitting nothing for the last 5 minutes)" – Royi Namir Dec 27 '13 at 19:43
  • Again, that depends on the server implementation and configuration. See http://tools.ietf.org/id/draft-thomson-hybi-http-timeout-01.html#rfc.section.1.1 again: _"Management of idle HTTP connections has an impact on long-lived communications between hosts. Hosts are able to close idle connections in order to reduce resource consumption."_ – CodeCaster Dec 27 '13 at 19:51
  • p.s. why did you say that the client _will fire the requests in serial, not parallell_ ? hows that relates to keep alive ? the only relation i see is that it wont have to open a new connection. but how's that relates to parallel/serial ? – Royi Namir Dec 27 '13 at 19:54
  • When `N` requests are fired at the same time, that is called parallell. If you fire `N` requests one after each other, that is called serial. However, HTTP allow for multiple connections, regardless keep-alives being used, so it can even serially fire requests in a parallell manner. – CodeCaster Dec 27 '13 at 19:59
  • 1
    I know that.(:-)) you said in your comment : _a client will make less simultaneous connections when using keep-alive, it will fire the requests in serial, not parallell_ . I just dont understand how it relates to keepalive. – Royi Namir Dec 27 '13 at 20:00
  • Because the browser can fire multiple HTTP requests per keep-alive connection (serial), it _can_ need less parallell connections and _will most likely_ use less connections than without keep-alive. – CodeCaster Dec 27 '13 at 20:03
  • 5
    E, C, B don't save sessions. Those are routers, they don't have any session table and they don't need to, because multiple packets from a same TCP client-to-server session may follow different paths. The role of the router is to choose the best IP path and forward the packet accordingly, so it doesn't go up to transport layer (TCP/UDP), nor it go the application layer to see the keep-alive header. So basically keep-alive is explicitly between the client and the server, and implicitly it lets session aware devices -e.g. firewalls- opened to that explicit client-to-server session – Amine Kadimi Jan 04 '16 at 08:22
  • @Amine you're absolutely right about that. I'll see if I can edit that in someday. Of course on-premise routers that do NAT _do_ save a session table. – CodeCaster Jan 04 '16 at 08:32
  • Normally HTTP connections are kept-alive, not closed, since HTTP 1.1, which is a long time ago. – user207421 Feb 07 '17 at 00:50
  • if I use keep alive can I still send headers with each GET and POST ? I need to put some authentication key and related data (which is different for each request) in the headers – ycomp Aug 28 '17 at 11:36