Why this lab exists
Every time your browser opens a tab, your operating system makes a DNS query. Before the first TCP byte of HTTP can be sent, before the TLS handshake begins, before a CDN edge server is involved — DNS runs. A typical browser session triggers 50–100 DNS queries. Your JVM triggers DNS every time it calls InetAddress.getByName(). Every microservice HTTP call starts with DNS.
You’ve seen DNS mentioned in every system design interview: “use Route 53 for DNS-based load balancing”, “set a low TTL for fast failover”, “watch out for DNS caching”. But most engineers treat DNS as a black box. What actually happens between typing example.com and the IP address appearing? What is a “query”? What does “cached” mean for a protocol that runs over UDP?
This lab makes DNS transparent. You’ll parse the wire format by hand, implement the recursive delegation tree walk from scratch, and build a TTL cache that mirrors what production resolvers like BIND, Unbound, and CoreDNS do. By the end, a “DNS query” won’t be magic — it’ll be a 512-byte UDP packet you know exactly how to read.
What we’re building
A recursive DNS resolver in Go, built across three stages.
We build:
- DNS wire-format parser that handles labels, questions, and resource records
- UDP server on port 5300 that serves a hardcoded local zone
- Recursive resolver that walks root → TLD → authoritative nameserver
- TTL-based positive cache (reduces resolution to a map lookup on the hot path)
- Negative cache for NXDOMAIN responses (SOA MINIMUM field)
- CNAME chain following with loop detection (max 10 hops)
- HTTP admin API: GET /cache, DELETE /cache, GET /stats, GET /health
We explicitly skip (and why):
- DNSSEC (requires signature validation up the entire chain — a lab on its own)
- TCP fallback (DNS uses TCP for responses over 512 bytes; EDNS0 solves this more commonly)
- Concurrent cache writes beyond a simple
sync.RWMutex(a production resolver uses sharded maps) - IPv6 (AAAA records) — the wire format is identical, only the RDATA length changes
Mental model
Think of DNS as a phone book problem where the phone book is distributed across thousands of servers, and nobody knows where everyone’s number is — but everyone knows who to ask next.
When you look up api.stripe.com:
- Your OS asks its configured resolver (usually your router or ISP): “What is
api.stripe.com?” - That resolver doesn’t know. It asks a root nameserver: “Who handles
.com?” - The root server responds: “I don’t know
api.stripe.com, but.comnames are handled by Verisign at 192.5.6.30.” - The resolver asks Verisign: “Who handles
stripe.com?” - Verisign responds: “I don’t know the details, but
stripe.comis handled by ns1.stripe.com at 205.251.196.1.” - The resolver asks ns1.stripe.com: “What is
api.stripe.com?” - ns1.stripe.com responds with the actual IP.
That’s three round-trips for a cold cache. With a warm cache, it’s zero — your resolver already knows the answer and just returns it. This is why high-traffic resolvers have 95%+ cache hit rates: popular domains are looked up millions of times per TTL window, and each TTL window starts with one cold recursion that fills the cache for everyone.
The 13 root nameserver IPs are the only hardcoded thing in the entire system. Every DNS implementation in the world has them burned in. Everything else — TLD servers, authoritative servers — is discovered dynamically through the delegation chain.
Prerequisites
- Understanding of UDP vs TCP (DNS runs on UDP port 53; no connection setup, no retransmission)
- Familiarity with Go structs and byte slices
- Basic understanding of what a network port is
The HTTP/1.1 Server lab (also in this series) covers UDP/TCP concepts in detail.
Stage-by-stage build
Stage 0 DNS wire format — labels, questions, responses
- Implement ParseQuery([]byte) that extracts the header, question section name, QTYPE, and QCLASS
- Implement BuildResponse(id uint16, name string, ip [4]byte) that constructs a valid A-record response
- Implement appendName() using length-prefixed label encoding
- Start a UDP server on port 5300 that answers queries for a hardcoded zone map
- Verify with: dig @127.0.0.1 -p 5300 example.com
DNS is a binary protocol. Every query is a sequence of bytes with a fixed structure. The header is always 12 bytes. The question section starts at byte 12. There is no JSON, no newlines, no human-readable format on the wire.
The 12-byte header:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| ID (16 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|QR| Opcode |AA|TC|RD|RA| Z | RCODE (4 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| QDCOUNT | ANCOUNT |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| NSCOUNT | ARCOUNT |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- ID: a 16-bit identifier. The client picks a random ID for each query. The server copies it into the response. This is how a client matches async responses to queries — DNS over UDP has no connection state.
- QR: 0 = query, 1 = response.
- RCODE: 0 = no error, 3 = NXDOMAIN (domain does not exist).
- QDCOUNT: number of questions in the question section (almost always 1).
- ANCOUNT: number of resource records in the answer section (0 for queries).
Label encoding:
DNS names are not null-terminated ASCII strings. They’re encoded as a sequence of labels, where each label is a length byte followed by that many ASCII bytes. The sequence ends with a zero-length byte (the root label).
api.example.com. encodes as:
[3] a p i [7] e x a m p l e [3] c o m [0]In Go:
func appendName(buf []byte, name string) []byte {
// Strip trailing dot: "api.example.com." -> "api.example.com"
if len(name) > 0 && name[len(name)-1] == '.' {
name = name[:len(name)-1]
}
start := 0
for i := 0; i <= len(name); i++ {
if i == len(name) || name[i] == '.' {
label := name[start:i]
buf = append(buf, byte(len(label)))
buf = append(buf, []byte(label)...)
start = i + 1
}
}
return append(buf, 0) // root label
}Parsing is the reverse: read a byte, if it’s zero you’re done, otherwise it’s the label length and the next N bytes are the label text. There’s one complication: compression pointers. If the top two bits of a length byte are both 1 (the byte is 0xC0 or higher), the next byte forms a 14-bit offset into the packet where the rest of the name continues. This saves space in responses that repeat the same domain name many times (every answer record includes the name).
v0 key lesson: DNS is UDP. The entire query fits in one 512-byte datagram. There are no connections, no sessions, no retransmissions at the protocol level. The client sends a UDP packet with a query ID; the server sends a UDP packet with the same ID as the response. If the packet is lost, the client retransmits after a timeout. If two responses arrive (network weirdness), the client ignores the second one. This statelessness is why DNS can serve millions of queries per second from a small number of servers.
Stage 1 Recursive resolution — root to TLD to authoritative
- Implement queryNameserver(addr, name, qtype) that sends a UDP DNS query and returns the parsed response
- Implement recurse(name, qtype, nameservers) that follows the delegation chain
- Extract NS records from the authority section and A records from the additional section (glue)
- Implement TTL-based caching: map[string]*cacheEntry with expiresAt
- Test: resolve a real domain (e.g., golang.org) with root server 198.41.0.4
The recursive resolver’s job is to answer a query the local zone doesn’t have. It does this by walking the delegation tree.
The delegation tree:
Every domain name has a chain of authority. For api.stripe.com:
. ← root (13 anycast IPs, hardcoded)
└── com. ← TLD server (run by Verisign)
└── stripe.com. ← SLD server (run by Stripe, or their DNS provider)
└── api.stripe.com. ← the leaf record we wantWhen our resolver asks the root for api.stripe.com, the root doesn’t have the answer. It returns an NS record (nameserver delegation) pointing to Verisign’s servers, along with A records for those NS names in the additional section (called “glue records”). Without glue, resolving the NS name would require another DNS lookup — circular dependency.
;; AUTHORITY SECTION:
com. 172800 IN NS a.gtld-servers.net.
;; ADDITIONAL SECTION:
a.gtld-servers.net. 172800 IN A 192.5.6.30Our resolver extracts the glue IP and queries Verisign at 192.5.6.30:53. Verisign returns another NS delegation pointing to Stripe’s nameservers. We query those. Finally, Stripe’s authoritative server returns the A record we wanted.
The TTL cache:
Each resource record includes a TTL (time-to-live) in seconds. After caching the response, the resolver sets expiresAt = time.Now().Add(time.Duration(ttl) * time.Second). On cache lookup, if time.Now().After(entry.expiresAt), the entry is expired and we re-resolve.
type cacheEntry struct {
records []DNSRecord
negative bool // true = NXDOMAIN
expiresAt time.Time
}The cache key is "name:type" (e.g., "golang.org.:1" for an A record). This correctly separates A, AAAA, MX, and other record types for the same name.
Why the cache is everything:
A recursive resolver without a cache would perform 3–5 UDP round-trips for every single query. At network latencies of 10–100ms per hop, that’s 30–500ms for a fresh resolution. With a 95% cache hit rate (typical for a busy resolver), 95% of queries are answered in under 1ms from memory. The cache is not an optimization — it’s the entire reason the DNS system works at scale.
Popular domain TTLs:
google.com ATTL: 300 seconds (5 min)cloudflare.com ATTL: 300 secondsamazon.com ATTL: 2700 seconds (45 min)twitter.com ATTL: 60 seconds (fast failover)
During one 300-second window, if google.com is queried 10,000 times, only 1 resolution involves upstream round-trips. The other 9,999 are cache hits.
Stage 2 Negative caching and CNAME following
- Implement negative caching: when a response has RCODE=3 (NXDOMAIN), cache it with the SOA MINIMUM TTL
- Implement CNAME following: if the answer section contains a CNAME record, recursively resolve the CNAME target
- Implement loop detection: track hop count and return an error after 10 hops
- Test: inject a CNAME chain of depth 3 and verify all A records are returned
- Test: inject a circular CNAME (a -> b -> a) and verify the loop error fires
Negative caching:
When a domain doesn’t exist, the authoritative server responds with RCODE=3 (NXDOMAIN). Without negative caching, every query for nosuchdomain.example.com would recurse all the way to the authoritative server, only to get the same NXDOMAIN back. This is a real attack surface — amplifying load on the authoritative server with repeated queries for nonexistent names.
RFC 2308 (1998) mandated negative caching. The TTL for a negative entry comes from the SOA MINIMUM field in the authority section of the NXDOMAIN response. Typical negative TTLs are 300–3600 seconds.
// Cache a negative entry: domain does not exist for TTL seconds
func (r *Resolver) cacheNegative(key string, ttl uint32) {
r.mu.Lock()
defer r.mu.Unlock()
r.cache[key] = &cacheEntry{
negative: true,
expiresAt: time.Now().Add(time.Duration(ttl) * time.Second),
}
r.stats.NXDOMAINs++
}CNAME records:
A CNAME (Canonical Name) record says “this name is an alias for another name”. For example:
www.example.com. 300 IN CNAME example.com.
example.com. 300 IN A 93.184.216.34When a resolver asks for www.example.com, the authoritative server returns the CNAME record. The resolver must then resolve example.com (the CNAME target) to find the actual IP. A resolver that returns only the CNAME without following it is useless to clients.
CNAME chains can be multiple hops deep. Real-world example:
www.shopify.com → shops.myshopify.com → shops.shopify.io → 23.185.0.1Each arrow is one CNAME record, and each requires a new resolution. Our resolver follows them recursively up to maxCNAMEHops = 10.
CNAME loop detection:
A misconfigured zone can create circular CNAME references: a.example.com → b.example.com → a.example.com. Without a hop counter, the resolver would recurse infinitely and exhaust its stack. The fix is simple:
func (r *Resolver) resolveWithDepth(name string, qtype uint16, depth int) ([]DNSRecord, error) {
if depth > maxCNAMEHops {
return nil, errors.New("dns: CNAME chain too long (max 10 hops)")
}
// ... resolve logic ...
if answer is CNAME {
return r.resolveWithDepth(cnameTarget, qtype, depth+1)
}
}RFC 1034 originally said resolvers should handle CNAME chains gracefully. RFC 8499 (2019) defined “CNAME chain” formally. In practice, public authoritative servers rarely have chains longer than 3 hops, but defensive programming requires checking.
Run it yourself
labs/dns-resolver ↗
Go 1.23+dig (part of dnsutils/bind-utils) - 1 Clone and build
git clone https://github.com/pushkarkumar-dev/system-design-labs cd system-design-labs/labs/dns-resolver go build ./... - 2 Start the DNS server (UDP :5300 + HTTP admin :5380)
go run ./cmd/server --udp 0.0.0.0:5300 --admin 0.0.0.0:5380 - 3 Query the local zone
dig @127.0.0.1 -p 5300 example.com - 4 Query a real domain (recursive resolution — requires outbound UDP 53)
dig @127.0.0.1 -p 5300 golang.org - 5 Inspect the cache
curl -s http://localhost:5380/cache | jq . - 6 View stats
curl -s http://localhost:5380/stats | jq . - 7 Run tests
go test ./pkg/dns/ -v - 8 Run benchmarks
go test -bench=. -benchmem ./pkg/dns/
; <<>> DiG 9.18 <<>> @127.0.0.1 -p 5300 example.com
;; QUESTION SECTION:
;example.com. IN A
;; ANSWER SECTION:
example.com. 300 IN A 93.184.216.34
;; SERVER: 127.0.0.1#5300(127.0.0.1) (UDP)
;; MSG SIZE rcvd: 56 Benchmarks
M2 MacBook Pro, 16GB RAM | Metric | Result | Notes |
|---|---|---|
| Cache hit query resolution | 450,000 queries/sec | estimated — pure in-memory map lookup, no I/O |
| DNS packet parse throughput | 2,000,000 packets/sec | estimated — binary parsing of 512-byte UDP packets |
| Cache miss + full recursion | 20 queries/sec | estimated — RTT-bound: 3 round trips to nameservers |
| CNAME chain (3 hops) | 3 RTTs | each hop adds one upstream resolution |
| Cache size for 100k domains | 12 MB | ~120 bytes per entry: name + IP + expiry metadata |
Stress tests
At 10× load (~4,500 cache-hit queries/sec): The resolver handles this comfortably. The hot path is a sync.RWMutex read lock + map lookup — both are extremely fast. At this rate, the bottleneck is network I/O (UDP recv buffer), not the resolver logic.
At 100× (45,000 queries/sec): The single sync.RWMutex becomes the bottleneck. Multiple goroutines contend on the read lock. Production resolvers use sharded caches (e.g., 256 shards, each with its own mutex) to eliminate contention. Our toy resolver would degrade here.
NXDOMAIN storm: If a client fires 10,000 queries per second for random nonexistent subdomains (abc123.example.com, def456.example.com…), the negative cache fills but each unique name is a miss. Without negative caching, each miss would forward to the authoritative server — effectively a DDoS. With negative caching after the first resolution of each unique name, subsequent identical queries are cache hits. Random-subdomain queries (NXNS attack pattern) still cause one upstream query per unique name and are a real production concern.
Cache eviction: Our cache never evicts. It only expires entries when they’re read. Under memory pressure, a real resolver implements an LRU eviction policy so the cache doesn’t grow unbounded. Our resolver would eventually OOM if given enough unique domains with long TTLs.
Packet truncation test: Sending a 6-byte packet (shorter than the 12-byte header) returns an error from ParseQuery. The server logs the error and drops the packet — correct behavior. Malformed inputs should never cause panics.
What the toy misses
- DNSSEC validation DNSSEC signs zone data with asymmetric cryptography. Our resolver accepts any answer from any nameserver with no verification. A DNS cache poisoning attack (Kaminsky attack) can forge authoritative responses and poison our cache. A DNSSEC-validating resolver checks signatures at every step up to the IANA root — making forgery computationally infeasible.
- DNS over TLS (DoT) and DNS over HTTPS (DoH) Plain DNS queries travel unencrypted on UDP port 53. An ISP, corporate firewall, or network observer can see every domain you look up. DoT (RFC 7858, port 853) and DoH (RFC 8484, HTTPS) encrypt queries. Our resolver speaks only plain UDP — adding TLS would require wrapping each connection in a TLS stream, which changes the concurrency model significantly.
- EDNS0 and responses larger than 512 bytes Classic DNS (RFC 1035) limits UDP responses to 512 bytes. EDNS0 (RFC 6891) extends this with an OPT pseudo-record that advertises the client's buffer size (typically 1232 or 4096 bytes). DNSSEC responses frequently exceed 512 bytes. Our resolver doesn't send or parse OPT records, so responses to DNSSEC-enabled zones would be silently truncated.
- Zone transfers (AXFR/IXFR) Secondary nameservers replicate their zones from a primary via AXFR (full transfer) or IXFR (incremental transfer). Our server is a pure recursive resolver — it only queries other nameservers, never serves zone data to secondaries. A real authoritative server would implement AXFR over TCP and TSIG authentication.
- Anycast routing and the root server network The 13 root nameserver IPs have been the same since 1997. Behind each IP are hundreds of physical servers worldwide operated via BGP Anycast — your packet goes to the nearest server, not a specific machine. Our resolver hardcodes 198.41.0.4 and can only reach whatever physical server the network routes to. Adding redundancy (query multiple roots in parallel, take the first answer) would improve reliability but adds significant complexity.
- Split-horizon DNS Kubernetes clusters, corporate VPNs, and cloud environments use split-horizon DNS: the same domain resolves to different IPs depending on whether the query comes from inside or outside the network (e.g., an internal service IP vs a public load balancer IP). Our resolver has a single flat zone file. Real resolvers support views — separate zone data per client IP range.
Wiring it into a Java/Spring service
Wiring it into a Java/Spring service
dnsjava:dnsjava:3.5.3 org.springframework.boot:spring-boot-starter-web:3.3.0 org.springframework.boot:spring-boot-starter-actuator:3.3.0 labs/dns-resolver/java-integration Clone and run: cd labs/dns-resolver/java-integration && mvn spring-boot:run Part A — DnsAdminClient: the HTTP bridge
The Go resolver exposes an HTTP admin API on port 5380. DnsAdminClient.java wraps it using Spring Framework 6.1’s RestClient.
public class DnsAdminClient {
private final RestClient http;
public DnsAdminClient(String adminUrl) {
this.http = RestClient.builder()
.baseUrl(adminUrl)
.defaultHeader("Accept", MediaType.APPLICATION_JSON_VALUE)
.build();
}
public List<CacheEntry> getCache() {
var result = http.get()
.uri("/cache")
.retrieve()
.body(new ParameterizedTypeReference<List<CacheEntry>>() {});
return result != null ? result : List.of();
}
public void clearCache() {
http.delete().uri("/cache").retrieve().toBodilessEntity();
}
public DnsStats getStats() {
return http.get().uri("/stats").retrieve().body(DnsStats.class);
}
// Java 16 records — immutable DTOs, zero boilerplate
public record CacheEntry(String key, boolean negative, Instant expiresAt, int recordCount) {}
public record DnsStats(long queries, long cacheHits, long cacheMisses, long nxdomains) {}
}Part B — DnsJavaComparison: our resolver vs dnsjava
This is where the learning happens. dnsjava is the production-grade Java DNS library. Its Lookup class does everything our Go resolver does — recursion, caching, CNAME following — plus DNSSEC validation, EDNS0, and proper TSIG support.
The key insight: you can point dnsjava’s Lookup at any resolver, including ours. This lets you compare our resolver’s behavior against the full dnsjava stack using the exact same API:
// Query our Go resolver
public List<String> resolveWithOurResolver(String domain) throws Exception {
Resolver resolver = new SimpleResolver(ourResolverHost);
resolver.setPort(ourResolverPort); // 5300
Lookup lookup = new Lookup(domain, Type.A);
lookup.setResolver(resolver);
var records = lookup.run();
// ...
}
// Query the system resolver (typically 8.8.8.8 or your router)
public List<String> resolveWithSystemResolver(String domain) throws Exception {
Lookup lookup = new Lookup(domain, Type.A);
// No setResolver() call — uses OS default
var records = lookup.run();
// ...
}What you can observe:
- Our resolver answers zone queries (example.com) immediately. The system resolver makes a real outbound UDP query.
- After
clearCache(), re-queryingexample.comthrough our resolver resets the counter. The system resolver has its own independent cache. - Our resolver logs every delegation step to stdout — you can see “asking root, got NS, asking Verisign, got NS, asking authoritative…” in real time.
What dnsjava does that we don’t:
dnsjava handles EDNS0 automatically (appends an OPT record to every query advertising a 4096-byte buffer). It handles truncated responses by retrying over TCP. It supports TSIG for authenticated zone transfers. Its cache is thread-safe with proper LRU eviction. Our resolver has none of these.
application.yml
dns-lab:
resolver-host: 127.0.0.1
resolver-port: 5300
admin-url: http://localhost:5380Run it
# Terminal 1: start the Go DNS server
cd labs/dns-resolver
go run ./cmd/server --udp 0.0.0.0:5300 --admin 0.0.0.0:5380
# Terminal 2: run the Spring Boot demo
cd labs/dns-resolver/java-integration
mvn spring-boot:runExpected output:
=== DNS Resolver Spring Integration Demo ===
Go resolver health: ok
Stats before: queries=0 hits=0 misses=0
Resolving 10 queries (includes repeats to show cache effect):
example.com → [93.184.216.34]
lab.example.com → [10.0.0.1]
alias.example.com → [10.0.0.2]
example.com → [93.184.216.34]
example.com → [93.184.216.34]
...
Stats after: queries=10 hits=0 misses=0
Live cache entries: 0
Clearing cache...
Cache entries after flush: 0
dnsjava comparison for 'example.com':
Our resolver: [93.184.216.34]
System resolver: [93.184.216.34]
JDK resolver: 93.184.216.34
Demo complete.The stats show 0 hits because local zone answers bypass the cache lookup path — zone answers are authoritative and don’t age out. Cache hits accumulate when recursive resolutions are cached and queried again within their TTL window.
What surprised me
Where to go deeper
- RFC 1035 (1987): the original DNS specification — still the primary reference for wire format and label encoding. Remarkably readable for a 1987 RFC.
- RFC 2308 (1998): Negative Caching of DNS Queries — defines how NXDOMAIN should be cached and for how long.
- RFC 8499 (2019): DNS Terminology — clarifies the difference between “recursive resolver”, “authoritative nameserver”, “stub resolver”, and other terms that have accumulated over 35 years.
- dnsjava: dnsjava.org — the production Java DNS library; study its
Lookup,Cache, andResolversource code for a complete recursive resolver implementation. - Unbound: nlnetlabs.nl/unbound — high-performance recursive resolver written in C; the reference open-source implementation for what a production resolver looks like.
- CoreDNS: coredns.io — the DNS server used by Kubernetes, written in Go; excellent reference for how to structure a production Go DNS server.
- Cloudflare’s “What is DNS?”: an exceptionally clear explanation of the delegation tree with diagrams — cloudflare.com/learning/dns.
Related labs and questions
- Next in networking: HTTP/1.1 Server lab — once DNS returns an IP, HTTP takes over; the two labs together cover the full request path from URL to response
- Related distributed systems: Consistent Hashing lab — DNS-based load balancing (Route 53 weighted records) is one use case for consistent hashing
- System design question: “Design a global DNS service like Route 53” — answer: hierarchical nameservers, anycast for latency, health-check integration for failover, TTL tuning for the consistency-vs-freshness tradeoff
- Follow-up question: “What happens to in-flight TCP connections when a DNS record changes?” — nothing, because TCP connections are keyed by IP not by name; the DNS change only affects new connections after the TTL expires