0
0
LLDsystem_design~25 mins

Product, Cart, Order classes in LLD - System Design Exercise

Choose your learning style9 modes available
Design: E-commerce Cart and Order Management
Design focuses on the core classes and their interactions for product catalog, cart management, and order processing. Payment, shipping, and user authentication are out of scope.
Functional Requirements
FR1: Allow users to browse products with details like name, price, and stock quantity
FR2: Users can add products to a shopping cart with specified quantities
FR3: Users can update or remove items in the cart
FR4: Users can place orders from their cart
FR5: Orders should capture product details, quantities, prices, and total amount
FR6: Support multiple users with separate carts and orders
Non-Functional Requirements
NFR1: Handle up to 10,000 concurrent users
NFR2: Ensure cart updates and order placements have p99 latency under 200ms
NFR3: Maintain data consistency between cart and order
NFR4: Availability target of 99.9% uptime
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Product catalog management
Cart service to manage user carts
Order service to process and store orders
Database or in-memory storage for persistence
APIs for user interactions
Design Patterns
Domain-Driven Design for modeling Product, Cart, and Order entities
Event-driven updates for stock changes
Repository pattern for data access abstraction
Transactional consistency for order placement
Reference Architecture
  +-------------+       +------------+       +------------+
  |   Product   |<----->|    Cart    |<----->|   Order    |
  +-------------+       +------------+       +------------+
        ^                     ^                    ^
        |                     |                    |
  Product Catalog       User Session          Order History
  (Database)            (In-Memory/DB)        (Database)
Components
Product
Class with attributes
Represents items available for purchase with details like id, name, price, and stock quantity
Cart
Class with collection of CartItems
Holds products selected by a user with quantities before order placement
Order
Class capturing finalized purchase details
Records purchased products, quantities, prices, total amount, and order status
Request Flow
1. User browses products from Product catalog
2. User adds product with quantity to Cart
3. Cart updates item quantities or removes items as requested
4. User places order from Cart
5. Order is created with snapshot of Cart items and prices
6. Stock quantity in Product catalog is updated accordingly
7. Order status is tracked until completion
Database Schema
Entities: - Product(id PK, name, price, stock_quantity) - Cart(id PK, user_id FK) - CartItem(id PK, cart_id FK, product_id FK, quantity) - Order(id PK, user_id FK, total_amount, status, created_at) - OrderItem(id PK, order_id FK, product_id FK, quantity, price_at_purchase) Relationships: - One Product can be in many CartItems and OrderItems - One Cart has many CartItems - One Order has many OrderItems - User has one Cart and many Orders
Scaling Discussion
Bottlenecks
Database contention on Product stock updates during high order volume
Cart data consistency when multiple devices update simultaneously
Latency in order placement under heavy load
Storage growth for order history over time
Solutions
Use optimistic locking or versioning on Product stock to prevent overselling
Implement distributed locks or atomic operations for Cart updates
Introduce caching layers and asynchronous processing for order creation
Archive old orders and use partitioned databases for scalability
Interview Tips
Time: Spend 10 minutes clarifying requirements and constraints, 20 minutes designing classes and their interactions, 10 minutes discussing scaling and trade-offs, and 5 minutes summarizing.
Explain clear separation of concerns between Product, Cart, and Order
Discuss data consistency challenges and solutions
Highlight how the design supports multiple users and concurrent updates
Mention scalability considerations and how to handle growth
Show understanding of real-world e-commerce workflows