System DesignMastery
--Foundations — ভিত্তি তৈরি

Networking Basics for Engineers

Duration৪৫-৬০ মিনিট
LevelBeginner
FocusFoundations
001Why It Matters

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।

002OSI Model

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।

7 — APPLICATIONHTTP, DNS, SMTP6 — PRESENTATIONTLS, SSL, JSON5 — SESSIONNetBIOS, PPTP4 — TRANSPORT ★ Key LayerTCP, UDP3 — NETWORK ★ Key LayerIP, ICMP, ARP2 — DATA LINKEthernet, MAC1 — PHYSICALCables, FiberUSERWIRE

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 করে।

003Transport Layer

TCP vs UDP — সবচেয়ে গুরুত্বপূর্ণ পার্থক্য

TCP — 3-Way Handshake দিয়ে Connection

TCP connection শুরু হওয়ার আগে তিনটা step এ handshake করে। তারপর reliable data transfer হয়। প্রতিটি packet এর acknowledgement (ACK) পাঠানো হয়।

CLIENTSERVER① SYN (seq=100) "আমি connect করতে চাই"② SYN-ACK (ack=101) "ঠিক আছে, আমি ready"③ ACK (ack=201) "Connection confirmed!"DATA TRANSFER শুরু— Connection Established — এর পরেই data যায় —

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 সবচেয়ে গুরুত্বপূর্ণ।

বিষয়TCPUDP
ConnectionConnection-oriented (handshake)Connectionless
Reliability✅ Guaranteed delivery❌ Best-effort, no guarantee
Order✅ In-order delivery❌ No ordering
Speed⚠️ Slower (overhead বেশি)✅ Faster (overhead কম)
Header size20 bytes8 bytes
Use caseHTTP, FTP, Email, SSH, BankingDNS, Gaming, Video call, IoT
tcp-vs-udp.js
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);
004Application Layer

HTTP এবং DNS — প্রতিদিনের কাজ

DNS — Internet এর Phone Book

আপনি facebook.com type করুন — কিন্তু computer বোঝে IP address। DNS এই translation করে। DNS lookup সবসময় cache থেকে শুরু হয়।

BROWSER① cache?OS CACHE② miss!RECURSIVERESOLVERROOT NSTLD NSAUTH NSIP ফিরে আসে!⑥ IP → Browser → TCP Connect → HTTP Request

HTTP/1.1 vs HTTP/2 vs HTTP/3

FeatureHTTP/1.1HTTP/2HTTP/3
Year199720152022
Multiplexing❌ নেই✅ আছে✅ আছে
TransportTCPTCPQUIC (UDP)
Header Compression❌ নেই✅ HPACK✅ QPACK
Head-of-Line Blocking❌ আছে⚠️ TCP level এ✅ সমাধান
UseLegacyবেশিরভাগ sitesGoogle, Cloudflare

Head-of-Line Blocking কি?

HTTP/1.1 এ একটি slow request পুরো queue block করে। ৫টা resource লোড হচ্ছে — একটা slow হলে বাকি ৪টা wait করে। HTTP/2 Multiplexing দিয়ে এটা fix করে — সব resource parallel এ আসে একই connection এ।

005Load Balancing

Load Balancing — Traffic কীভাবে ভাগ হয়

যখন একটা server handle করতে পারছে না, load balancer multiple servers এ traffic distribute করে। Server healthy আছে কিনা health check দিয়ে monitor করে — down হলে automatically বাদ দেয়।

👥USERSLOADBALANCERServer 1Server 2Server 3DATABASE(Shared)

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 HashClient IP hash করে same server এ পাঠায়Session persistence দরকার
Weighted RRPowerful server বেশি traffic পায় (weight অনুযায়ী)Heterogeneous servers
nginx-lb.conf
# 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;
    }
}
006Real World Examples

বড় কোম্পানিগুলো কীভাবে করেছেনে

📺 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।

websocket-chat.js
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 tools
007Tools Comparison

Networking Tools — কোনটা কখন ব্যবহার করবেন

ToolTypeBest ForProtocolকে ব্যবহার করে
NginxReverse Proxy / LBHTTP traffic, static filesHTTP/1.1, HTTP/2Instagram, Dropbox
HAProxyLoad BalancerHigh-perf TCP/HTTP LBTCP, HTTPGitHub, Reddit
AWS ALBApp Load BalancerAWS, microservicesHTTP/2, WebSocketNetflix, Airbnb
CloudflareCDN + DNS + DDoSGlobal distributionHTTP/3, QUICDiscord, Canva
WebSocketProtocolReal-time bidirectionalTCP upgradeSlack, WhatsApp Web
gRPCRPC FrameworkMicroservice communicationHTTP/2Google, 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 করে।

008Lesson Summary

SUMMARY — আজকে যা শিখলাম

Conceptএক লাইনে
OSI Model৭ layer — Physical থেকে Application, প্রতিটির আলাদা role
TCPReliable, connection-oriented — HTTP, banking, file transfer
UDPFast, no guarantee — gaming, video call, DNS
DNSDomain → IP translation, cache hierarchy দিয়ে fast
HTTP/2Multiplexing — একই connection এ multiple parallel requests
Load Balancer L4IP:Port দেখে route — fast, simple
Load Balancer L7URL/Header দেখে route — intelligent, flexible
WebSocketPersistent bidirectional — server push করতে পারে
CDNUser এর কাছে content cache = অনেক কম latency
009Knowledge Check
010Assignments
011Practical Lab