KAHIBARO
Discord Login Register

2.4. DNS

Why DNS Exists

When you open a website, you type a name like example.com, not an IP address like 93.184.216.34. Computers on the internet, however, use IP addresses to talk to each other.

DNS, which stands for Domain Name System, is the system that translates human friendly domain names into machine friendly IP addresses. It is often called the "phonebook of the internet."

Without DNS, you would have to remember and type IP addresses for every site:

For backend development, understanding DNS matters because it affects how your API is reached, how you point domains to your servers, and how production issues can be caused by misconfigured DNS records.

DNS exists to map names to IP addresses, so clients can reach servers using readable domain names.

Basic DNS Concepts

DNS is a distributed database spread across many servers worldwide. No single server knows everything, but together they provide the complete mapping of domain names to records.

Some key concepts:

Domain: A name like example.com that you can register and configure.

Subdomain: A name that is a part of a domain, like api.example.com or blog.example.com.

Hostname: Often used to mean a specific name that points to a server, typically a domain or subdomain like www.example.com or api.example.com.

Zone: A portion of the DNS namespace managed by a particular authority. When you manage DNS for example.com at a DNS provider, that DNS configuration is the example.com zone.

Record: An entry within a zone that maps a name to some data. For example, api.example.com to an IP address.

Most backend developers work directly with records, not the deeper implementation details of DNS servers.

How DNS Resolves a Domain

When you type https://api.example.com in your browser, the browser must translate api.example.com to an IP address before it can connect.

Here is a simplified sequence of what happens:

  1. Your application or browser checks its own cache. If it recently looked up api.example.com, it might already know the IP and skip the rest.
  2. If not cached locally, the request goes to a DNS resolver, usually operated by your ISP or a public DNS provider like Google DNS (8.8.8.8) or Cloudflare (1.1.1.1).
  3. The resolver checks its own cache. If not present, it performs a recursive lookup by asking other servers:
    1. A root DNS server, which tells it where to find the TLD (Top Level Domain) servers for .com.
    2. A .com TLD server, which tells it which nameservers are authoritative for example.com.
    3. The authoritative nameserver for example.com, which returns the final answer, for example the IP address of api.example.com.
  4. The resolver returns the IP to your browser, and usually caches it for a certain time.
  5. Your browser connects to that IP address on the appropriate port, typically port 80 or 443 for web traffic.

From a backend developer’s perspective, the important parts are:

Root, TLD, and Authoritative Servers

The DNS system is hierarchical.

At the top are root servers. They do not know every domain, but they know where to find the servers for top level domains such as .com, .org, .net, .io.

Below that are TLD servers. A .com TLD server knows which authoritative nameservers are responsible for domains like example.com, github.com, google.com.

Authoritative nameservers hold the final records for a domain. If you use a DNS provider such as Cloudflare, AWS Route 53, or your domain registrar’s DNS, their servers act as authoritative nameservers for your domain.

Example of the path for resolving api.example.com:

  1. Root server: "I do not know api.example.com, but here are the nameservers for .com."
  2. .com TLD server: "I do not know api.example.com, but example.com is managed by these authoritative nameservers: ns1.dns-provider.net, ns2.dns-provider.net."
  3. Authoritative server: "For api.example.com, the IP address is 203.0.113.10."

Once that answer is obtained, it can be cached for some time.

DNS Records You Must Know

DNS records are typed entries in the DNS zone for a domain. Each type has a specific purpose. As a backend developer, you will regularly deal with at least A, AAAA, CNAME, and TXT records.

The most common DNS record types are:

Record typePurposeExample use
AMaps a name to an IPv4 addressapi.example.com203.0.113.10
AAAAMaps a name to an IPv6 addressapi.example.com2001:db8::1
CNAMECreates an alias from one name to anotherwww.example.comexample.com
MXSpecifies mail servers for a domainFor email delivery to user@example.com
TXTStores arbitrary text dataSPF records, domain verification, etc.
NSSpecifies authoritative nameservers for zoneexample.com uses ns1.provider.com, ns2...
SRVSpecifies service discovery recordsSome services use for locating specific ports
CAARestricts which CAs can issue certificatesUsed for TLS certificate security

The records you are most likely to configure when exposing a backend API are A, AAAA, CNAME, and sometimes TXT.

A and AAAA Records

An A record maps a name to an IPv4 address. For instance, suppose your API server is at IPv4 address 203.0.113.10. You might set up:

NameTypeValue
api.example.comA203.0.113.10

Now a DNS lookup for api.example.com will return that IPv4 address, and clients can connect.

An AAAA record is similar, but for IPv6 addresses. For example:

NameTypeValue
api.example.comAAAA2001:db8:abcd::10

You can have both A and AAAA records for the same hostname. Clients that support IPv6 might use the AAAA record, while others use the A record.

As a backend developer deploying APIs, you often receive a server IP address from your hosting provider or cloud platform, and then you create A and possibly AAAA records to point your domain to that IP.

An A record maps a hostname to an IPv4 address, and an AAAA record maps a hostname to an IPv6 address.

CNAME Records

A CNAME record creates an alias from one name to another canonical name. For example, suppose your main domain points to your server:

NameTypeValue
example.comA203.0.113.10

You can then create:

NameTypeValue
www.example.comCNAMEexample.com

Now both example.com and www.example.com will end up at the same IP address, because www.example.com resolves to example.com, and example.com resolves to 203.0.113.10.

Common use cases for CNAME records for backend work:

Example:

NameTypeValue
api.example.comCNAMEbackend-1.hosting.com
auth.example.comCNAMEbackend-1.hosting.com

If the hosting provider changes the IP of backend-1.hosting.com, you do not need to modify your CNAME records. They continue to point to that name, which is updated by the provider.

There are practical rules:

MX, TXT, and Other Records

While focusing on backend APIs, you still often encounter MX and TXT records, especially when you implement email verification or password reset features that rely on your domain’s email setup.

MX records:

TXT records:

As a backend engineer, when integrating with mail providers or cloud services, you will often be instructed to add specific TXT records to your DNS.

SRV and CAA records:

DNS Lookup Examples

You can inspect DNS records from the command line. This is very useful when debugging backend issues.

On Linux or macOS, you might use dig or nslookup. For example:

Using dig to resolve an A record:

bash
dig api.example.com A

You might see a result that includes:

text
;; ANSWER SECTION:
api.example.com.   300   IN   A   203.0.113.10

This shows that api.example.com has an A record pointing to 203.0.113.10 with a TTL of 300 seconds.

To see all records, you can use:

bash
dig api.example.com ANY

or for a specific type:

bash
dig example.com MX
dig example.com TXT

On Windows, you can use:

powershell
nslookup api.example.com

For a TXT record:

powershell
nslookup -type=TXT example.com

Learning to check DNS from the command line helps you understand if a problem is:

TTL and Caching

DNS responses are cached to reduce the load on DNS servers and speed up lookups. Each DNS record has a Time To Live, usually abbreviated as TTL, measured in seconds. The TTL tells resolvers how long they can cache the record before they must ask again.

For example:

NameTypeTTLValue
api.example.comA300203.0.113.10

This means that once a resolver looks up api.example.com, it can store the result for 300 seconds, which is 5 minutes. During that time, it will reuse the cached IP and will not ask the authoritative server again.

TTL controls how long DNS answers are cached. Lower TTL means faster updates but more DNS traffic. Higher TTL means slower updates but fewer DNS queries.

If you change a DNS record, users might still see the old IP until the cached TTL expires. That delay is often called DNS propagation.

Example strategy when you plan to move your API to a new server:

  1. A day before the migration, reduce the TTL of api.example.com from something like 3600 seconds to 300 seconds.
  2. Wait for at least the original TTL so caches holding the old long TTL expire.
  3. Perform the migration. Change the A record to the new IP.
  4. Because the TTL is now 300 seconds, most clients will see the new IP within a few minutes.
  5. After migration, you may increase the TTL again if you want fewer DNS queries.

DNS and Backend Deployments

When you deploy your backend in production, you almost always interact with DNS.

Typical flow when launching api.example.com:

  1. You register a domain, for example example.com, at a domain registrar.
  2. You choose or configure DNS hosting for that domain. This may be the registrar itself, or another provider such as Cloudflare or AWS Route 53.
  3. At your DNS provider, you create records:
    • An A record for example.com to point to your main web server.
    • An A record or CNAME record for api.example.com to point to your API server or load balancer.
  4. You configure your backend application server or reverse proxy to respond to api.example.com. Browsers and clients can now reach your API at https://api.example.com.

If you migrate to a different provider, you usually:

If something goes wrong and you see error messages like "Server not found" or "DNS_PROBE_FINISHED_NXDOMAIN", this can indicate:

Knowing how DNS records are structured lets you diagnose these issues quickly.

DNS Tools for Developers

Backend developers typically use both command line tools and online tools to inspect DNS.

Command line tools:

Online tools:

Example: If you add an A record for api.example.com and it seems not to work for some users, a propagation checker can show if some regions still see the old IP or no record at all.

As you build and deploy backends, it is important to practice:

Over time, DNS becomes a routine part of your deployment and maintenance workflow.

Views: 9

Comments

Please login to add a comment.

Don't have an account? Register now!