Sẵn sàng thử sức?
Phỏng vấn thử với AI, nhận đánh giá và feedback chi tiết
Giải thích CAP Theorem? Khi nào chọn CP vs AP?
CAP: Trong distributed system, chỉ có thể đạt 2 trong 3:
- Consistency: Mọi read nhận được data mới nhất
- Availability: Mọi request nhận được response
- Partition tolerance: System hoạt động khi network partition
CP (Consistency): Banking, inventory - data phải chính xác
AP (Availability): Social media, analytics - chấp nhận eventual consistency
Design URL Shortener (như bit.ly)?
Requirements: Shorten URL, redirect, analytics
Components:
- API: POST /shorten, GET /:shortCode (301 redirect)
- Short code: Base62 encode (a-z, A-Z, 0-9)
- Storage: Key-value store (Redis), Database backup
- Scale: Counter service (Zookeeper), pre-generate codes
Collision handling: Check existence, retry với code mới
Database Sharding strategies?
1. Horizontal (Row-based): Chia rows theo shard key
- Range: user_id 1-1M → shard1, 1M-2M → shard2
- Hash: hash(user_id) % num_shards
- Directory: Lookup table chỉ đến shard
2. Vertical: Chia columns (separate tables)
Challenges: Cross-shard queries, rebalancing, transactions.
Rate Limiting implementation?
Algorithms:
- Token Bucket: Tokens sinh theo thời gian, request lấy token
- Sliding Window: Count requests trong time window
- Leaky Bucket: Queue requests, process với fixed rate
# Redis Sliding Window
def is_allowed(user_id, limit=100, window=60):
key = f"rate:{user_id}"
now = time.time()
pipe = redis.pipeline()
pipe.zremrangebyscore(key, 0, now - window)
pipe.zadd(key, {str(now): now})
pipe.zcard(key)
pipe.expire(key, window)
_, _, count, _ = pipe.execute()
return count <= limit
Event-Driven Architecture vs Request-Response?
Request-Response: Synchronous, simple, nhưng tight coupling.
Event-Driven:
- Loose coupling, scalable, resilient
- Eventual consistency
- Patterns: Event Sourcing, CQRS, Saga
Khi nào dùng Event-Driven?
- Multiple services cần react to same event
- Async processing acceptable
- Audit trail quan trọng (Event Sourcing)
Làm thế nào để handle millions of concurrent connections?
- Load Balancer: Distribute traffic (L4/L7)
- Connection Pooling: Reuse DB connections
- Async I/O: Event loop (Node.js, Go goroutines)
- Horizontal Scaling: Add more servers
- Caching: CDN, Redis, in-memory
- Database: Read replicas, sharding