This article discusses the architectural challenges of managing real-time inventory in cross-border e-commerce, especially when dealing with unreliable upstream APIs. It presents a two-layer system design combining local caching with proactive invalidation for queries and distributed locks with asynchronous reconciliation for deductions to prevent overselling.
Read original on Dev.to #systemdesignPreventing overselling in e-commerce, particularly in cross-border scenarios, presents significant system design challenges. The core issue often stems from stale inventory data due to slow or inaccurate upstream APIs, coupled with the concurrency of customer orders. Traditional approaches like scheduled syncs or simple caching fall short when the source of truth is inherently delayed and prone to inaccuracies, such as "orderable quantity" versus actual physical stock or even inflated numbers during sales events.
The problem can be broken down into two main parts: ensuring fresh data for inventory queries and managing concurrent deductions reliably. Simply caching data speeds up reads but doesn't resolve the underlying data staleness. Relying on scheduled batch updates means a window of vulnerability where multiple concurrent requests can see outdated stock levels, leading to overselling before deductions can be processed.
Taocarts addresses this with a two-layer architectural approach: one for queries and another for deductions, tackling both read and write concerns in a distributed environment.
System Design Lessons
This solution highlights the importance of understanding the limitations of external dependencies (e.g., slow upstream APIs) and designing compensating mechanisms. A layered approach for reads and writes, using appropriate consistency models for each, can provide both performance and correctness. The combination of eventual consistency for queries and strong consistency via distributed locks for critical writes is a powerful pattern.