assume 100M DAU, 50% place an order daily, read write ratio is 10:1. Suppose we have 100k items.
TPS
write: 100M / 2 /10^5 = 500 transacptions/s
read: 500 * 10 = 5000
Storage:
Inventory management
Order management
Inventory (partition on id+warehouse_id, index on category, warehouse_id)
Order (can contain multiple inventory, index on status + user_id)
OrderInventory (index on order_id)
Warehouse
Customer order
customer requests listing of the items interested via order service, order service retrieves the details from inventory service. If the customer places an order, order service calls inventory service to update the database.
Replenish
The replenish service scans the database hourly, if it sees an item is low in stock, it puts a message on the message queue. The workers pull from the queue and invokes order service to place a replenish order against the supplier.
Inventory updates
when updating the inventory, concurrent update may occur, we should use transactions to check the quantity and decrement.
BEGIN TRANSACTION;
SELECT Quantity FROM Inventory WHERE InventoryID = 123;
UPDATE Inventory SET Quantity = Quantity - 10 WHERE InventoryID = 123;
COMMIT;
Monitoring/reporting
When a database updates appen, the inventory service push a message to the message queue, the event sourcing service can then build the inventory table snapshots, also other reporting services can consume the message for reporting purposes.
{
inventory_id: 123
quantity: -1
warehouse_id: 23
timestamp: 12321413
order_id: 123123
}
Explain any trade offs you have made and why you made certain tech choices...
We can scale the database by partition the inventory table by id+warehouse_id because the inventory is grouped by locations.
We can consider supporting the hold feature where the item is held for a certain time range after being put to the cart.