Skip to content

Instantly share code, notes, and snippets.

@sejalkailashyadav
Last active June 25, 2025 07:41
Show Gist options
  • Save sejalkailashyadav/d1fa41b7e8cd6ffd29ca8ab01635ca3a to your computer and use it in GitHub Desktop.
Save sejalkailashyadav/d1fa41b7e8cd6ffd29ca8ab01635ca3a to your computer and use it in GitHub Desktop.

🌐 What is DNS?

DNS = Domain Name System

It's like the phonebook of the internet.


📱 Real-Life Example

Imagine you want to call your friend Rahul, but you don’t remember his phone number. Instead, you just look for "Rahul" in your phone contacts, and your phone automatically dials the correct number for you.

Similarly, when you type a website like:

www.google.com

Your computer doesn’t understand names — it needs an IP address like:

142.250.64.78

So DNS acts like your phone contacts: it converts website names into IP addresses.


🔁 How DNS Works (Step by Step)

Let’s say you open your browser and type:

www.netflix.com

Step 1: DNS Query Begins

Your computer asks:

“What is the IP address of www.netflix.com?”

Step 2: DNS Resolver Steps In

This is like an assistant who goes to find the answer. Usually handled by your ISP (like Jio, Airtel) or Google DNS (8.8.8.8).

Step 3: Resolver Checks Cache

First, it checks if it already knows the answer.

If it does, it returns quickly. ✅

Step 4: If Not Found, It Asks the Internet

It goes and asks 3 main servers:

  1. Root Server – “Where can I find .com websites?”
  2. TLD Server (Top Level Domain) – “Where’s netflix.com?”
  3. Authoritative Server – “Here’s the IP address of www.netflix.com!”

Step 5: Resolver Gets IP & Returns It

The resolver gives that IP address to your computer.

Step 6: You Visit the Website

Now your browser connects to that IP and shows you the Netflix homepage.


🧠 Why Is DNS Important?

  • It makes browsing fast and easy.
  • Without it, you’d have to remember IPs like 192.168.1.1.
  • It helps websites load faster through caching.
  • DNS also helps protect against malicious websites using security features.

🚦Analogy Summary

Real World DNS Equivalent
Contact name www.netflix.com
Phone number 142.250.64.78 (IP address)
Your phone Web browser
Contact search DNS resolver
Great bro! 🔥 Let’s go deeper into DNS — covering the main parts step-by-step in simple language, with examples. We'll talk about:
  1. 🔁 How DNS works (deeper)
  2. 🧾 Types of DNS records
  3. 🗂️ DNS caching
  4. 🔐 DNS security

1. 🔁 Deeper Dive: How DNS Works (With Example)

Imagine you type:

www.facebook.com

Here’s what happens under the hood:

Step-by-step DNS Resolution:

✅ 1. Browser Cache

Your browser checks:

“Have I already visited this recently?”

If yes, it remembers the IP and uses it instantly.

🔁 2. OS (Operating System) Cache

If browser doesn’t know, your computer’s OS checks:

“Do I have it stored in memory?”

📞 3. DNS Resolver (Usually by ISP like Jio/Airtel)

If your OS doesn’t know, it contacts a DNS Resolver, like:

  • Google DNS (8.8.8.8)
  • Cloudflare DNS (1.1.1.1)
  • ISP DNS

It says:

“Hey, do you know where www.facebook.com is?”

🌍 4. Root Name Server (Global)

If resolver doesn’t know, it asks:

“Who handles .com domains?”

Root Server replies:

“Ask the .com server.”

🏷️ 5. TLD Name Server (Top-Level Domain)

Now it asks the .com server:

“Where can I find facebook.com?”

TLD server replies:

“Ask the authoritative server for facebook.com.”

🧠 6. Authoritative Name Server

This is the final boss.

It knows:

“Yes! www.facebook.com = 157.240.20.35”

That IP is returned to your browser.

🚀 7. Browser Connects

Now, your browser goes to:

http://157.240.20.35

And loads Facebook!


2. 🧾 Types of DNS Records

Each domain has DNS Records, like entries in a notebook.

Record Type What It Does Example
A Maps domain to IPv4 address facebook.com → 157.240.20.35
AAAA Maps domain to IPv6 address example.com → 2606:4700::1111
CNAME Alias to another domain www.example.com → example.com
MX Mail server info Used for sending/receiving emails
NS Name servers of domain Who handles DNS for the domain
TXT Text info (used for security, verification) SPF, DKIM, etc.

3. 🗂️ DNS Caching (Speed Boost)

To avoid doing this long process every time, DNS uses caching.

  • Browser Cache: Remembers recently visited sites.
  • OS Cache: System memory stores IPs for quick reuse.
  • Resolver Cache: Your ISP or Google keeps a memory of IPs it already fetched.

📌 Example:

If you visit www.google.com:

  1. First time = full DNS lookup 🕐
  2. Second time = fast, from cache ⚡

Each DNS record has a TTL (Time to Live) — defines how long it can be cached.


4. 🔐 DNS Security (Protecting You)

Standard DNS is old and not encrypted. Hackers can spy or redirect you.

⚠️ Problems:

  • DNS Spoofing: Attacker tricks DNS and sends you to a fake website.
  • Man-in-the-Middle Attacks

✅ Solutions:

Protocol Description
DNSSEC Adds a digital signature to DNS records (verifies data isn’t changed)
DoH DNS over HTTPS (encrypts your DNS traffic)
DoT DNS over TLS (also encrypts DNS traffic)

👨‍💻 Simple Real-World Analogy

Let’s say:

  • You want to visit "PIZZA HUT".
  • You don’t know the address, so you ask someone (DNS Resolver).
  • That person asks others (Root, TLD, Authoritative) and finds it.
  • You note it down (Caching).
  • Next time, you go directly to the address.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment