Skip to content

Design A Flash Sale / Ticketing System (Prevent Overselling)

1) Problem Clarification / Làm rõ bài toán

EN

We need to design a system where large numbers of users attempt to purchase limited items simultaneously (flash sale, ticket sale, sneaker drop).

Challenges:

  • extreme concurrency
  • hot item inventory accuracy
  • fairness
  • no oversell

VI

Thiết kế hệ thống bán vé / flash sale (đua ship, vé concert, flash sale Shopee).

Thách thức:

  • concurrency cực cao
  • số lượng có hạn
  • fairness (công bằng)
  • không được bán tràn

2) Requirements Definition / Yêu cầu hệ thống

EN – Functional

✔ Reserve inventory
✔ Prevent oversell
✔ Queue/priority model
✔ Payment confirmation
✔ Release expired reservations
✔ Notifications

VI – Chức năng

✔ giữ slot
✔ chống oversell
✔ queue/ưu tiên
✔ xác nhận thanh toán
✔ hết hạn thì trả lại slot
✔ thông báo kết quả

EN – Non-functional

✔ <200ms latency
✔ handle millions of requests
✔ consistency > availability (C over A)
✔ eventual settlement after payment completion

VI – Phi chức năng

✔ latency <200ms
✔ chịu tải triệu request
✔ ưu tiên consistency hơn availability
✔ settlement sau payment async

3) Architecture Overview / Kiến trúc tổng quan

Client → Gateway → Rate Limiter → Reservation Service → Queue → Payment Service → Inventory DB → Settlement Engine

VI

Flow:

Client → Rate limit → Reserve Service → Queue → Payment → Inventory → Settlement

4) Inventory Reservation Model / Mô hình giữ hàng

EN

Core idea: sell reservation, not item.

Steps:

  1. Check inventory
  2. Pre-reserve slot
  3. Timeout window (e.g., 2 minutes)
  4. Confirm payment → finalize
  5. Timeout expiry → release slot

VI

Ý tưởng: bán quyền giữ slot, không bán hàng ngay.

Bước:

  1. check tồn
  2. pre-reserve
  3. timeout (2 phút)
  4. thanh toán → finalize
  5. timeout → trả slot

5) Race Condition Avoidance / Tránh race

EN

Implement atomic inventory decrement:

  • Redis atomic counter
  • DB atomic UPDATE inventory SET count = count - 1 WHERE count > 0

VI

Giảm tồn kho atomically:

  • Redis atomic counter
  • DB update atomic count > 0

6) Queueing Strategy / Chiến lược xếp hàng

EN

Users enter a queue:

  • token retrieved
  • FIFO ordering
  • dequeue when inventory available

This smooths request spikes.

VI

Người dùng vào queue:

  • lấy token
  • xử lý FIFO
  • dequeue khi còn slot

Giảm spike request.

7) Payment Flow & Idempotency / Thanh toán + idempotency

EN

Payment async:

  • reservation recorded
  • payment gateway process
  • callback confirm

Idempotency key ensures retry-safe confirmation.

VI

Thanh toán async:

  • ghi reservation
  • payment gateway xử lý
  • callback xác nhận

Idempotency key tránh double confirm.

8) Consistency Strategy / Chiến lược consistency

EN

Because oversell is unacceptable:

  • Strong consistency when decrementing inventory
  • Eventual consistency acceptable for analytics

VI

Không được oversell → cần strong consistency cho tồn kho.

Analytics có thể eventual.

9) Cache Hot Key Avoidance / Tránh nóng cache key

EN

When millions hit 1 product ID → hot-key issue:
Solution:

  • key partitioning
  • local cache
  • request coalescing
  • replicated counters

VI

1 sản phẩm hot → thành hot-key.
Giải pháp:

  • partition key
  • local cache
  • gom request
  • counter replicate

10) Handling Failures / Xử lý lỗi

EN

  • Payment fails → release reservation
  • Queue stuck → timeout fallback
  • Gateway down → retry reservation
  • Duplicate callback → idempotency handler

VI

  • Payment fail → trả slot
  • Queue kẹt → timeout fallback
  • Gateway down → retry reserve
  • Callback trùng → idempotency handler

11) Anti-Bot Measures / Chống bot

EN

  • rate limit
  • session validation
  • HMAC token signing
  • captcha
  • device fingerprinting

VI

  • rate limit
  • validate session
  • token ký HMAC
  • captcha
  • fingerprint thiết bị

12) Observability / Giám sát

EN

Monitor:

  • reservation success rate
  • queue latency
  • payment conversion
  • timeout releases
  • oversell risk trace logs

VI

Theo dõi:

  • tỷ lệ đặt giữ thành công
  • latency dequeue
  • conversion thanh toán
  • số lượng slot hết hạn trả lại
  • trace rủi ro oversell

13) Trade-offs / Thương lượng thiết kế

EN

Option A — Pessimistic locking (strong consistency)
✔ safe but slow under contention

Option B — Optimistic reservation + compensation
✔ high throughput + release on failure

Hybrid approach preferred.

VI

A — khoá pessimistic
✔ an toàn nhưng chậm

B — optimistic + compensation
✔ throughput cao, xoá bù khi fail

Thực tế hybrid.

14) Future Enhancements / Mở rộng tương lai

EN

  • priority queue for VIP members
  • auction-based bidding
  • ML fraud detection
  • surge pricing
  • distributed ledger settlement

VI

  • queue ưu tiên theo VIP
  • bidding theo giá
  • ML phát hiện fraud
  • surge pricing
  • ledger settlement phân tán
Published inAll

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *