Home/Learn/Computer Networks/DNS — Domain Name System

DNS — Domain Name System

Beginner
Application Layer

DNS translates human-readable domain names into IP addresses. It is a globally distributed, hierarchical, cached database queried billions of times per second using a recursive resolution process.

Overview

Every time you type a URL, DNS resolves it to an IP address before a TCP connection can be made. DNS is a distributed hierarchical system: Root servers know where TLD servers are (.com, .org, .in); TLD servers know where authoritative name servers are for each domain; authoritative servers hold the actual records. Resolution is handled by a recursive resolver (usually your ISP or 8.8.8.8), which walks the hierarchy on your behalf and caches results. DNS records come in types: A (IPv4 address), AAAA (IPv6), CNAME (alias), MX (mail), TXT (verification, SPF), NS (name server), and SRV (service location). TTL controls how long records are cached — low TTL enables fast failover; high TTL reduces resolver load. For backend engineers, DNS is critical for service discovery, blue-green deployments (swap DNS records), CDN routing (GeoDNS), and health-check-based failover.

DNS Resolution Flow

A DNS query walks the hierarchy from root to TLD to authoritative server. The recursive resolver does this on the client's behalf and caches each response. The full round-trip only happens on a cache miss.

DNS resolution flow and Java DNS cache tuning
// DNS resolution for "api.example.com" (cache miss):
//
// 1. Your app asks OS resolver: "What is api.example.com?"
// 2. OS checks /etc/hosts → not found
// 3. OS checks local DNS cache → not found
// 4. OS queries recursive resolver (e.g. 8.8.8.8 or ISP resolver)
//
// Recursive resolver (8.8.8.8):
// 5. Queries Root server → "I don't know, ask .com TLD at 192.5.6.30"
// 6. Queries .com TLD server → "I don't know, ask ns1.example.com at 205.251.196.1"
// 7. Queries ns1.example.com (authoritative) → "api.example.com = 93.184.216.34, TTL=300"
// 8. Recursive resolver caches result for 300 seconds, returns to OS
//
// 9. OS caches result, returns to application
// 10. App connects to 93.184.216.34:443
//
// Total: ~100ms on first lookup, ~0ms on cache hit
// This is why DNS adds latency to first connections — connection pooling helps

// Java: DNS lookup
InetAddress addr = InetAddress.getByName("api.example.com");
System.out.println(addr.getHostAddress()); // 93.184.216.34

// JVM DNS cache — Java caches DNS results aggressively (30s default positive, forever negative)
// For cloud environments where IPs change, reduce cache TTL:
// java.security.Security.setProperty("networkaddress.cache.ttl", "10");

DNS Record Types and Engineering Uses

Different record types serve different purposes. Engineers interact with DNS for deployment (CNAME swaps), email (MX, SPF), service discovery, and health-based routing.

DNS record types and engineering deployment patterns
// Common DNS record types:
// A     — maps hostname to IPv4:        api.example.com → 93.184.216.34
// AAAA  — maps hostname to IPv6:        api.example.com → 2606:2800:220:1:248:1893:25c8:1946
// CNAME — alias to another hostname:    www.example.com → example.com
// MX    — mail exchange:                example.com → mail.example.com (priority 10)
// TXT   — arbitrary text:               _dmarc.example.com → "v=DMARC1; p=reject"
// NS    — name server:                  example.com → ns1.example.com
// SRV   — service location:             _grpc._tcp.example.com → host:port, priority, weight
// PTR   — reverse DNS (IP → hostname):  34.216.184.93.in-addr.arpa → api.example.com

// Engineering patterns:
//
// Blue-Green deployment via CNAME:
// api.example.com CNAME → blue.api.example.com  (live)
// Deploy to green, test, then update CNAME:
// api.example.com CNAME → green.api.example.com (instant cutover)
// Risk: TTL must be low (60s) before the swap

// AWS Route53 health-check failover:
// Primary: api.example.com → 1.2.3.4  (health check every 30s)
// Secondary: api.example.com → 5.6.7.8 (failover)
// If primary fails health check → Route53 automatically serves secondary record

// GeoDNS — route users to nearest region:
// api.example.com → 1.2.3.4  (for users in Asia)
// api.example.com → 5.6.7.8  (for users in Europe)

Key Points to Remember

  • 1DNS walks Root → TLD → Authoritative server hierarchy to resolve names to IPs.
  • 2Recursive resolver (8.8.8.8) does the hierarchy walk and caches results for TTL seconds.
  • 3Record types: A (IPv4), AAAA (IPv6), CNAME (alias), MX (mail), TXT (SPF/DMARC), SRV (services).
  • 4Low TTL enables fast failover; high TTL reduces DNS query load.
  • 5JVM caches DNS aggressively — tune networkaddress.cache.ttl for cloud environments.
  • 6Blue-green deployments use CNAME swaps; Route53 supports health-check-based automatic failover.

Interview Questions

Sign in to ask Aria
1

What happens at the network level when you type a URL in a browser?

MediumAmazon
2

What is the difference between an A record and a CNAME record?

EasyTCS
3

How would you use DNS to implement a blue-green deployment?

MediumThoughtWorks
4

Why can DNS TTL affect the speed of a failover?

MediumFlipkart
5

Why should you tune JVM DNS cache TTL in a cloud microservice?

MediumRazorpay

Ask Aria about DNS — Domain Name System

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…