Reusing the same curl handle. Big performance increase?

Viewed 40087

In a PHP script I am doing a lot of different curl GET requests (a hundred) to different URLs.

Will reusing the same handle from curl_init improve the performance, or is it negligible compare to the response time of the requests?

I am asking that because in the current architecture it would be not easy to keep the same handle.

6 Answers

It depends on if the urls are on same servers or not. If they are, concurrent requests to same server will reuse the connection. see CURLOPT_FORBID_REUSE.

If the urls are sometimes on same server you need to sort the urls as the default connection cache is limited to ten or twenty connections.

If they are on different servers there is no speed advantage on using the same handle.

With curl_multi_exec you can connect to different servers at a same time (parallel). Even then you need some queuing to not use thousands of simultaneous connections.

Although this question is answered correctly, I would like to add a WARNING to NOT reuse the curl for POST or PUT requests, for the resetting is not always done fully.

I just had the following issue which resulted in corrupt data in my database. :-(

Due to some corrupted ascii codes in some records, the request-post remained empty and my script did not check that :-( (I will fix this of course) The curl seemed to have the request-post from the previous record and just passed that on. No error was returned.

This would not have happened if the curl was initialized for each request. In that case there would have not been any pre-loaded data available, therefore the server would have responded with an empty-error.

So my advice, better safe than fast: always use a new curl instance, except for getting external data.

UPDATE: I just found out that I did not use the php-function curl_reset(). According to the manual that would reset everything. For now I prefer to use curl_close() and curl_init() ;-)

I hope I explained it well enough, please ask if it is not clear! Greetz

Related