Skip to content

System Design Methodology

Mở đầu

System design không phải vẽ architecture diagram tuỳ tiện, mà set methodology có rule. Dù interview hay work thật, đều theo similar framework: hiểu vấn đề → estimate scale → design → optimize sâu.

Bạn sẽ học:

  • Flow design: framework 4-step
  • Capacity estimation: "back-of-envelope"
  • Common patterns: cache, sharding, MQ, CDN
  • Trade-off thinking: tư duy đánh đổi
  • Real cases: short link, feed, flash sale
ChươngNội dung
14-step framework
2Capacity estimation
3Core design patterns
4Trade-off thinking
5Classic cases

1. 4-step framework

System design không nhảy thẳng vẽ diagram. Theo structured flow.

系统设计四步法
点击每个步骤查看详细内容
1
需求澄清
~5 分钟
2
容量估算
~5 分钟
3
架构设计
~15 分钟
4
深入优化
~10 分钟
需求澄清
不要急着画架构图。先搞清楚:系统要解决什么问题?用户规模多大?有哪些核心功能?有哪些非功能需求?
核心功能有哪些?(MVP 范围)
用户规模?DAU / QPS 预估
读写比例?读多写少还是写多读少?
数据量级?需要存多少数据?
可用性要求?几个 9?
延迟要求?P99 要多少毫秒?
示例(设计短链服务):
短链服务:生成短链(写)+ 重定向(读),读写比约 100:1,日均 1 亿次重定向,短链永不过期。

Sao clarify requirement trước?

Nhiều người nhận đề → vẽ ngay → "đúng nhưng không phải cái interviewer muốn". Bỏ 5 phút clarify = tránh 30 phút làm lại.

Câu clarify common:

  • Core function là gì? (đừng design hết)
  • User scale bao nhiêu? (quyết distributed không)
  • Read/write ratio? (quyết cache strategy)
  • Data giữ bao lâu? (quyết storage)

2. Capacity estimation: nghệ thuật back-of-envelope

Back-of-envelope estimation = skill core. Không cần chính xác, chỉ cần order of magnitude.

信封背面估算器
输入基础数据,自动计算系统容量需求
日请求量
2000.0 万
平均 QPS
231
峰值 QPS
693
日带宽
102.4 GB
峰值带宽
3.5 MB/s
常用估算参考值
1 天86,400 秒
1 月≈ 250 万秒
QPS 1000≈ 1 台 8 核服务器
1 亿/天≈ 1,200 QPS
MySQL 单机≈ 5,000 QPS
Redis 单机≈ 100,000 QPS

Conversion cheatsheet

OrderConversionMemory trick
1 day86,400s≈ 100k giây
100M req/day≈ 1,200 QPSChia 100k
1KB × 100M≈ 100GB100M small record
1MB × 1M≈ 1TB1M ảnh

80/20 rule trong estimation

Đa số system theo 80/20: 20% data carry 80% request:

  • Cache size ≈ total data × 20%
  • Hot QPS ≈ total QPS × 80% concentrate vào 20% key
  • Cache hit rate target ≈ 80%+ (thấp hơn = strategy có vấn đề)

3. Core design patterns

Patterns lặp đi lặp lại trong system design. Master = handle đa số scenario.

3.1 Cache patterns

PatternReadWriteUse
Cache-AsideQuery cache, miss → DB + refillWrite DB, delete cacheMost common
Read-ThroughCache auto load từ DBSame Cache-AsideCần framework support
Write-BehindSame Cache-AsideWrite cache, async DBWrite-heavy, accept loss

Sao "delete cache" không "update cache"?

Update cache trong concurrent dễ inconsistent: thread A + B cùng update, A write DB trước nhưng B update cache trước → cache = B's old value. Delete cache = next read load fresh từ DB, tránh issue này native.

3.2 Sharding

Khi single table > triệu row, hoặc single DB QPS quá bottleneck → sharding.

StrategyCáchLợiNhược
Vertical DBTách DB theo business domainDecouple business, scale độc lậpCross-DB JOIN khó
Horizontal tableCùng table tách theo ruleSingle table size controlSharding key key
Vertical tableTách big field thành table riêngGiảm IO, tăng queryCần JOIN thêm

Sharding key principle:

  • Chọn field query thường nhất (vd user_id)
  • Distribute đều, tránh hot spot
  • Cùng user data trong cùng shard

3.3 Message queue

MQ = "shock absorber" distributed, core: decouple, async, smoothing.

ScenarioKhông MQCó MQ
Order xong send notifyOrder API sync gọi notify, notify fail → order failOrder OK → send message, notify async consume
Flash saleBurst traffic đập DBRequest enqueue, backend consume theo capacity
Data syncService A direct gọi BA publish event, B subscribe

4. Trade-off thinking: no silver bullet

Architecture design bản chất = Trade-off. Mỗi decision có cost, key là hiểu cost + chọn phù hợp giai đoạn.

DimOption AOption BDecision
Consistency vs AvailabilityStrong (CP)High availability (AP)Business tolerate inconsistent ngắn?
Perf vs CostFull cacheOn-demand cacheData volume + budget
Simple vs FlexibleMonolithMicroserviceTeam scale + business complexity
Realtime vs BatchStreamBatchData timeliness need
Self-host vs ManagedTự MySQLCloud RDSOps capability + cost

Architecture Decision Record (ADR)

Mỗi quan trọng decision phải record: background, options, why chosen, cost. Không phải đổ lỗi, mà để người sau hiểu "sao lúc đó design vậy".

Format đơn giản:

  • Title: Replace XXX với YYY
  • Background: Gặp vấn đề gì
  • Decision: Chọn option nào
  • Rationale: Sao chọn
  • Cost: Nhược điểm + risk

Common bad trade-offs

ErrorBiểu hiệnĐúng
Premature optimizationDAU 1k đã shardingSingle DB trước, gặp bottleneck mới tách
Tech-driven"Tôi muốn Kafka" thay vì "tôi cần async"Từ vấn đề, không từ tech
Ignore ops costChọn option tối ưu nhưng team maintain không nổiMatch team ability
Pursue perfect consistencyMọi scenario distributed transactionĐa số eventual consistency đủ

5. Classic cases

3 cases kinh điển, áp dụng methodology:

Short link = case interview kinh điển, nhỏ nhưng đủ.

Clarify:

  • Core: long → short (write), short → redirect (read)
  • R/W: ~100:1 (read >> write)
  • Daily redirect: 100M
  • Never expire

Estimation:

MetricCalcResult
Write QPS100M / 100 / 86400≈ 12 QPS
Read QPS100M / 86400≈ 1,200 QPS
Peak read1,200 × 3≈ 3,600 QPS
5-year storage1M/day × 365 × 5 × 100B≈ 18 GB
Cache (20%)18 GB × 20%≈ 3.6 GB

Architecture:

Write: Client → API → ID gen → Base62 encode → MySQL + Redis
Read: Client → CDN → API → Redis query → 302 redirect
                              ↓ (miss)
                            MySQL → refill Redis

Key decisions:

  • Short code: Snowflake ID + Base62, tránh hash collision
  • Cache: Cache-Aside, hot link dùng CDN
  • DB: single table OK (18GB nhỏ), index theo short code

5.2 Feed stream

Social feed (Facebook feed, Twitter timeline).

Challenge core: user post 1 update, làm sao mọi follower thấy?

SolutionCáchLợiNhược
PullRead time aggregate updates of followersWrite đơn giản, ít storageRead chậm, follow nhiều = latency cao
PushPublish time write inbox của mỗi followerRead cực nhanhBig V post → write fanout nặng
HybridNormal user push, big V pullBalance R/WImplement complex

Hybrid solution:

  • Follower <10k: publish push tới mọi follower (push)
  • Follower >10k: không push, follower read time pull (pull)
  • User open feed: merge pushed content + realtime pull big V, sort by time

5.3 Flash sale system

Core challenge: instant high concurrency + stock không over-sell.

Traffic feature:

  • Pre-event: nhiều user refresh đợi
  • Event start instant: QPS có thể 100x bình thường
  • Post-event: traffic giảm nhanh

Layered smoothing:

Request → CDN (static) → Gateway (rate limit) → MQ (smoothing) → Stock service (deduct)
LayerStrategyEffect
FEButton gray + random delay + captchaFilter bot, scatter request
CDNStatic cacheGiảm 90% page request
GatewayToken bucketChỉ pass traffic system chịu nổi
MQEnqueue + async processSmoothing, protect DB
Stock serviceRedis pre-deduct + Lua atomicPrevent over-sell, ms response

Flash sale core principles

  1. Block upstream: CDN block được thì không lên app
  2. R/W split: product detail page → cache, chỉ checkout → DB
  3. Async: user click "buy" → return "queuing" ngay, async xử
  4. Plan B: rate limit, circuit break, degradation

Tổng kết

System design = practical skill, core là structured thinking + trade-off.

  1. 4-step framework: clarify → estimate → design → optimize
  2. Back-of-envelope: không chính xác, order of magnitude OK
  3. Core patterns: cache, sharding, MQ, CDN, rate limit + circuit breaker = "lego" của system design
  4. Trade-off thinking: no perfect, chỉ phù hợp giai đoạn, record decision
  5. Classic cases: short link (basic), feed (push-pull), flash sale (high concurrency) — master 3 này = apply rộng

2026 cho VN dev

  • Interview prep:
    • ByteByteGo (Alex Xu): visual best
    • System Design Primer (GitHub): comprehensive
    • Hello Interview: structured framework
  • VN scenarios:
    • Tiki search system
    • Grab matching system
    • MoMo payment + fraud detection
    • Zalo messaging architecture
  • AI-era system design:
    • LLM gateway (LiteLLM, Portkey)
    • Vector DB integration
    • Async inference queue
    • Cost optimization layer
  • Trends 2026: serverless-first, edge computing, event-driven, eventually consistent

Tài liệu