[Special Summer Sale] 40% OFF All Magento 2 Themes

Cart

What's the cause of the error 'getaddrinfo EAI_AGAIN'?

  • This topic is empty.
Viewing 16 post (of 16 total)
  • Author
    Posts
  • #9274
    marcos-casagrande
    Participant

    For those who perform thousand or millions of requests per day, and need a solution to this issue:

    It’s quite normal to get getaddrinfo EAI_AGAIN errors when performing a lot of requests on your server. Node.js itself doesn’t perform any DNS caching, it delegates everything DNS related to the OS.

    You need to have in mind that every http/https request performs a DNS lookup, this can become quite expensive, to avoid this bottleneck and getaddrinfo errors, you can implement a DNS cache.

    http.request (and https) accepts a lookup property which defaults to dns.lookup()

    http.get('http://example.com', { lookup: yourLookupImplementation }, response => {
        // do something here with response
    });
    

    I strongly recommend to use an already tested module, instead of writing a DNS cache yourself, since you’ll have to handle TTL correctly, among other things to avoid hard to track bugs.

    I personally use cacheable-lookup which is the one that got uses (see dnsCache option).

    You can use it on specific requests

    const http = require('http');
    const CacheableLookup = require('cacheable-lookup');
    
    const cacheable = new CacheableLookup();
    
    http.get('http://example.com', {lookup: cacheable.lookup}, response => {
        // Handle the response here
    });
    

    or globally

    const http = require('http');
    const https = require('https');
    const CacheableLookup = require('cacheable-lookup');
    
    const cacheable = new CacheableLookup();
    
    cacheable.install(http.globalAgent);
    cacheable.install(https.globalAgent);
    

    NOTE: have in mind that if a request is not performed through Node.js http/https module, using .install on the global agent won’t have any effect on said request, for example requests made using undici

Viewing 16 post (of 16 total)
  • You must be logged in to reply to this topic.