Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between keep_alive and persistent option for HTTP request?

Can someone explain us the difference in behaviour between the following parameters :

keep_alive parameter in Zend_Http_Client class ?

and

persistent in Zend_Http_Client_Adapter_Socket class ?

I'd like to understand what I need to do to keep a bunch of HTTPS connection open (to avoid the negociation of the SSL).

Thanks, Gaston

like image 775
Gaston Annebicque Avatar asked Jan 12 '11 17:01

Gaston Annebicque


People also ask

What is the use of persistent connection?

Persistent connections between a web client and a server can be reused for more than one exchange of a request and a response. Persistent connections improve network performance because a new connection does not have to be established for each request.

What is Keep-Alive request?

HTTP keep-alive, a.k.a., HTTP persistent connection, is an instruction that allows a single TCP connection to remain open for multiple HTTP requests/responses. By default, HTTP connections close after each request.

What does Keep-Alive do?

Enabling Keep-Alive can help to optimize website's performance and deliver a better user experience. It allows a visitor's browser to reuse a single TCP connection to load page content. Keep-Alive is usually enabled by default on your origin server.

What is a persistent TCP connection?

Persistent connections provide a mechanism by which a client and a server can signal the close of a TCP connection. This signaling takes place using the Connection header field (section 14.10). Once a close has been signaled, the client MUST NOT send any more requests on that connection.


1 Answers

If you use the persistent connection ou should use as well the keep-alive, as without keep Alive HTTP 1.1 connexion your persitent will have to do a lot of work to emulate the job.

Edit : (it was time to eat)

Keepalive settings talks about a quite-short time setting, set by the server. Apache by default handle 15s for Keep-Alive requests, but a current optimized setting is 5s. This is mostly done to help HTTP client downloading js and css attached to a page in the same HTTP connexion. If you can adjust the server settings you can try longest Keep-Alive queries (but be careful, this will limit seriously the number of client accepeted by your server).

Persistent connexion mode is done to really emulate a long-term persistent connexion, the socket opened is not closed at the end of the script. You should be very careful with such setting. Are you in CLI mode? FCGI?. If you're running in an apache process I'm really not sure you'll get the same connexion on the next request on this script (which will may be handle by another apache process), it's even worst if your code is running on several apache servers in a large deployment. And this is for the client (PHP) side, but it can be as well a big pain for the targeted server.

Re-edit: (as something about SSL must be said)

Are you sure you need to optimize SSL negociation time? SSL use Cache, at least on server side, to limit the negociation to the first request. Client side caching of SSL session is maybe done by the PHP stream_socket_client function (which is used by the Zend class. If not you could test a new class from your own (just need to implement the interface) and try using curl, as curl use SSL session caching by default.

like image 169
regilero Avatar answered Sep 30 '22 22:09

regilero