Networking Basics for Engineers
Networking কেন শিখতে হবে?
আপনি একটা API লিখলে। Production এ গিয়ে হঠাৎ slow হয়ে গেলো। Logs দেখলে কিছু বুঝছো না। Problem কোথায়? — Network layer এ। Networking না জানলেন এই debug করা impossible।
System Design interview এ "Design Twitter" বললে — Load balancer কোথায় বসবে? CDN কেন লাগবে? TCP নাকি UDP? HTTP/2 কেন better? — এই সব answer দিতে হবে। Networking হলো System Design এর backbone।
DEFINITION
Networking হলো computers এর মধ্যে data exchange করার system। আপনি যখন google.com type করুন — DNS lookup, TCP connection, HTTP request, TLS encryption — সব মিলিয়ে মাত্র ১০০ms এ response আসে। এই পুরো journey টাই networking।
OSI Model — ৭টি Layer বোঝো
OSI (Open Systems Interconnection) Model হলো networking এর conceptual framework। ৭টি layer এ কাজ ভাগ করা হয়। প্রতিটি layer শুধু নিচের layer এর সাথে কথা বলে।
মনে রাখার trick: "Please Do Not Throw Sausage Pizza Away" — Physical, Data Link, Network, Transport, Session, Presentation, Application।
INTERVIEW এ মনে রাখুন
System Design এ সবচেয়ে বেশি Layer 4 (TCP/UDP) এবং Layer 7 (HTTP) নিয়ে কথা হয়। "Layer 7 Load Balancer" মানে HTTP header, URL path দেখে routing করে। "Layer 4 Load Balancer" মানে শুধু IP:Port দেখে route করে।
TCP vs UDP — সবচেয়ে গুরুত্বপূর্ণ পার্থক্য
TCP — 3-Way Handshake দিয়ে Connection
TCP connection শুরু হওয়ার আগে তিনটা step এ handshake করে। তারপর reliable data transfer হয়। প্রতিটি packet এর acknowledgement (ACK) পাঠানো হয়।
UDP — Fire and Forget
UDP তে কোনো handshake নেই। Data পাঠিয়ে দিন — পৌঁছালো কিনা দেখুন না। Overhead কম, তাই speed বেশি। Real-time applications এর জন্য perfect।
USE CASE ✅ TCP use করুন যখন
Data integrity critical। File transfer, Email, Banking, Database, Web pages (HTTP)। কোনো packet হারানো চলবে না।
USE CASE ✅ UDP use করুন যখন
Speed critical, কিছু packet loss acceptable। Online gaming, Video call, Live streaming, DNS। Latency সবচেয়ে গুরুত্বপূর্ণ।
| বিষয় | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented (handshake) | Connectionless |
| Reliability | ✅ Guaranteed delivery | ❌ Best-effort, no guarantee |
| Order | ✅ In-order delivery | ❌ No ordering |
| Speed | ⚠️ Slower (overhead বেশি) | ✅ Faster (overhead কম) |
| Header size | 20 bytes | 8 bytes |
| Use case | HTTP, FTP, Email, SSH, Banking | DNS, Gaming, Video call, IoT |
const net = require('net'); // TCP
const dgram = require('dgram'); // UDP
// TCP Server — reliable, connection-oriented
const tcpServer = net.createServer((socket) => {
socket.on('data', (data) => {
socket.write(`Echo: ${data}`); // ACK পাঠায়
});
});
tcpServer.listen(3000);
// UDP Server — fast, connectionless, fire and forget
const udpServer = dgram.createSocket('udp4');
udpServer.on('message', (msg, rinfo) => {
// No ACK! Just process and move on
console.log(`From ${rinfo.address}: ${msg}`);
});
udpServer.bind(4000);HTTP এবং DNS — প্রতিদিনের কাজ
DNS — Internet এর Phone Book
আপনি facebook.com type করুন — কিন্তু computer বোঝে IP address। DNS এই translation করে। DNS lookup সবসময় cache থেকে শুরু হয়।
HTTP/1.1 vs HTTP/2 vs HTTP/3
| Feature | HTTP/1.1 | HTTP/2 | HTTP/3 |
|---|---|---|---|
| Year | 1997 | 2015 | 2022 |
| Multiplexing | ❌ নেই | ✅ আছে | ✅ আছে |
| Transport | TCP | TCP | QUIC (UDP) |
| Header Compression | ❌ নেই | ✅ HPACK | ✅ QPACK |
| Head-of-Line Blocking | ❌ আছে | ⚠️ TCP level এ | ✅ সমাধান |
| Use | Legacy | বেশিরভাগ sites | Google, Cloudflare |
Head-of-Line Blocking কি?
HTTP/1.1 এ একটি slow request পুরো queue block করে। ৫টা resource লোড হচ্ছে — একটা slow হলে বাকি ৪টা wait করে। HTTP/2 Multiplexing দিয়ে এটা fix করে — সব resource parallel এ আসে একই connection এ।
Load Balancing — Traffic কীভাবে ভাগ হয়
যখন একটা server handle করতে পারছে না, load balancer multiple servers এ traffic distribute করে। Server healthy আছে কিনা health check দিয়ে monitor করে — down হলে automatically বাদ দেয়।
Load Balancing Algorithms
| Algorithm | কীভাবে কাজ করে | Best For |
|---|---|---|
| Round Robin | একে একে প্রতিটি server এ পাঠায় (1→2→3→1→2→3) | Stateless apps, equal servers |
| Least Connections | সবচেয়ে কম active connection আছে সেখানে পাঠায় | Variable request duration |
| IP Hash | Client IP hash করে same server এ পাঠায় | Session persistence দরকার |
| Weighted RR | Powerful server বেশি traffic পায় (weight অনুযায়ী) | Heterogeneous servers |
# Layer 7 Load Balancer — URL দেখে intelligent routing
upstream api_backend {
least_conn; # Algorithm: least connections
server 192.168.1.10:3000 weight=3; # বেশি powerful server
server 192.168.1.11:3000 weight=2;
server 192.168.1.12:3000 weight=1;
}
server {
listen 80;
# API requests → backend
location /api/ {
proxy_pass http://api_backend;
proxy_set_header X-Real-IP $remote_addr;
proxy_connect_timeout 5s;
}
# Static files → CDN / local cache
location /static/ {
root /var/www/static;
expires 30d;
}
}বড় কোম্পানিগুলো কীভাবে করেছেনে
📺 Netflix — UDP দিয়ে Streaming
Netflix শুরুতে TCP দিয়ে streaming করতো। Problem ছিল TCP এর head-of-line blocking — একটা packet miss হলে video stutter করতো। তারা HTTP/3 (QUIC) তে migrate করে যা UDP-based। QUIC এ individual stream গুলো independent — একটা packet miss হলে শুধু সেই stream affected, বাকিগুলো চলে। Result: buffering উল্লেখযোগ্যভাবে কমে।
🎮 PUBG / Online Gaming — UDP এর ব্যবহার
Game position updates UDP তে পাঠায় — প্রতি 60ms এ। কিছু packet miss হলেও problem নেই, পরের frame এ নতুন position আসবেন। কিন্তু in-game purchase TCP তে — এখানে reliability দরকার। এটাই হলো real-world mixed protocol approach।
CDN — Content Delivery Network
CDN globally distributed servers এ content cache করে। Bangladesh এর user Singapore CDN থেকে content পায় — ৩০০ms এর বদলে ৩০ms latency। Static assets (images, CSS, JS, videos) CDN এ দিলে app ১০x faster মনে হয়। Cloudflare, AWS CloudFront, Akamai popular CDN।
const WebSocket = require('ws');
const http = require('http');
// WebSocket — HTTP upgrade করে persistent bidirectional connection
// REST API তে server প্রথমে push করতে পারে না — WebSocket পারে!
const server = http.createServer();
const wss = new WebSocket.Server({ server });
const clients = new Set();
wss.on('connection', (ws) => {
clients.add(ws);
ws.on('message', (msg) => {
// Server push করছে — সব connected clients এ broadcast
clients.forEach((c) => {
if (c.readyState === WebSocket.OPEN) c.send(`${msg}`);
});
});
ws.on('close', () => clients.delete(ws));
});
server.listen(3000);
// Use: Chat apps, Live feeds, Notifications, Collaborative toolsNetworking Tools — কোনটা কখন ব্যবহার করবেন
| Tool | Type | Best For | Protocol | কে ব্যবহার করে |
|---|---|---|---|---|
| Nginx | Reverse Proxy / LB | HTTP traffic, static files | HTTP/1.1, HTTP/2 | Instagram, Dropbox |
| HAProxy | Load Balancer | High-perf TCP/HTTP LB | TCP, HTTP | GitHub, Reddit |
| AWS ALB | App Load Balancer | AWS, microservices | HTTP/2, WebSocket | Netflix, Airbnb |
| Cloudflare | CDN + DNS + DDoS | Global distribution | HTTP/3, QUIC | Discord, Canva |
| WebSocket | Protocol | Real-time bidirectional | TCP upgrade | Slack, WhatsApp Web |
| gRPC | RPC Framework | Microservice communication | HTTP/2 | Google, Netflix |
🎯 Interview এ এটা বলুন
"Real-time features এর জন্য WebSocket use করবো। Static assets CDN এ রাখবো। Load balancing এর জন্য Layer 7 (Nginx/ALB) ব্যবহার করবো যাতে URL-based routing করতে পারি। Microservices communication এ gRPC কারণ HTTP/2 এ binary protocol faster।" — এই কথাগুলো interviewer কে impress করে।
SUMMARY — আজকে যা শিখলাম
| Concept | এক লাইনে |
|---|---|
| OSI Model | ৭ layer — Physical থেকে Application, প্রতিটির আলাদা role |
| TCP | Reliable, connection-oriented — HTTP, banking, file transfer |
| UDP | Fast, no guarantee — gaming, video call, DNS |
| DNS | Domain → IP translation, cache hierarchy দিয়ে fast |
| HTTP/2 | Multiplexing — একই connection এ multiple parallel requests |
| Load Balancer L4 | IP:Port দেখে route — fast, simple |
| Load Balancer L7 | URL/Header দেখে route — intelligent, flexible |
| WebSocket | Persistent bidirectional — server push করতে পারে |
| CDN | User এর কাছে content cache = অনেক কম latency |