IP Addresses, Ports & DNS
Basics
Visual Representation
🏠 The Postal System Analogy
Think of it like sending a letter to someone in an apartment building.
IP Address = the building's street address (like "42 MG Road, Mumbai"). It tells the internet which DEVICE to deliver data to.
Port = the apartment number (like "Flat 302"). It tells the device which APP should receive the data. Your phone runs many apps — the port makes sure Netflix data goes to Netflix, not to WhatsApp.
DNS = your phone's contact list. Instead of remembering "142.250.80.46," you just type "google.com" and DNS looks up the number for you.
💡 Simple Summary: IP = which building on the street. Port = which apartment in that building. DNS = the contact list that converts names to addresses.
📺 Why Do We Need IP Addresses?
Here's a question: when you press play on Netflix, how does Netflix's server know to send the video to YOUR phone and not to someone else's laptop in another city?
The answer: your device has a unique address — an IP address (Internet Protocol address). It's a set of numbers like 192.168.1.5 that identifies your device on the network.
Think of it like a home address. Without it, the postman wouldn't know where to deliver your letter. Without an IP address, Netflix wouldn't know where to send your video.
💡 Simple Summary: Every device connected to the internet has an IP address. It's how data finds its way to YOU specifically.
🏢 Private IP vs Public IP
Here's where it gets interesting. Your phone's IP is probably something like 192.168.1.5 — but if you Google "what is my IP," you'll see a completely different number (like 49.205.87.123).
Why two different addresses? Think of it like an apartment building:
Your WiFi router is like the building's receptionist. When you send a request to Netflix, the router changes your private IP to the public IP before sending it out. When Netflix responds, the router knows "this response is for the device in Flat 302" and routes it to your phone.
❓ But wait — if everyone in my house shares one public IP, how does the router know which device gets which response?
Great question! The router keeps a little table: "Phone asked Netflix at 9:01 PM → remember to send the response back to the phone." This system is called NAT (Network Address Translation). Think of the router as a receptionist who takes notes: "The call from Room 302 goes to Netflix, the call from Room 505 goes to YouTube."
💡 Simple Summary: Private IP = your room number (internal). Public IP = your house's street address (what the world sees). Your router (NAT) translates between them.
🚪 What's a Port? (And Why Spotify and Chrome Don't Mix Up Data)
Your phone runs dozens of apps simultaneously — Chrome, Spotify, WhatsApp, Instagram. They ALL use the same internet connection and the same IP address.
So how does your phone know that this incoming data is a web page for Chrome and THAT data is a song for Spotify?
Ports! Each app listens on a different port number (0 to 65535). Think of ports like doors in an office building:
When data arrives at your device, the port number tells the operating system: "This belongs to the app behind door 443" or "This goes to the app behind door 8080."
💡 Simple Summary: IP gets data to the right DEVICE. Port gets data to the right APP on that device. Together they form a complete address like 192.168.1.5:443.
📖 DNS: What Actually Happens When You Type google.com
Let's trace exactly what happens when you type "google.com" in your browser — step by step:
Step 1: Check your browser's memory. "Have I looked up google.com recently?" If yes (and it hasn't expired), use that IP. Done!
Step 2: Check your computer's memory. Your operating system also caches DNS results. Maybe another app looked it up 5 minutes ago.
Step 3: Ask your router. Your WiFi router has its own DNS cache. Maybe your sister looked up google.com an hour ago.
Step 4: Ask your ISP's DNS resolver. Your internet provider (Jio, Airtel, etc.) has a big DNS server. It probably knows google.com's IP because millions of people ask for it daily.
Step 5: If nobody knows — the treasure hunt begins:
Step 6: Everyone saves the answer. The resolver, your router, your OS, and your browser all cache this result. Next time? Instant!
The whole process takes about 20-100ms. You never notice it.
❓ But wait — why not just memorize IP addresses and skip DNS?
Could you memorize 142.250.80.46 (Google), 157.240.1.35 (Facebook), 54.237.226.164 (Netflix), and thousands more? Of course not! Plus, companies change their IPs frequently for scaling and reliability. DNS abstracts this away — you always type "google.com" and it just works.
💡 Simple Summary: DNS is a phonebook that converts human-friendly names (google.com) into computer-friendly numbers (142.250.80.46). It caches results at every level so it's usually instant.
🎯 Key Points for Interviews
❌ Common Mistakes in Interviews
Mistake 1: Forgetting DNS caching — DNS does NOT query root servers every time. Results are cached at browser, OS, router, and ISP levels. Always mention TTL!
Mistake 2: Saying "port 80" for everything — In 2024+, almost everything uses port 443 (HTTPS/secure). Port 80 is the old unencrypted version.
Mistake 3: Not mentioning DNS as a failure point — If DNS goes down, websites become unreachable even though the servers are perfectly fine. The 2016 Dyn DNS attack took down Twitter, Netflix, and Reddit simultaneously.
Mistake 4: Confusing DNS with load balancing — DNS CAN do basic load balancing (return different IPs in rotation), but it can't check if a server is dead. Real load balancers are smarter.
Mistake 5: Ignoring DNS propagation — When you change a DNS record, the old IP is cached worldwide. Full propagation can take hours. This matters during server migrations.
🎯 Interview One-Liner
"DNS resolves human-readable domain names to IP addresses through a hierarchical lookup (root → TLD → authoritative), with caching at every level controlled by TTL, while ports multiplex multiple services on a single IP — and NAT allows billions of devices to share limited IPv4 addresses."
Interview Q&A
Q: What happens when DNS goes down?
Chaos! In 2016, an attack on Dyn (a DNS provider) took down Twitter, Netflix, Reddit simultaneously. The servers were fine, but nobody could look up their addresses. Fix: use multiple DNS providers, keep reasonable TTLs (so caches survive outages), and hardcode fallback IPs for critical internal services.
Q: How does one server run a web app, database, and SSH all at once?
Ports! The web app listens on port 443, the database on 3306, SSH on 22. When data arrives at the server's IP, the port number tells the operating system which application gets it. It's like one building with different offices on different floors — the floor number routes you correctly.
Q: What's the difference between A record and CNAME?
An A record directly maps a name to an IP: "google.com → 142.250.80.46." A CNAME maps a name to another name: "blog.mysite.com → mysite.wordpress.com" (which then gets resolved to an IP). Think of A record as a direct phone number, CNAME as "call my assistant, she'll connect you."
Q: Why is IPv4 running out and how does NAT help?
IPv4 only has 4.3 billion addresses — fewer than the world's people, let alone their devices. NAT helps by letting thousands of devices share ONE public IP through a router. Your phone, laptop, and TV all have private IPs (192.168.x.x) but appear as one public IP to the outside world. We're slowly moving to IPv6 which has enough addresses for every grain of sand on Earth.
Q: How does DNS-based load balancing work?
You configure multiple A records: "api.example.com → 1.1.1.1, 2.2.2.2, 3.3.3.3." DNS returns them in rotation (round-robin), so different users hit different servers. It's simple but limited — DNS can't tell if a server is dead or overloaded. That's why real load balancers exist for production traffic.
Q: What's DNS TTL and how would you set it during a migration?
TTL tells caches how long to remember the result. For migration: lower TTL to 60 seconds a few days BEFORE switching. Then change the DNS record. Within 60 seconds, everyone gets the new IP. If TTL was 24 hours, users would hit the old dead server for up to a full day! After migration stabilizes, raise TTL back to reduce DNS lookup load.
Q: What's the difference between public and private IP addresses?
Public IPs are globally unique and reachable from anywhere on the internet. Private IPs (192.168.x.x, 10.x.x.x) only work inside a local network — your home WiFi or a company's internal network. Your router uses NAT to translate between them. This is how 10 devices at home share one internet connection.
Q: Why do we need port numbers if IP already identifies a machine?
A machine runs many services simultaneously — web server, database, email, SSH. The IP gets you to the right machine, but the port gets you to the right SERVICE on that machine. Without ports, you'd need a separate IP address for every app. It's like a big office building — the street address gets mail to the building, the room number gets it to the right person.
Quick Quiz
1/5You type "swiggy.com" in your browser. What is the FIRST thing that happens?