Checkout process and inventory management in a distributed e-commerce system

 Let’s dive into the checkout process and inventory management in a distributed e-commerce system.

Checkout Process

The checkout process in an e-commerce system typically involves the following steps:

  1. Adding items to the cart: Customers browse the online store, select products they wish to purchase, and add them to their cart.
  2. Providing shipping and billing information: Customers enter their shipping address, billing address, and contact details.
  3. Selecting a payment method: Customers choose their preferred payment option, such as credit card, PayPal, or digital wallets.
  4. Reviewing the order: Customers have an opportunity to review the selected items, quantities, prices, and shipping options before proceeding.
  5. Confirming the purchase: Customers confirm the purchase, and the order is processed.

Inventory Management

Inventory management in a distributed e-commerce system involves real-time tracking and management of stock across various storage facilities. Here are some key aspects:

  1. Distributed Inventory Management (DIM): This strategy ensures products are available where and when they’re needed, without excessive overstocking. It leverages multiple storage locations to bring products closer to the end consumer.
  2. Real-time Inventory Tracking: With DIM, businesses gain real-time visibility into their inventory across all fulfillment centers. This helps in making informed decisions about stock levels, replenishment, and demand forecasting.
  3. Scalability and Flexibility: As businesses grow, so do their inventory management needs. DIM provides the scalability and flexibility required to adapt to changing business needs without significant investments in physical infrastructure.

Temporary Inventory Hold

When a customer adds an item to their cart, you might want to temporarily hold that item in your inventory to ensure it’s available when the customer decides to purchase. Here’s a high-level overview of how this could work:

  1. Add to Cart: When a customer adds an item to their cart, you create a temporary hold on that item in your inventory. This reduces the available stock of that item by the quantity added to the cart.
  2. Timeout: The temporary hold lasts for a certain period of time (e.g., 15 minutes). If the customer doesn’t complete the purchase within this time, the hold is released, and the item becomes available for other customers to purchase.
  3. Purchase: If the customer completes the purchase within the timeout period, the temporary hold is converted into a permanent deduction from your inventory.

This approach ensures that customers can always purchase the items they’ve added to their cart, while also preventing overselling. However, it requires careful management of the timeout period to balance the needs of the customer currently shopping with the availability of stock for other customers.

Remember, implementing these processes requires careful design and coordination between microservices. Each microservice needs to be aware of the actions it needs to take in case of failures. This can be achieved through a combination of event-driven architecture and asynchronous messaging. Also, these are general strategies and might need to be adjusted based on your specific use case.


Let’s consider an example where we have an Inventory table and a Cart table in our database. The Inventory table holds information about each product and its available quantity. The Cart table holds information about the products added to a user’s cart and the timestamp when they were added.

Here’s a simplified schema for these tables:

CREATE TABLE Inventory (
    ProductID INT PRIMARY KEY,
    ProductName VARCHAR(255),
    QuantityAvailable INT
);

CREATE TABLE Cart (
    UserID INT,
    ProductID INT,
    Quantity INT,
    AddedAt TIMESTAMP,
    PRIMARY KEY(UserID, ProductID)
);

When a user adds a product to their cart, you would insert a row into the Cart table and decrease the QuantityAvailable in the Inventory table by the quantity added to the cart.

BEGIN TRANSACTION;

UPDATE Inventory
SET QuantityAvailable = QuantityAvailable - @quantity
WHERE ProductID = @productID;

INSERT INTO Cart(UserID, ProductID, Quantity, AddedAt)
VALUES (@userID, @productID, @quantity, CURRENT_TIMESTAMP);

COMMIT;

You would also start a timer when the product is added to the cart. If the user doesn’t complete the purchase within a certain time limit (say, 15 minutes), you would release the hold on the inventory:

BEGIN TRANSACTION;

UPDATE Inventory
SET QuantityAvailable = QuantityAvailable + @quantity
WHERE ProductID = @productID;

DELETE FROM Cart
WHERE UserID = @userID AND ProductID = @productID;

COMMIT;

If the user completes the purchase within the time limit, you would simply delete the row from the Cart table without increasing the QuantityAvailable in the Inventory table.


Let’s consider a scenario in an e-commerce application where a user adds an item to their cart, which triggers a temporary hold on the item’s inventory. If the user doesn’t complete the purchase within a certain time limit, the system should automatically release the hold on the inventory. This is a timeout scenario.

Here’s how you could implement this with a compensating transaction:

  1. Add to Cart: When a user adds an item to their cart, you start a timer (this could be implemented using a background job or a scheduled task). You also create a temporary hold on the item in your inventory, reducing the available stock of that item by the quantity added to the cart.
function addToCart(userId, productId, quantity) {
    // Start a timer
    setTimeout(() => {
        releaseInventoryHold(userId, productId);
    }, HOLD_DURATION);

    // Create a temporary hold on the item in the inventory
    inventoryService.holdItem(productId, quantity);
}
  1. Timeout: If the user doesn’t complete the purchase within the timeout period (say, 15 minutes), the releaseInventoryHold function is called. This function checks if the user has completed the purchase. If not, it releases the hold on the inventory, making the item available for other customers to purchase.
function releaseInventoryHold(userId, productId) {
    // Check if the user has completed the purchase
    const purchaseCompleted = orderService.isPurchaseCompleted(userId, productId);

    // If the purchase is not completed, release the hold on the inventory
    if (!purchaseCompleted) {
        inventoryService.releaseHold(productId);
    }
}

This is a compensating transaction because it undoes the changes made by the addToCart function if the user doesn’t complete the purchase within the timeout period.

Please note that this is a simplified example and might need to be adjusted based on your specific use case. For example, you might want to handle cases where the available stock becomes negative, or where two users try to add the same product to their carts at the same time. You might also want to consider using a more robust queuing or messaging system to handle the timeouts. Remember to always test your code thoroughly to ensure it behaves correctly under various scenarios. Also, the actual implementation of inventoryService.holdItem, inventoryService.releaseHold, and orderService.isPurchaseCompleted would depend on your specific application and infrastructure. This example assumes that these methods are available and correctly implemented. 

Vikash Chauhan

C# & .NET experienced Software Engineer with a demonstrated history of working in the computer software industry.

Post a Comment

Previous Post Next Post

Contact Form