Design a DNS Resolver with health check, TTL support, and fast reads
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-06-14
Problem Statement
Design a simplified in-memory DNS Resolver system that maps domain names (e.g., example.com) to IP addresses (e.g., 93.184.216.34).
The resolver should support fast lookups, TTL-based cache expiry, and periodic health checks to ensure that cached IPs remain valid.
✅ Functional Requirements
1.Resolve domain name
•When a domain name is requested:
•If present and valid (not expired) in cache → return cached IP.
•Otherwise → fetch IP from an upstream DNS (simulate it), store it in cache with TTL, and return it.
2.Cache with TTL (Time To Live)
•Each record has a TTL value (in seconds).
•After TTL expires, the entry should be invalidated or refreshed.
3.Health Check
•Periodically check the health of cached IPs (e.g., by ping or simulated check).
•If an IP becomes unhealthy → remove it or mark as invalid.
4.Fast Reads
•Reads should be O(1) or near constant time.
•Use efficient in-memory data structures for lookups (like a HashMap / Trie).
5.Concurrency Safety
•Multiple threads can query DNS simultaneously.
•Reads should not block each other; writes/updates must be thread-safe.
🚫 Non-Functional Requirements
•High performance lookups.
•Scalable to handle thousands of domains.
•Thread-safe cache and update mechanism.
•Minimal memory footprint.
•Extensible to support multiple upstream DNS servers.
🧱 Expected Components / Classes
You should define (suggested):
DNSResolver
•Main entry point for resolving domains.
•Interfaces with cache and upstream DNS.
DNSCache
•Stores domain → DNSRecord mapping.
•Handles TTL expiry logic.
DNSRecord
•Represents a record with domain name, IP address, TTL, expiry timestamp, and health status.
HealthChecker
•Background service that periodically verifies cached IPs.
UpstreamDNS
•Simulates or mocks real DNS lookup (e.g., returns IPs for testing).
Scheduler / ThreadPool
•For running TTL expiry and health checks asynchronously.
🧩 Example Flow
1.User requests resolve("google.com").
2.Resolver checks cache:
•If record exists and not expired → return cached IP.
•Else → query UpstreamDNS, store new record with TTL=300s.
3.HealthChecker periodically validates each record.
4.If IP is unreachable or TTL expired → remove or refresh it.
5.Future lookups return healthy, up-to-date results instantly.
🧪 Example Input / Output
Input:
1
2resolve("openai.com")
3resolve("openai.com")
4(wait 5 mins)
5resolve("openai.com")Output (simulated):
1
2Fetching from upstream: openai.com -> 13.66.199.210
3Cache hit: openai.com -> 13.66.199.210
4TTL expired: refreshing openai.com
5Fetching from upstream: openai.com -> 13.66.199.220⚙️ Constraints
•DNS record count ≤ 10^5
•TTL range: 30s – 3600s
•Health check frequency: configurable (default every 60s)
•Thread-safe operations required.
🎯 Goals
Your design should be:
•Modular (easy to extend with caching policies like LRU)
•Thread-safe and high-performance
•Capable of easy testing (mock upstream responses)