DNS Caching

You may notice that code using the cURL extension appears to run faster than code using streams. The reason for this is that cURL implements its own DNS cache, which is more likely to be apparent if your operating system or internet service provider does not provide one.

DNS, or Domain Name System, is a system used to derive an IP address for a domain name in a manner similar to how phone directories are used to obtain a phone number for a person using their name. The process of obtaining an IP address for a domain name, called a DNS lookup, can be a costly operation in terms of the time required.

Because the results of DNS lookups don't change often, DNS caching is often used to retain the results of lookups for a certain time period after they are performed. This can be done at multiple levels including the source code level as with cURL, natively at the OS level, or via software like nscd or dnsmasq run either locally or on remote servers such as those used by internet service providers.

cURL DNS caching is enabled by default. Some situations like debugging may warrant disabling it, which can be done by setting CURLOPT_DNS_USE_GLOBAL_CACHE to false. cURL will also by default retain the results of DNS lookups in memory for two minutes. To change this, set the CURLOPT_DNS_CACHE_TIMEOUT setting to the number of seconds a result should remain in the cache before expiring.

Also noteworthy is the fact that cURL DNS caching is not thread-safe. Threading is a particular style of parallel processing. The most common implementation of threading consists of multiple threads of execution contained within a single operating system process that share resources such as memory. Because of this, it may operate unpredictably in a threaded environment such as Windows Server or *NIX running a threaded Apache MPM such as worker.

If you are using the HTTP streams wrapper or either of the PHP-based HTTP client libraries covered in this chapter and you have access to install software on your server, you may want to install a local DNS caching daemon to improve performance. Try nscd or dnsmasq on *NIX. Writing DNS caching into your own client will be covered in a later chapter on writing your own HTTP client.


© cURL Extension — Web Scraping

>>> Back to TABLE OF CONTENTS <<<
Category: Article | Added by: Marsipan (30.08.2014)
Views: 414 | Rating: 0.0/0
Total comments: 0
avatar