E-Commerce Backend Design
E-Commerce Backend Design
Section titled “E-Commerce Backend Design”📖 Introduction
Section titled “📖 Introduction”An e-commerce backend handles product catalogs, shopping carts, order processing, payment integration, and inventory management. It’s one of the most common and complex system design problems — combining CRUD operations, transactional integrity, real-time stock updates, and payment processing.
🤔 Why Do We Need This?
Section titled “🤔 Why Do We Need This?”E-commerce backends present unique challenges:
- Transactional integrity — Money and inventory must be accurate
- Concurrent access — Two users buying the last item simultaneously
- Catalog performance — Millions of products, fast faceted search
- Order lifecycle — Complex state machine with multiple services
💻 Coding Challenge 1: Product Catalog API
Section titled “💻 Coding Challenge 1: Product Catalog API”Build a product catalog API:
- Products have: name, description, price, category, variants (size/color), images
GET /products— paginated list with category filter, price sort, searchGET /products/:id— single product with all variantsGET /categories— category tree- Add proper indexes for common queries (category, price range)
💻 Coding Challenge 2: Cart and Checkout
Section titled “💻 Coding Challenge 2: Cart and Checkout”Build a shopping cart with checkout:
POST /cart— add item (with variant selection)PUT /cart/:itemId— update quantityDELETE /cart/:itemId— remove itemPOST /checkout— create order, charge payment, clear cart- Checkout uses a transaction: validate stock → charge → confirm order
- Handle the “last item” race condition
💻 Coding Challenge 3: Order Management
Section titled “💻 Coding Challenge 3: Order Management”Build an order management system:
- Order states:
pending → confirmed → processing → shipped → delivered - Orders can be cancelled (before shipped) or returned (after delivery)
GET /orders— user’s order historyGET /admin/orders— all orders with filters (by status, date range)- Admin can update order status
- Event-driven: order status changes trigger email notifications
📖 Summary
Section titled “📖 Summary”| Service | Responsibility | Technology |
|---|---|---|
| Product Service | Catalog, search, categories | MongoDB (flexible schema) |
| Cart Service | Temporary cart management | Redis (fast, TTL-based) |
| Order Service | Order processing, lifecycle | PostgreSQL (ACID) |
| Payment Service | Stripe integration | BullMQ (async, retries) |
| Inventory Service | Stock tracking | PostgreSQL + Redis |
| Notification Service | Emails, SMS | BullMQ worker |
📝 MCQs
Section titled “📝 MCQs”1. What database technology is best suited for a product catalog with flexible attributes?
- A) PostgreSQL (rigid schema)
- B) MongoDB (flexible schema) ✅
- C) Redis
- D) SQLite
2. How would you handle the “last item in stock” race condition when two users buy simultaneously?
- A) Optimistic locking with version check ✅
- B) Let both succeed
- C) Use a queue (too slow)
- D) Reject all concurrent purchases
3. What pattern is ideal for processing order payments asynchronously?
- A) HTTP callback
- B) Message queue with retries ✅
- C) Polling every second
- D) Inline payment processing
4. Why is Redis ideal for shopping cart storage?
- A) Relational queries
- B) Fast reads/writes with TTL-based expiration ✅
- C) Complex join operations
- D) Full-text search
5. What ensures data integrity during checkout (order creation + payment + inventory deduction)?
- A) Async processing
- B) Database transactions (ACID) ✅
- C) WebSocket notifications
- D) Redis caching
Answer Key: 1-B, 2-A, 3-B, 4-B, 5-B
🔗 Related Topics
Section titled “🔗 Related Topics”- Working with Databases — Data modeling, transactions
- Message Queues — Async order processing
- Authentication — JWT for user sessions
- Caching — Product catalog caching
- Building REST APIs — API design patterns