Amazon Online Shopping System: low-level design

Design an online shopping system. Customers search products, add them to a cart, place an order, pay and track shipping. Sellers manage products and stock.

Stage 1: Products, catalogue, cart

Browse and collect items.

Browsing and holding items before buying need a home. `Catalog` owns search, `Cart` holds lines and totals them, `Customer` owns one cart. Search and the cart are separate, so each can change (add filters, merge guest carts) alone. The cart stores a price copy at add time or reads the live price. Decide and show the change at checkout.

Stage 2: Orders and stock

Turn a cart into an order that reserves stock.

Stock is not guarded at all, so two buyers can both get the last item. `Inventory.reserve` decrements stock only if enough is there, atomically. `Order` is created only if every line reserves. The reserve step is one atomic check-and-decrement, so stock cannot go negative. Reserved stock must expire if payment never arrives, or it stays locked forever.

Stage 3: Paying: Strategy

One interface for every payment method.

Payment methods differ in detail but the order only needs 'charge' and 'refund'. A `PaymentMethod` interface with `charge` and `refund`; `CardPayment` and `UpiPayment` implement it. A wallet is one new class (**Strategy**), and the order never changes. Charging may succeed while the response is lost. Use the order id as an idempotency key.

Stage 4: Order states, events and shipping

Move an order forward and react to it.

An order can be cancelled after it ships, and nobody is told when its status changes. `OrderState` allows only legal moves (`PlacedState` can pay or cancel, `ShippedState` can only deliver). Listeners (email, inventory, analytics) are called on every change, and `ShippingService` creates the shipment. Illegal moves are impossible by construction (**State**), and side effects hang off events (**Observer**). Listeners run in the request. Move slow ones (email) to a queue.