Base.vn Prep

2026-05-19 · Middle-level

PHASE 2

System Design

Topics overview · Universal Clarify · 3 mock design Q · Quick-fire. Topics chi tiết ở BE page.

📚 6 Core Topics (overview)

🎯 Universal Clarify Checklist

Áp dụng cho MỌI design Q

Hỏi 5-8 câu từ checklist (không hỏi cả 14 — sẽ lan man). Mặc định lấy từ Functional + Scale + 1-2 câu phù hợp context.

① Functional scope

  • MVP core feature là gì?
  • Cái gì out of scope?
  • Edge case rare có cần?

② Scale & load

  • DAU/MAU? Peak concurrent?
  • Read:Write ratio?
  • RPS đỉnh? Data size? Growth?

③ Latency / performance

  • p50/p95/p99 target (ms)?
  • Sync hay async OK?
  • TTFB critical không?

④ Consistency

  • Strong hay eventual?
  • Read-after-write cần?
  • Conflict resolution?

⑤ Multi-tenant

  • Single hay multi-tenant?
  • Isolation level?
  • Cross-tenant feature?

⑥ Auth & permissions

  • Role model?
  • Resource ACL hay RBAC?
  • SSO/MFA/API key?

⑦ Real-time

  • Polling đủ hay push?
  • Tolerable update delay?
  • Need presence?

⑧ Data lifecycle

  • Retention policy?
  • Archive cold storage?
  • Soft/hard delete? GDPR?

⑨ Mobile / offline

  • Mobile-first?
  • Offline + sync conflict?
  • Push notification?

⑩ Integration

  • 3rd-party API?
  • Webhook in/out?
  • Import/export?

⑪ Compliance & security

  • PII? Encryption at rest?
  • Audit log?
  • Data residency?

⑫ Availability / DR

  • SLA target?
  • RTO/RPO?
  • Multi-region? Failover?

⑬ Cost & constraints

  • Infra budget?
  • OSS vs SaaS?
  • Vendor lock-in?

⑭ Team & stack

  • Existing stack?
  • Team familiar gì?
  • Ops capability?

💡 Cách mở đầu interview

"Trước khi em design, em muốn clarify vài điểm: (1) MVP scope là... (2) scale dự kiến...? (3) đây là single hay multi-tenant? (4) consistency yêu cầu strong hay eventual? Sau khi rõ những cái này em sẽ pick approach phù hợp."

⚡ 5-Step Design Framework

  1. 1. Clarify (5m) — Functional + Non-functional (xem Universal Checklist)
  2. 2. API design (5m) — Endpoints, request/response shape, auth
  3. 3. Data model (10m) — Tables, relationships, index hints
  4. 4. Architecture (5m) — Client → API → DB. Thêm cache/queue/worker khi cần
  5. 5. Trade-offs (5m) — Optimized cho gì? Sacrificed gì? Failure modes?

💡 Luôn nói ra suy nghĩ. Interviewer chấm reasoning, không phải đáp án đúng.

🎯 Mock Design Questions

Time-box 30 phút/câu, verbalize từng step.

Q1 · Design Task Management System (như Base Work)

Brief: Tasks có project, assignee, comments, attachments. Multi-tenant. Notify khi assigned.

Specific clarify (ngoài Universal):

  • Attachment lưu ở đâu? (S3 vs DB blob)
  • Sub-task / dependency có support?
  • Recurring task (daily/weekly)?
  • Time tracking integrated?

Data model:

tenants(id, name, plan)
users(id, tenant_id, email, role)
projects(id, tenant_id, name)
tasks(id, tenant_id, project_id, assignee_id, title, status, due_date)
comments(id, task_id, author_id, body, created_at)
attachments(id, task_id, s3_key, mime, size)

idx_tasks_assignee_status (assignee_id, status)
idx_tasks_project_created (project_id, created_at DESC)

API:

  • POST /tasks · GET /tasks?projectId · PATCH /tasks/:id
  • POST /tasks/:id/comments · GET /tasks/:id/comments
  • POST /attachments/sign → presigned S3 URL

Architecture:

Client → API (NestJS) → Postgres. Redis cache cho task list. BullMQ cho notification. Worker push WebSocket nếu cần real-time comment.

Trade-offs:

  • Multi-tenant: shared schema + tenant_id + RLS — balance cost vs isolation
  • Notification dùng queue để không block POST
  • Attachment qua presigned URL — không proxy qua API
Q2 · Design Notification System (email + in-app + push)

Brief: Multi-channel. User preference. Retry on failure. Multi-tenant.

Specific clarify:

  • Templating ai làm? (admin GUI hay code-defined)
  • Localization i18n?
  • Provider: SaaS (SendGrid, FCM) hay self-host?
  • Unsubscribe management UI?

Data model:

notifications(id, tenant_id, user_id, type, payload_json, channel, status, retry_count)
user_preferences(user_id, channel, type, enabled, frequency)
templates(id, type, channel, subject, body_html, body_text)

idx_notif_user_status (user_id, status, created_at DESC)
idx_notif_retry       (status, retry_count) WHERE status='failed'

Flow:

App emit event → Notification Service → check preferences
→ write DB (pending) → enqueue BullMQ per channel
→ Worker render template → send via provider
→ update status, retry exponential backoff nếu fail

Trade-offs:

  • Write DB trước khi enqueue → audit + retry sau crash
  • Queue per channel → email chậm không block push
  • Digest batch cho low-priority → tránh spam
  • Provider fail → fallback (SendGrid → SES)
Q3 · Design HR Leave Request System (như Base HRM)

Brief: Employee submit, manager approve. Calendar tích hợp. Approval chain (manager → director nếu > 5 ngày).

Specific clarify:

  • Loại leave? (annual, sick, unpaid)
  • Balance tracking tự động?
  • Approval chain config per-tenant?
  • Calendar: Google/Outlook hay built-in?

Data model:

leave_types(id, tenant_id, name, paid, max_days_per_year)
leave_balances(user_id, leave_type_id, year, allocated, used)
leave_requests(id, tenant_id, user_id, type_id, start_date, end_date, days, status)
approvals(id, request_id, approver_id, level, status, decided_at)
approval_rules(tenant_id, type_id, min_days, approver_role)

State machine:

draft → submitted → manager_pending → director_pending → approved
                                  ↓                ↓
                              rejected         rejected

Trade-offs:

  • State machine tường minh > boolean flag → audit dễ
  • Approval rules data-driven → per-tenant config
  • Balance update khi approved, không khi submitted → tránh rollback
  • Lock pessimistic khi update balance → tránh double-deduct

⚡ Quick-fire System Design Q

SQL vs NoSQL khi nào?

SQL: JOIN/transaction + schema ổn định. NoSQL: write-heavy + schema linh hoạt + sharding.

Cache stampede fix?

Lock (single-flight) + stale-while-revalidate + jittered TTL.

Khi nào pick MongoDB?

Document tự nhiên, không JOIN nặng, schema thay đổi nhiều.

Read replica rủi ro?

Replication lag. Fix: route "read-after-write" về primary.

JWT vs session?

JWT cho stateless+mobile. Session-Redis cho web+revoke. Hybrid: JWT short + refresh.

Idempotency là gì?

Gọi nhiều = 1 lần. GET/PUT/DELETE idempotent. POST → idempotency-key header.

Tenant_id thiếu WHERE?

Data leak. Mitigate: ORM middleware + RLS + integration test.

Khi nào tách microservice?

Team > 2-3 + scale độc lập + deploy độc lập. Đừng start với MS.

← Hub Next: Phase 3 Mock Q →