🎯 Mα»₯c TiΓͺu BΓ i ViαΊΏt

Ở Part 1 chΓΊng ta Δ‘Γ£ nαΊ―m mindset: solve business problems, keep it simple, scale when needed. BΓ i nΓ y Δ‘i vΓ o technical fundamentals β€” nhα»―ng building blocks mΓ  mọi system design interview đều hỏi.

System design interview khΓ΄ng phαΊ£i viαΊΏt code. NhΓ  tuyển dα»₯ng muα»‘n thαΊ₯y bαΊ‘n hiểu architecture, scalability vΓ  tradeoffs.

Series Navigation

Part 1 β†’ Engineering Mindset: Beyond Beautiful Code
Part 2 β†’ (bΓ i nΓ y) System Design Crash Course: Fundamentals

πŸ—οΈ 1. System Design Interview β€” Focus Ở ĐÒu?

System Design Interview

        Problem Statement
              β”‚
              β–Ό
      Architecture Design
              β”‚
              β–Ό
      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
      β”‚  Load Balancer    β”‚
      β”‚  App Servers      β”‚
      β”‚  Database         β”‚
      β”‚  Cache            β”‚
      β”‚  CDN              β”‚
      β”‚  Message Queue    β”‚
      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              β”‚
              β–Ό
      Tradeoff Discussion
      (Consistency vs Availability,
       Latency vs Throughput,
       Cost vs Performance)

BαΊ‘n cαΊ§n hiểu tα»«ng component vΓ  biαΊΏt khi nΓ o dΓΉng cΓ‘i nΓ o.


πŸ’» 2. Computer Architecture β€” Nền TαΊ£ng

TrΖ°α»›c khi thiαΊΏt kαΊΏ hệ thα»‘ng lα»›n, cαΊ§n hiểu mΓ‘y tΓ­nh chαΊ‘y code nhΖ° thαΊΏ nΓ o.

Data Hierarchy

Bit          β†’ 0 hoαΊ·c 1
Byte         β†’ 8 bits
KB           β†’ 1,024 bytes
MB           β†’ 1,024 KB
GB           β†’ 1,024 MB
TB           β†’ 1,024 GB

Memory Hierarchy (Tα»‘c Độ Truy CαΊ­p)

Fastest ──────────────────── Slowest

  CPU Cache     RAM      SSD      HDD
  (L1/L2/L3)
  ~1ns         ~100ns   ~100ΞΌs   ~10ms

Diagram: Computer Architecture

          CPU
           β”‚
           β–Ό
     CPU Cache (L1/L2/L3)     ← nanoseconds
           β”‚
           β–Ό
          RAM                  ← ~100 nanoseconds
           β”‚
           β–Ό
          SSD                  ← ~100 microseconds
           β”‚
           β–Ό
          HDD                  ← ~10 milliseconds
Component Role Đặc Δ‘iểm
CPU Execute instructions Xα»­ lΓ½ logic
Cache Ultra-fast memory (gαΊ§n CPU nhαΊ₯t) KB β†’ vΓ i chα»₯c MB
RAM Active program memory GB, mαΊ₯t khi tαΊ―t mΓ‘y
SSD/HDD Persistent storage TB, lΖ°u lΓ’u dΓ i

Tip cho Frontend: Khi bαΊ‘n hiểu memory hierarchy, bαΊ‘n sαΊ½ hiểu tαΊ‘i sao caching quan trọng β€” nΓ³ Δ‘Ζ°a data lΓͺn tαΊ§ng nhanh hΖ‘n.


🏭 3. Production Application Architecture

Mα»™t production app thα»±c tαΊΏ khΓ΄ng chỉ cΓ³ server.

Full Production System

Developer
    β”‚
    β–Ό
Git Repository (GitHub / GitLab)
    β”‚
    β–Ό
CI/CD Pipeline (GitHub Actions / Jenkins)
    β”‚
    β–Ό
Build & Test
    β”‚
    β–Ό
Deployment
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚        PRODUCTION            β”‚
β”‚                              β”‚
β”‚   Load Balancer              β”‚
β”‚        β”‚                     β”‚
β”‚   β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”              β”‚
β”‚   β–Ό          β–Ό              β”‚
β”‚ App Server  App Server      β”‚
β”‚   β”‚          β”‚              β”‚
β”‚   β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜              β”‚
β”‚        β”‚                    β”‚
β”‚   Database Server           β”‚
β”‚        β”‚                    β”‚
β”‚   External Storage (S3)     β”‚
β”‚                              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Observability Layer

Production system luΓ΄n cαΊ§n monitoring:

Application
    β”‚
    β”œβ”€β”€β–Ί Logging (ELK / CloudWatch)
    β”‚
    β”œβ”€β”€β–Ί Monitoring (Grafana / Datadog)
    β”‚
    β”œβ”€β”€β–Ί Alerting (PagerDuty / Slack)
    β”‚
    └──► Tracing (Jaeger / Zipkin)

Debugging Production Issue — Quy Trình Chuẩn

Step 1: Detect
    User report / Alert fires
         β”‚
         β–Ό
Step 2: Analyze
    Check logs & monitoring
         β”‚
         β–Ό
Step 3: Reproduce
    Reproduce in staging environment
         β”‚
         β–Ό
Step 4: Debug
    Root cause analysis
         β”‚
         β–Ό
Step 5: Fix
    Hotfix β†’ Test β†’ Deploy
         β”‚
         β–Ό
Step 6: Post-mortem
    Document what happened & prevent recurrence

βš–οΈ 4. Core Pillars of System Design

Mα»™t system tα»‘t phαΊ£i Δ‘αΊ£m bαΊ£o 4 pillars:

          Good System Design

     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
     β”‚   1. Scalability       β”‚  β†’ Handle growth
     β”‚   2. Maintainability   β”‚  β†’ Easy to change
     β”‚   3. Efficiency        β”‚  β†’ Fast & cost-effective
     β”‚   4. Reliability       β”‚  β†’ Works when needed
     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

3 HoαΊ‘t Động ChΓ­nh Cα»§a Mọi Hệ Thα»‘ng

User Request
     β”‚
     β–Ό
Move Data        ← Network (truyền data giα»―a cΓ‘c component)
     β”‚
     β–Ό
Store Data       ← Database (lΖ°u trα»― persistent)
     β”‚
     β–Ό
Transform Data   ← Application Logic (xα»­ lΓ½ business logic)
     β”‚
     β–Ό
Response

Mọi system design đều xoay quanh Move, Store, Transform data hiệu quαΊ£.


πŸ”Ί 5. CAP Theorem

Distributed system chỉ cΓ³ thể Δ‘αΊ£m bαΊ£o 2 trong 3:

         Consistency (C)
              β–²
             / \
            /   \
           /     \
          / pick  \
         /  only   \
        /    2      \
       /             \
      β–Ό               β–Ό
Availability (A) ── Partition Tolerance (P)
Property NghΔ©a
Consistency Mọi node đều thαΊ₯y data giα»‘ng nhau cΓΉng lΓΊc
Availability System luΓ΄n respond (dΓΉ cΓ³ thể data chΖ°a mα»›i nhαΊ₯t)
Partition Tolerance System vαΊ«n hoαΊ‘t Δ‘α»™ng khi network bα»‹ chia cαΊ―t

VΓ­ Dα»₯ Thα»±c TαΊΏ

Banking System (CP)
β”œβ”€ Consistency + Partition Tolerance
β”œβ”€ ChαΊ₯p nhαΊ­n giαΊ£m availability
└─ LΓ½ do: Sα»‘ dΖ° tΓ i khoαΊ£n PHαΊ’I chΓ­nh xΓ‘c

Social Media (AP)
β”œβ”€ Availability + Partition Tolerance
β”œβ”€ ChαΊ₯p nhαΊ­n eventual consistency
└─ LΓ½ do: Like count chαΊ­m 1-2 giΓ’y khΓ΄ng sao

⏱️ 6. Availability & SLA

Availability Levels

Availability    Downtime/year     DΓΉng cho
─────────────────────────────────────────────
99%             3.65 ngΓ y         Internal tools
99.9%           8.7 giờ           Business apps
99.99%          52 phΓΊt           E-commerce
99.999%         5 phΓΊt            Banking, Healthcare

SLO vs SLA

SLO (Service Level Objective)         SLA (Service Level Agreement)
= Internal goal                       = Contract vα»›i khΓ‘ch hΓ ng

"Response time < 300ms"               "99.99% uptime"
"99.9% uptime"                        "Otherwise refund 10%"
"Error rate < 0.1%"                   "Penalty if breached"

Reliability Concepts

     Primary Server
           β”‚
           β”‚ ← health check
           β”‚
     Backup Server (standby)
           β”‚
           β–Ό
     Failover System
     (auto-switch khi primary fail)
Reliability    = System hoαΊ‘t Δ‘α»™ng Δ‘ΓΊng
Fault tolerance = System chα»‹u được lα»—i
Redundancy     = CΓ³ backup components

πŸ“Š 7. Throughput vs Latency

Hai metric quan trọng nhαΊ₯t

Throughput                          Latency
= Bao nhiΓͺu request xα»­ lΓ½ được    = Thời gian xα»­ lΓ½ 1 request

ĐƑn vα»‹:                            ĐƑn vα»‹:
β”œβ”€ Requests/second (RPS)           β”œβ”€ Milliseconds (ms)
β”œβ”€ Queries/second (QPS)            └─ Seconds (s)
└─ Bytes/second

VΓ­ dα»₯:                             VΓ­ dα»₯:
"Server xα»­ lΓ½ 10,000 RPS"         "API respond trong 50ms"
Client ──── Request ────► Server
                            β”‚
                         Process
                            β”‚
Client ◄── Response ────── β”˜

Latency = tα»•ng thời gian tα»« request β†’ response

Tradeoff: TΔƒng throughput (xα»­ lΓ½ nhiều request hΖ‘n) cΓ³ thể lΓ m tΔƒng latency (mα»—i request chαΊ­m hΖ‘n) nαΊΏu server overloaded.


🌐 8. Networking Basics

IP Address

IPv4 = 32 bit β‰ˆ 4 billion addresses
  VΓ­ dα»₯: 192.168.1.1

IPv6 = 128 bit β‰ˆ gαΊ§n nhΖ° unlimited
  VΓ­ dα»₯: 2001:0db8:85a3:0000:0000:8a2e:0370:7334

Packet Communication

Computer A                     Computer B
    β”‚                              β–²
    β–Ό                              β”‚
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚  Packet  β”‚  ──────────►  β”‚  Packet  β”‚
 β”‚ IP headerβ”‚               β”‚ IP headerβ”‚
 β”‚ Data     β”‚               β”‚ Data     β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

TCP vs UDP

TCP (Transmission Control Protocol)

Client                    Server
  │── SYN ──────────────►  β”‚
  │◄── SYN-ACK ──────────  β”‚
  │── ACK ──────────────►  β”‚
  β”‚                        β”‚
  │◄═══ Reliable Data ═══►│
  β”‚  (ordering, ack,       β”‚
  β”‚   retransmission)      β”‚

Use cases: Web, API, Database, Email

════════════════════════════════════

UDP (User Datagram Protocol)

Client                    Server
  │── Packet ────────────► β”‚
  │── Packet ────────────► β”‚
  │── Packet ────────────► β”‚
  β”‚  (no guarantee,        β”‚
  β”‚   no ordering)         β”‚

Use cases: Video call, Live streaming, Gaming
Feature TCP UDP
Reliable βœ… Yes ❌ No
Ordering βœ… Guaranteed ❌ No
Speed Slower Faster
Connection Connection-based Connectionless
Use case Web, API Realtime

πŸ” 9. DNS β€” Internet Phonebook

User types: example.com
         β”‚
         β–Ό
    DNS Resolver
         β”‚
         β–Ό
    Root DNS Server
         β”‚
         β–Ό
    TLD DNS Server (.com)
         β”‚
         β–Ό
    Authoritative DNS
         β”‚
         β–Ό
    IP: 93.184.216.34
         β”‚
         β–Ό
    Web Server responds

πŸ“‘ 10. Application Protocols

HTTP β€” Request/Response

Client ── GET /api/users ──► Server
Client ◄── 200 OK + data ── Server

HTTP Status Codes

2xx β†’ Success
β”œβ”€ 200 OK
β”œβ”€ 201 Created
└─ 204 No Content

3xx β†’ Redirect
β”œβ”€ 301 Moved Permanently
└─ 304 Not Modified

4xx β†’ Client Error
β”œβ”€ 400 Bad Request
β”œβ”€ 401 Unauthorized
β”œβ”€ 403 Forbidden
└─ 404 Not Found

5xx β†’ Server Error
β”œβ”€ 500 Internal Server Error
β”œβ”€ 502 Bad Gateway
└─ 503 Service Unavailable

WebSocket β€” Realtime

Client ◄═══════════► Server
    persistent connection
    (bi-directional)

Use cases: Chat, Live trading, Sports updates

WebRTC β€” Peer to Peer

Client A ◄═══════════► Client B
    direct connection
    (no server in between)

Use cases: Video call, Voice chat

πŸ”Œ 11. API Design

CRUD Operations (RESTful)

POST   /products         β†’ Create
GET    /products         β†’ Read all
GET    /products/{id}    β†’ Read one
PUT    /products/{id}    β†’ Update
DELETE /products/{id}    β†’ Delete

API Architecture

Client (Angular app)
     β”‚
     β–Ό
API Gateway
     β”‚
     β”œβ”€β”€β–Ί Service A (Users)
     β”‚        β”‚
     β”‚        β–Ό
     β”‚    Database A
     β”‚
     β”œβ”€β”€β–Ί Service B (Products)
     β”‚        β”‚
     β”‚        β–Ό
     β”‚    Database B
     β”‚
     └──► Service C (Orders)
              β”‚
              β–Ό
          Database C

REST vs GraphQL vs gRPC

REST                    GraphQL                 gRPC
─────────────────────────────────────────────────────
GET /users              query {                 Binary protocol
GET /orders               user {                Protobuf schema
                           name
                           orders
                          }
                        }

Pros:                   Pros:                   Pros:
β”œβ”€ Simple               β”œβ”€ No over-fetching     β”œβ”€ Fastest
β”œβ”€ Widely used          β”œβ”€ Flexible queries     β”œβ”€ Type-safe
└─ Cacheable            └─ Single endpoint      └─ Streaming support

Cons:                   Cons:                   Cons:
β”œβ”€ Over-fetching        β”œβ”€ Complex              β”œβ”€ Not browser-native
└─ Multiple endpoints   β”œβ”€ Caching harder       └─ Learning curve
                        └─ N+1 problem

Frontend perspective: REST lΓ  default. GraphQL khi UI cαΊ§n flexible data fetching. gRPC cho microservice-to-microservice.


πŸš€ 12. Caching

Tẑi Sao Caching Quan Trọng?

Cache Δ‘Ζ°a data lΓͺn tαΊ§ng memory nhanh hΖ‘n, giαΊ£m load cho database.

Cache Hit vs Cache Miss

Cache Hit (fast path)           Cache Miss (slow path)

User                            User
 β”‚                               β”‚
 β–Ό                               β–Ό
Cache ──► Data found!           Cache ──► Not found
 β”‚                               β”‚
 β–Ό                               β–Ό
Return response                 Database
(~1ms)                           β”‚
                                 β–Ό
                                Get data
                                 β”‚
                                 β–Ό
                                Store in Cache
                                 β”‚
                                 β–Ό
                                Return response
                                (~100ms)

Cache Strategies

Cache-Aside (Lazy loading)
β”œβ”€ App checks cache first
β”œβ”€ If miss β†’ read from DB β†’ write to cache
└─ Most common strategy

Write-Through
β”œβ”€ App writes to cache AND DB simultaneously
└─ Data always consistent

Write-Behind
β”œβ”€ App writes to cache
β”œβ”€ Cache async writes to DB
└─ Fastest write, risk of data loss

TTL (Time to Live)
β”œβ”€ Cache expires after X seconds
└─ Balance between freshness vΓ  performance

Common Cache Tools

Browser Cache     β†’ Static assets (JS, CSS, images)
CDN Cache         β†’ Content close to users
Redis / Memcached β†’ Application-level cache
Database Cache    β†’ Query result cache

🌍 13. CDN β€” Content Delivery Network

CDN = Servers phΓ’n bα»‘ trΓͺn toΓ n thαΊΏ giα»›i, serve content gαΊ§n user nhαΊ₯t.

KhΓ΄ng cΓ³ CDN:

User (Vietnam)
     β”‚
     β”‚  ~200ms latency
     β–Ό
Origin Server (US)

════════════════════════════════

CΓ³ CDN:

User (Vietnam)
     β”‚
     β”‚  ~20ms latency
     β–Ό
CDN Edge (Singapore)
     β”‚
     β”‚  (cache miss β†’ fetch from origin)
     β–Ό
Origin Server (US)

CDN Benefits

β”œβ”€ GiαΊ£m latency (serve tα»« edge gαΊ§n nhαΊ₯t)
β”œβ”€ GiαΊ£m load origin server
β”œβ”€ Chα»‘ng DDoS (distributed traffic)
└─ TΔƒng availability (multiple edge locations)

Frontend impact: Bundle JS/CSS, images, fonts β†’ Δ‘αΊ·t lΓͺn CDN β†’ user load nhanh hΖ‘n 5-10x.


πŸ”„ 14. Proxy

Forward Proxy

Client                                   Internet
  β”‚                                          β–²
  β–Ό                                          β”‚
Forward Proxy ──────────────────────────────►│
  (hide client identity)
  (content filtering)
  (access control)

Reverse Proxy (Quan Trọng HƑn Cho System Design)

Client
  β”‚
  β–Ό
Reverse Proxy (Nginx / Cloudflare)
  β”‚
  β”œβ”€β”€ SSL termination
  β”œβ”€β”€ Rate limiting
  β”œβ”€β”€ Caching
  β”‚
  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β–Ό          β–Ό
Server1    Server2

βš–οΈ 15. Load Balancer

PhΓ’n Phα»‘i Request Đều Giα»―a CΓ‘c Server

           Clients
         /    |    \
        β–Ό     β–Ό     β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚    Load Balancer     β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”Œβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”
          β–Ό    β–Ό    β–Ό
       Srv1  Srv2  Srv3

Load Balancing Algorithms

Round Robin
β”œβ”€ Request 1 β†’ Server A
β”œβ”€ Request 2 β†’ Server B
β”œβ”€ Request 3 β†’ Server C
β”œβ”€ Request 4 β†’ Server A (lαΊ·p lαΊ‘i)
└─ Simple, equal distribution

Least Connections
β”œβ”€ Route to server with fewest active connections
└─ Better for long-running requests

IP Hashing
β”œβ”€ Same client IP β†’ same server
└─ Session affinity

Weighted Round Robin
β”œβ”€ Server A (powerful) β†’ weight 5
β”œβ”€ Server B (normal)   β†’ weight 2
└─ A nhαΊ­n nhiều traffic hΖ‘n

Layer 4 vs Layer 7

Layer 4 (Transport)           Layer 7 (Application)
β”œβ”€ Based on IP + Port         β”œβ”€ Based on HTTP content
β”œβ”€ Faster                     β”œβ”€ URL routing
└─ Simple                     β”œβ”€ Header-based routing
                              └─ More flexible

πŸ—„οΈ 16. Databases

SQL (Relational)

Examples: PostgreSQL, MySQL, SQLite

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         users            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ id     β”‚ name   β”‚ email  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 1      β”‚ John   β”‚ j@x.co β”‚
β”‚ 2      β”‚ Jane   β”‚ j@y.co β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Properties: ACID
β”œβ”€ Atomicity   β†’ All or nothing
β”œβ”€ Consistency β†’ Data always valid
β”œβ”€ Isolation   β†’ Transactions don't interfere
└─ Durability  β†’ Committed data persists

NoSQL

Document DB (MongoDB)
β”œβ”€ JSON-like documents
β”œβ”€ Flexible schema
└─ Good for: content management, catalogs

Key-Value (Redis)
β”œβ”€ Simple key β†’ value pairs
β”œβ”€ Ultra fast
└─ Good for: caching, sessions

Wide-Column (Cassandra)
β”œβ”€ Column families
β”œβ”€ Massive scale
└─ Good for: time series, IoT

Graph DB (Neo4j)
β”œβ”€ Nodes + relationships
└─ Good for: social networks, recommendations

Khi NΓ o DΓΉng SQL vs NoSQL?

SQL                              NoSQL
─────────────────────────────────────────
Structured data                  Flexible schema
Complex queries (JOIN)           Simple queries
ACID required                    Scale required
Relationships important          High throughput
Banking, E-commerce              Social media, IoT, Cache

πŸ“ˆ 17. Database Scaling

Vertical Scaling

Database Server
      ↑
  More CPU
  More RAM
  More Disk

Simple nhΖ°ng cΓ³ limit.

Horizontal Scaling β€” Replication

Master-Slave Replication

        Master (Write)
           β”‚
      β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”
      β–Ό          β–Ό
  Slave 1     Slave 2
  (Read)      (Read)

Use case: Read-heavy apps (90% read, 10% write)

══════════════════════════════════════════════

Master-Master Replication

  Master A  ◄═══►  Master B
  (Read/Write)     (Read/Write)

Use case: High availability, multi-region

Horizontal Scaling β€” Sharding

Sharding = chia data ra nhiều DB servers

User ID 1-1M      User ID 1M-2M     User ID 2M-3M
     β”‚                  β”‚                  β”‚
     β–Ό                  β–Ό                  β–Ό
  Shard 1            Shard 2           Shard 3

Pros: Massive scale
Cons: Complex, cross-shard queries khΓ³

Database Performance β€” 3 Kα»Ή ThuαΊ­t Quan Trọng

1. Caching
β”œβ”€ Cache query results trong Redis
└─ GiαΊ£m DB load dramatically

2. Indexing
β”œβ”€ TαΊ‘o index trΓͺn columns hay query
β”œβ”€ SELECT * FROM users WHERE email = ?
β”‚  β†’ NαΊΏu cΓ³ index trΓͺn email: ~1ms
β”‚  β†’ KhΓ΄ng cΓ³ index: full table scan ~1000ms
└─ Tradeoff: write chαΊ­m hΖ‘n, tα»‘n storage

3. Query Optimization
β”œβ”€ EXPLAIN ANALYZE
β”œβ”€ TrΓ‘nh SELECT *
β”œβ”€ TrΓ‘nh N+1 queries
└─ Pagination thay vΓ¬ load all

πŸ—ΊοΈ 18. Tα»•ng Hợp β€” System Design Building Blocks

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           SYSTEM DESIGN BUILDING BLOCKS       β”‚
β”‚                                              β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚  Client  β”‚  β”‚   CDN    β”‚  β”‚   DNS    β”‚  β”‚
β”‚  β”‚ (Browser)β”‚  β”‚          β”‚  β”‚          β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β”‚
β”‚       β”‚              β”‚              β”‚        β”‚
β”‚       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β”‚
β”‚                      β”‚                       β”‚
β”‚              β”Œβ”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
β”‚              β”‚ Load Balancer  β”‚              β”‚
β”‚              β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚
β”‚                 β”Œβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”                  β”‚
β”‚                 β–Ό    β–Ό    β–Ό                  β”‚
β”‚              App Servers (cluster)           β”‚
β”‚                 β”‚    β”‚    β”‚                  β”‚
β”‚                 β””β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”˜                  β”‚
β”‚                      β”‚                       β”‚
β”‚           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”            β”‚
β”‚           β–Ό          β–Ό          β–Ό            β”‚
β”‚        Cache      Database   Message Queue  β”‚
β”‚       (Redis)    (PostgreSQL) (RabbitMQ)    β”‚
β”‚                      β”‚                       β”‚
β”‚                 β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”                  β”‚
β”‚                 β–Ό         β–Ό                  β”‚
β”‚             Primary    Replica              β”‚
β”‚                                              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🎯 19. Checklist Tự ĐÑnh GiÑ

Computer Architecture

  • Hiểu memory hierarchy (Cache β†’ RAM β†’ SSD β†’ HDD)?
  • BiαΊΏt tαΊ‘i sao caching quan trọng?

Networking

  • Hiểu TCP vs UDP vΓ  khi nΓ o dΓΉng?
  • BiαΊΏt DNS hoαΊ‘t Δ‘α»™ng thαΊΏ nΓ o?
  • PhΓ’n biệt được HTTP status codes?

API Design

  • BiαΊΏt REST vs GraphQL vs gRPC tradeoffs?
  • Design được RESTful API chuαΊ©n?

Caching & CDN

  • Hiểu cache hit/miss flow?
  • BiαΊΏt cache strategies (Cache-Aside, Write-Through)?
  • Hiểu CDN giαΊ£i quyαΊΏt vαΊ₯n đề gΓ¬?

Load Balancing

  • BiαΊΏt cΓ‘c load balancing algorithms?
  • PhΓ’n biệt được Layer 4 vs Layer 7?

Databases

  • BiαΊΏt khi nΓ o dΓΉng SQL vs NoSQL?
  • Hiểu ACID properties?
  • BiαΊΏt replication vs sharding?
  • Hiểu CAP theorem vΓ  Γ‘p dα»₯ng?

System Design Tα»•ng Hợp

  • VαΊ½ được architecture diagram cho production system?
  • BiαΊΏt availability levels (99.9%, 99.99%)?
  • PhΓ’n biệt throughput vs latency?

πŸ“š TΓ i Liệu Tham KhαΊ£o


πŸ’‘ Tα»•ng KαΊΏt

System Design Interview cαΊ§n hiểu:

1️⃣  Computer Architecture    β†’ Memory hierarchy, bottlenecks
2️⃣  Networking               β†’ TCP/UDP, DNS, HTTP
3️⃣  APIs                     β†’ REST, GraphQL, gRPC
4️⃣  Caching                  β†’ Redis, CDN, strategies
5️⃣  Load Balancing           β†’ Algorithms, L4 vs L7
6️⃣  Databases                β†’ SQL vs NoSQL, ACID, replication
7️⃣  Distributed Systems      β†’ CAP theorem, consistency models
8️⃣  Scalability              β†’ Vertical vs Horizontal
9️⃣  Reliability              β†’ Availability, redundancy, failover
Frontend Engineer System Design Focus:

β”œβ”€ CDN & Caching strategies (αΊ£nh hưởng trα»±c tiαΊΏp UX)
β”œβ”€ API Design (REST/GraphQL β€” bαΊ‘n consume hΓ ng ngΓ y)
β”œβ”€ Load Balancing (hiểu tαΊ‘i sao request cα»§a bαΊ‘n Δ‘αΊΏn Δ‘ΓΊng server)
β”œβ”€ Database basics (hiểu data model để design UI tα»‘t hΖ‘n)
└─ Monitoring & Observability (debug production issues)

β€œA system is only as strong as its weakest component.”

BΓ i trΖ°α»›c: Part 1 β€” Engineering Mindset: Beyond Beautiful Code β€” TΖ° duy professional engineering, business impact, vΓ  nguyΓͺn tαΊ―c simplicity.