Courses System design · Part 2 · Lesson 4
DNS and CDN: How a Request Finds the Nearest Server
In the load balancer lab we saw how a request is shared between servers. But first the request must find out where those servers are. Two systems do this before anything else: DNS and a CDN.
DNS: the phone book of the internet
Computers work with numeric addresses (IP addresses), for example 104.21.32.1. People remember names: detafti.ir. DNS turns a name into an address.
The first time you open a name, these steps happen:
- The browser asks a resolver: usually your internet provider’s, or a service such as
1.1.1.1. - If the resolver does not know, it asks a root server: “Who knows about
.ir?” - Then it asks the
.irserver: “Who knows aboutdetafti.ir?” - Finally it asks the domain’s authoritative server and gets the address.
These four round trips can take 100 milliseconds or more. So every answer comes with a TTL (time to live), for example 300 seconds. Until it runs out, the resolver and the browser keep the answer in a cache and do not ask again. The second time, finding the address takes almost no time.
TTL is a trade-off
- Long TTL (say, one day): fewer lookups, faster. But if you change your server’s address, some users go to the old address for up to a day.
- Short TTL (say, 60 seconds): changes are seen quickly, but there are more lookups.
A common habit: lower the TTL a few hours before you move a server, and raise it again afterwards.
DNS as a load balancer
The authoritative server can return several addresses for one name, or give each user the nearest one (GeoDNS). This is the simplest way to spread users over several data centers. But because answers are cached, DNS cannot react quickly when one server fails. That is why a real load balancer usually sits behind it.
CDN: warehouses near the user
In the numbers lesson we saw that a round trip between continents is about 150 milliseconds. A web page has dozens of files: images, fonts, CSS, JavaScript. If they all come from one far server, the page opens slowly.
A CDN (content delivery network) has hundreds of servers in many cities, called edges. The user connects to the nearest edge. The edge serves the file from its own cache; if it does not have it, it fetches it once from the main server (the origin) and keeps it.
A simple calculation: a user in Tehran opens a page with 30 files, and the browser fetches 6 files at a time.
- Without a CDN, from a server in Europe: 30 ÷ 6 = 5 rounds, about 100 milliseconds each, so about 500 milliseconds just for distance.
- With an edge in the same region and a 10-millisecond round trip: 5 × 10 = 50 milliseconds.
What can go on a CDN?
- Static files: images, video, fonts, CSS and JS. The best fit.
- Pages that are the same for everyone: like this site, which is built with Hugo.
- Personal answers (a shopping cart, an account page): usually not, or only with great care.
The old problem: stale files
When you change a file, the edges may keep serving the old version until its TTL ends. Two common fixes:
- Purge the cache from the CDN’s dashboard.
- Change the file name with every change, for example
app.3f9a1c.js. A new name means a new file, and the old one can be cached forever. This is the more reliable way.
All together
When a user opens shop.example.com:
- DNS turns the name into the address of the nearest CDN edge.
- The edge serves static files from its cache.
- Dynamic requests (such as “my orders”) go from the edge to the origin, where a load balancer shares them between servers.
In an interview
- Whenever users are in several countries, or the content is heavy (images, video), put a CDN in the high-level picture.
- Say one sentence about TTL and stale files: it shows you see the cost too.
- Remember that a CDN does more than add speed: it takes most of the load off the origin and absorbs part of many attacks.
Summary
- DNS turns names into addresses and caches the answers until the TTL ends.
- A CDN keeps files at edges near the user and takes distance out of the equation.
- In both, caching gives speed, and the price is the chance of seeing an old version.