Prepin
Log in

Create a DNS cache.

Approach / Explanation

In this question, the interviewer aims to assess your understanding of system design, particularly in relation to DNS cache design. A DNS (Domain Name System) cache is a temporary storage system that a computer's operating system maintains, containing records of recent website visits and attempts to access various internet domains. You are expected to propose a design that efficiently stores these lookups and allows for their retrieval as needed. The primary purpose of a DNS cache is to alleviate the burden on DNS servers by retaining responses from previous requests. When devising your design, consider the following aspects: Cache size Management of cache evictions when the cache reaches capacity Thread safety Latency reduction

Suggested Answer

A possible design for a DNS cache might involve utilizing a hashmap in conjunction with a doubly linked list, forming a Least Recently Used (LRU) cache. In this setup, the hashmap would hold the IP addresses, while the doubly linked list would track the order of usage. The cache would have a predetermined size, and when it reaches capacity and a new entry needs to be added, the least recently used entry would be evicted from the cache. To ensure thread-safe request handling, a lock can be implemented prior to executing any operations. However, this approach may result in race conditions and delays, as multiple threads attempt to acquire the lock. An alternative solution is to employ Read-Write locks, which allow several threads to read simultaneously while restricting write access to just one thread. Given that reads occur more often than writes, this method enhances parallelism. Additionally, to minimize latency further, we could implement prefetching, which involves retrieving certain entries in advance based on historical data and anticipated usage patterns.

Alternative Answer

Another option to explore is the LFU (Least Frequently Used) cache design. The DNS cache could implement a Hashmap for constant-time lookups and a min heap to efficiently retrieve the least frequently used item. When a DNS query occurs, we update both the hashmap and the heap. If the entry already exists, we increase the frequency and refresh the heap. If the entry is new and the cache size has reached its limit, we will remove the least frequently used entry from both the hashmap and heap before adding the new entry. This DNS cache must be thread-safe. To minimize lock contention and enhance parallelism, we can use sharding based on keys. By applying locks to individual shards rather than the entire structure, we can improve efficiency. Moreover, to alleviate the load on a DNS server and lower latency, Time-To-Live (TTL) can be employed. TTL is a method that restricts the duration that data remains in the computer or network, ensuring that outdated or unnecessary data is purged from the cache, thus making space for new entries. An appropriately set TTL value can help maintain the most relevant entries in the cache. Keep in mind that the choice of a specific method over others is largely influenced by the precise requirements and limitations of your system. Always weigh the trade-offs of various approaches and select the one that suits your needs best.

Question Details

Difficulty & Category

Medium
system design

Companies

Meta (Facebook)

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.