Cart là gì ?
Cart = temporary state của user trước khi trở thành Order
-
Không cần strict consistency như payment
-
Nhưng phải:
1/ Guest user
-
Browser (cookie / localStorage)
-
Nhanh
-
Mất khi clear browser
2/ Logged-in user
Logged-in user
-
Redis (primary) → nhanh, scale tốt
-
DB (optional) → backup / analytics
Recommended
Redis = primary cart store
Cart Lifecycle
Add → Update → Remove → Checkout → Convert to Order
Core Use Cases
1/ Add to Cart
User → add item
→ Check product exists
→ Add / increase qty
Không check stock ngay (optional)
→ check lúc checkout
2/ Update Quantity
-
Increase / decrease qty
-
Validate:
3/ Remove Item
-
Delete item khỏi cart
-
Idempotent operation
4/ View Cart
⇒ Data cần:
-
Product info (name, price, image)
-
Quantity
-
Total price
-
Fetch real-time từ DB / cache
-
Không trust giá lưu trong cart
5/ Merge Cart
⇒ Khi user login:
Guest cart + User cart → merge
Strategies:
-
Sum quantity
-
Override
-
Keep latest
6/ Checkout
Steps:
-
Validate stock
-
Lock inventory
-
Calculate final price
-
Create order
-
Clear cart
Key Challenges
1) Price inconsistency
-
Giá thay đổi sau khi add cart
-
Recalculate price lúc checkout
2) Inventory mismatch
-
Validate + reserve inventory lúc checkout
3) Multi-device sync
-
Store cart server-side (Redis)
-
Sync real-time
4) Cart expiration
Solution:
5) High traffic
Solution:
-
Redis
-
Cache product info
-
Avoid DB hit
Key Principles
-
Không reserve inventory khi add cart
-
Không trust price trong cart
-
Validate tất cả ở checkout
-
Cart = fast, Order = correct
Đặc điểm
-
Multi-seller cart
-
Scale cực lớn
Cách làm
-
Redis cart (group theo shop)
-
Checkout dùng Saga
-
Async processing (Kafka/RabbitMQ)
-
Real-time price & voucher
Complexity
-
Flash sale
-
Multi-warehouse
-
Voucher stacking
Insight:
Cart = aggregation layer, không phải source of truth
TikTok Shop
Đặc điểm
-
Live commerce
-
Conversion > consistency
Cách làm
-
Cart nhẹ hoặc bypass (buy now)
-
Real-time inventory
-
Event-driven (Kafka)
Insight:
Tối ưu speed & impulse buying
Shopify
Đặc điểm
Cách làm
-
Cart đơn giản (session / cookie)
-
Backend xử lý checkout mạnh
-
Extensible qua app/plugin
Insight:
Optimize cho merchant simplicity
WooCommerce
Đặc điểm
Cách làm
-
PHP session + DB
-
Server render
Insight:
Simple nhưng khó scale
So sánh nhanh
| Platform | Cart | Scale | Complexity |
| Shopee/Lazada | Multi-seller | Very high | Very high |
| TikTok Shop | Lightweight | Very high | Medium |
| Shopify | Single store | High | Medium |
| WooCommerce | Session-based | Low–medium | Low |