System Design

Internet protokollari (HTTP, DNS)

Internet bir nechta protokollar orqali ishlaydi. Eng muhimlari: DNS, HTTP/HTTPS, WebSocket.

DNS (Domain Name System)

DNS — domain nomini IP addressga aylantiradi.

Nega kerak?

Odam:      google.com (eslab qolish oson)
Kompyuter: 142.250.185.46 (IP address)

Odamlar nom eslab qoladi, kompyuterlar raqamlar bilan ishlaydi. DNS bu ikkalasini bog’laydi.

DNS qanday ishlaydi?

1. Browser → Local DNS Cache
   google.com topildimi? Yo'q 

2. Browser → ISP DNS Server
   google.com topildimi? Yo'q 

3. ISP DNS → Root DNS Server
   ".com" qayerda? → .com DNS serveri

4. → .com DNS Server
   google.com qayerda? → Google DNS serveri

5. → Google DNS Server
   google.com = 142.250.185.46 

6. ISP DNS → Browser
   142.250.185.46 (cache'ga saqlaydi)

Birinchi marta: 100-200 ms
Keyingi safar: 1-2 ms (cache’dan)

DNS Record turlari

A Record:     google.com → 142.250.185.46 (IPv4)
AAAA Record:  google.com → 2001:4860:4860::8888 (IPv6)
CNAME:        www.google.com → google.com (alias)
MX Record:    gmail.com → mail server
TXT Record:   Verification, SPF, DKIM

DNS TTL (Time To Live)

google.com A 142.250.185.46 TTL=300

Cache 300 sekund (5 minut) saqlanadi. Keyin yana so’raydi.

Short TTL (60s): Tez o’zgartirish kerak bo’lsa
Long TTL (3600s): Barqaror, DNS so’rovlarni kamaytiradi

HTTP (Hypertext Transfer Protocol)

HTTP — brauzer va server o’rtasidagi til.

HTTP Request

GET /api/users/123 HTTP/1.1
Host: api.example.com
User-Agent: Mozilla/5.0
Accept: application/json
Authorization: Bearer token123

Request qismlari:

HTTP Response

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 1234
Cache-Control: max-age=3600

{"id": 123, "name": "Jamshid"}

Response qismlari:

HTTP Methods

MethodMaqsadIdempotent?
GETMa’lumot olishHa
POSTYangi yaratishYo’q
PUTTo’liq yangilashHa
PATCHQisman yangilashYo’q
DELETEO’chirishHa

Idempotent = Bir xil requestni 10 marta yuborsangiz, natija bir xil.

HTTP Status Codes

2xx Success
  200 OK
  201 Created
  204 No Content

3xx Redirection
  301 Moved Permanently
  302 Found (Temporary)
  304 Not Modified (cache)

4xx Client Error
  400 Bad Request
  401 Unauthorized
  403 Forbidden
  404 Not Found
  429 Too Many Requests

5xx Server Error
  500 Internal Server Error
  502 Bad Gateway
  503 Service Unavailable
  504 Gateway Timeout

HTTPS (HTTP Secure)

HTTPS = HTTP + TLS/SSL (shifrlash)

HTTP vs HTTPS

HTTP:  Client → [plain text] → Server
       Hacker o'qiy oladi 

HTTPS: Client → [encrypted] → Server
       Hacker faqat gibberish ko'radi 

SSL/TLS Handshake

1. Client → Server: "Salom, HTTPS gaplashamiz"
2. Server → Client: SSL sertifikat (public key)
3. Client: Sertifikatni tekshiradi
4. Client → Server: Encrypted session key
5. Secure connection established

Vaqt: 50-100 ms qo’shimcha

Sertifikat (Certificate)

Subject: example.com
Issuer: Let's Encrypt
Valid: 2024-01-01 → 2024-12-31
Public Key: ...

Bepul: Let’s Encrypt
Pulli: DigiCert, Comodo (business uchun)

HTTP/1.1 vs HTTP/2 vs HTTP/3

HTTP/1.1 (1997)

Request 1 → Response 1
Request 2 → Response 2  (kutishi kerak)
Request 3 → Response 3  (kutishi kerak)

Muammo: Head-of-line blocking

HTTP/2 (2015)

Request 1 → Response 1
Request 2 → Response 2  (parallel)
Request 3 → Response 3  (parallel)

Yaxshilanish: Multiplexing, header compression

HTTP/3 (2020+)

HTTP Headers muhim

Cache-Control

Cache-Control: max-age=3600
Cache-Control: no-cache
Cache-Control: no-store, private

Authorization

Authorization: Bearer eyJhbGc...
Authorization: Basic dXNlcjpwYXNz

Content-Type

Content-Type: application/json
Content-Type: text/html
Content-Type: multipart/form-data

CORS (Cross-Origin)

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST
Access-Control-Allow-Headers: Content-Type

REST API Design

REST — HTTP’ni to’g’ri ishlatish qoidalari.

RESTful endpoints

GET    /users           → Barcha userlar
GET    /users/123       → Bitta user
POST   /users           → Yangi user yaratish
PUT    /users/123       → User yangilash
DELETE /users/123       → User o'chirish

GET    /users/123/posts → User postlari

Yaxshi API design

/api/v1/users
/api/v1/users/123
/api/v1/users/123/orders

/api/getUser?id=123
/api/user_delete
/api/updateUser123

Qoidalar:

WebSocket

Real-time uchun HTTP yetarli emas:

HTTP (polling)

Client: Yangilik bormi? → Server: Yo'q
(1 sekund)
Client: Yangilik bormi? → Server: Yo'q
(1 sekund)
Client: Yangilik bormi? → Server: Ha!

Muammo: Ko’p so’rovlar, kechikish

WebSocket

Client ←→ Server (doimiy aloqa)
Server → Client: Yangilik!
Server → Client: Yana yangilik!

Ishlatiladi: Chat, gaming, real-time dashboard

Xulosa

DNS:

HTTP:

HTTPS:

REST:

Keyingi dars: Load Balancing — trafikni taqsimlash.