Caching Responses

dnsdist implements a simple but effective packet cache, not enabled by default. It is enabled per-pool, but the same cache can be shared between several pools. The first step is to define a cache, then to assign that cache to the chosen pool, the default one being represented by the empty string:

pc = newPacketCache(10000, 86400, 0, 60, 60)

The first parameter (10000) is the maximum number of entries stored in the cache, and is the only one required. All the other parameters are optional and in seconds. The second one (86400) is the maximum lifetime of an entry in the cache, the third one (0) is the minimum TTL an entry should have to be considered for insertion in the cache, the fourth one (60) is the TTL used for a Server Failure or a Refused response. The last one (60) is the TTL that will be used when a stale cache entry is returned.

For performance reasons the cache will pre-allocate buckets based on the maximum number of entries, so be careful to set the first parameter to a reasonable value. Something along the lines of a dozen bytes per pre-allocated entry can be expected on 64-bit. That does not mean that the memory is completely allocated up-front, the final memory usage depending mostly on the size of cached responses and therefore varying during the cache’s lifetime. Assuming an average response size of 512 bytes, a cache size of 10000000 entries on a 64-bit host with 8GB of dedicated RAM would be a safe choice.

The setStaleCacheEntriesTTL() directive can be used to allow dnsdist to use expired entries from the cache when no backend is available. Only entries that have expired for less than n seconds will be used, and the returned TTL can be set when creating a new cache with newPacketCache().

A reference to the cache affected to a specific pool can be retrieved with:


And removed with:


Cache usage stats (hits, misses, deferred inserts and lookups, collisions) can be displayed by using the PacketCache:printStats() method:


Expired cached entries can be removed from a cache using the PacketCache:purgeExpired() method, which will remove expired entries from the cache until at most n entries remain in the cache. For example, to remove all expired entries:


Specific entries can also be removed using the PacketCache:expungeByName() method:

getPool("poolname"):getCache():expungeByName(newDNSName(""), dnsdist.A)

Finally, the PacketCache:expunge() method will remove all entries until at most n entries remain in the cache: