| Users | Transactions/sec | Database Load | System Changes |
|---|---|---|---|
| 100 users | 10-50 | Single DB instance handles writes and reads | Simple ACID transactions, no sharding needed |
| 10,000 users | 1,000-5,000 | DB CPU and I/O increase, some read replicas added | Introduce connection pooling, caching for reads |
| 1,000,000 users | 50,000-100,000 | Single DB cannot handle writes, bottleneck at DB | Sharding writes, distributed transactions, async processing |
| 100,000,000 users | 5,000,000+ | Network and storage bottlenecks, complex consistency | Microservices, event sourcing, CQRS, global data centers |
Why e-commerce tests transactional design in HLD - Scalability Evidence
Start learning this pattern below
Jump into concepts and practice - no test required
In e-commerce transactional design, the first bottleneck is the database write capacity. As users increase, the number of transactions (orders, payments, inventory updates) grows. A single database instance can only handle a limited number of writes per second (usually a few thousand). This causes delays and potential data inconsistency if not managed properly.
- Read Replicas: Offload read queries to replicas to reduce load on primary DB.
- Connection Pooling: Efficiently reuse DB connections to handle more concurrent requests.
- Caching: Use in-memory caches (e.g., Redis) for frequently read data like product info.
- Sharding: Split database by user or order ID to distribute write load across multiple DBs.
- Asynchronous Processing: Use message queues to handle non-critical updates later, reducing immediate DB load.
- Eventual Consistency: Relax strict ACID for some operations to improve scalability.
- Microservices: Separate transactional domains (orders, payments, inventory) for independent scaling.
Assuming 1 million users with 100,000 transactions per second:
- Database writes: 100,000 QPS (queries per second) - requires multiple DB shards.
- Storage: Each transaction ~1 KB, daily storage ~8.6 TB (100,000 tx/sec * 1 KB * 86400 sec).
- Network bandwidth: 100,000 tx/sec * 1 KB = ~100 MB/s sustained write bandwidth.
- Cache: To reduce DB reads, cache size ~100 GB for hot product data.
- Servers: Multiple app servers (100+) to handle concurrent users and transactions.
When discussing e-commerce transactional design scalability, start by identifying the transaction volume and database write limits. Explain how ACID properties affect scaling. Then, describe solutions like sharding and asynchronous processing. Always mention trade-offs between consistency and availability. Use real numbers to show understanding.
Your database handles 1,000 QPS. Traffic grows 10x to 10,000 QPS. What do you do first and why?
Answer: The first step is to add read replicas and implement connection pooling to reduce load on the primary database. For writes, consider sharding or asynchronous processing to distribute and delay load. This prevents the DB from becoming a bottleneck and maintains transaction integrity.
Practice
Solution
Step 1: Understand transaction purpose in e-commerce
Transactions ensure that multiple related actions, like payment and order creation, happen together.Step 2: Identify the benefit of transactional design
This prevents partial updates that could cause errors like double charges or lost orders.Final Answer:
It ensures all steps in a purchase either complete fully or not at all. -> Option AQuick Check:
Transaction safety = B [OK]
- Confusing transaction with website speed
- Thinking transactions only affect browsing
- Assuming transactions reduce product categories
Solution
Step 1: Understand transaction steps order
First, confirm inventory is available to fulfill the order.Step 2: Follow with payment and order update
Then check payment success, and finally update order status to complete.Final Answer:
Confirm inventory, check payment success, then update order status. -> Option DQuick Check:
Correct transaction sequence = A [OK]
- Updating order before payment confirmation
- Checking payment before inventory availability
- Skipping inventory confirmation step
beginTransaction()
if (checkInventory()) {
if (processPayment()) {
updateOrderStatus('confirmed')
commitTransaction()
} else {
rollbackTransaction()
}
} else {
rollbackTransaction()
}What happens if
processPayment() fails?Solution
Step 1: Analyze payment failure branch
IfprocessPayment()returns false, the code callsrollbackTransaction().Step 2: Understand rollback effect
Rollback cancels all changes made in the transaction, so no order update or inventory change is saved.Final Answer:
The transaction is rolled back, no changes saved. -> Option AQuick Check:
Payment failure triggers rollback = C [OK]
- Assuming order status updates despite payment failure
- Thinking inventory changes persist after rollback
- Believing partial commits happen on failure
Solution
Step 1: Identify duplication cause
If orders duplicate after retry, it means failed transactions were not properly rolled back.Step 2: Understand rollback role
Rollback prevents partial or repeated writes; missing rollback causes duplicates.Final Answer:
The transaction does not rollback on failure. -> Option CQuick Check:
Missing rollback causes duplicates = D [OK]
- Blaming payment speed for duplicates
- Confusing order update timing with duplication
- Ignoring rollback importance
Solution
Step 1: Understand atomicity in transactions
Atomicity means all parts succeed or all fail together to keep data consistent.Step 2: Apply atomicity to payment and inventory
If payment succeeds but inventory update fails, the transaction must rollback to avoid errors like charging without stock.Final Answer:
To ensure that if payment succeeds but inventory update fails, the whole operation is reversed. -> Option BQuick Check:
Atomic transaction covers payment and inventory = A [OK]
- Allowing partial order processing
- Separating payment and inventory for speed
- Splitting transactions to reduce load
