DNS & CDN

How the internet routes your users to the right servers, and how CDNs bring content closer to them.

Domain Name System (DNS)

DNS is the internet's phone book. It translates human-readable domain names (like sdprimer.com) into IP addresses that machines use to route traffic. Every time a user types a URL, a DNS lookup happens before a single byte of your application code runs.

Browser
Recursive Resolver
Root NS
TLD NS (.com)
Authoritative NS
93.184.216.34
DNS resolution: recursive lookup from root to authoritative nameserver

DNS Record Types

Record Purpose Example
A Maps domain to IPv4 address sdprimer.com → 93.184.216.34
AAAA Maps domain to IPv6 address sdprimer.com → 2001:db8::1
CNAME Alias to another domain www.sdprimer.com → sdprimer.com
MX Mail server routing sdprimer.com → mail.sdprimer.com
NS Delegates to authoritative nameserver sdprimer.com → ns1.provider.com
TXT Arbitrary text (verification, SPF) v=spf1 include:_spf.google.com

DNS as a Load Distribution Tool

DNS isn't just name resolution — it's your first opportunity to distribute traffic. Managed DNS services like Route 53, Cloudflare DNS, or Google Cloud DNS offer:

  • Weighted routing: Send 90% of traffic to your primary and 10% to a canary deployment.
  • Latency-based routing: Direct users to the geographically closest data center.
  • Failover routing: Automatically redirect traffic when health checks detect an outage.
  • Geolocation routing: Route users based on their country or region (useful for compliance or localization).
TTL matters. DNS responses are cached by resolvers and browsers based on the TTL (Time to Live) you set. A high TTL (e.g., 86400s = 24hr) reduces DNS lookup latency but makes changes slow to propagate. A low TTL (e.g., 60s) lets you failover quickly but increases DNS query volume. Before a migration or failover setup, lower your TTL well in advance.

Content Delivery Networks (CDN)

A CDN is a globally distributed network of edge servers that cache and serve content close to your users. Instead of every request traveling to your origin server (which might be in a single region), the CDN serves static assets — images, CSS, JavaScript, videos — from the nearest edge node.

Origin Server (us-east-1)
↓ distributes to ↓
Edge: Tokyo
Edge: London
Edge: Sydney
Edge: Mumbai
↓ serve users locally ↓
User JP
User UK
User AU
User IN
Users hit the nearest edge server instead of crossing the globe to your origin.

Push CDN vs Pull CDN

Push CDN

You explicitly upload content to the CDN. You control exactly what gets cached and when it's updated.

  • Best for: content that changes infrequently
  • Pro: full control over cache contents
  • Con: you manage upload + invalidation
Pull CDN

CDN fetches content from your origin on the first request, then caches it. Subsequent requests are served from cache until TTL expires.

  • Best for: high-traffic sites with diverse content
  • Pro: zero config, lazy caching
  • Con: first request is slow (cache miss)

Most modern CDNs (Cloudflare, Fastly, CloudFront) operate as pull CDNs by default, with the ability to push/preload specific assets when needed. This hybrid approach gives you the simplicity of pull with the control of push where it matters.

What CDNs Actually Improve

  • Latency: Serving from a node 50ms away vs 200ms away is a massive UX difference, especially on mobile.
  • Origin load: Your servers handle fewer requests. Most of your traffic is static assets that the CDN absorbs entirely.
  • Availability: If your origin goes down briefly, cached content at the edge can still serve users.
  • DDoS protection: CDN edge networks are designed to absorb large traffic spikes and malicious floods.
Don't only cache static files. Modern CDNs can cache API responses, HTML pages, and even GraphQL queries at the edge. If your API response doesn't change per-user, consider edge caching it with appropriate cache headers. The performance gain can be dramatic.