Designing Scalable E‑Commerce Shopping Carts: From Cookies to Redis
This article explores the core functions of e‑commerce shopping carts and compares client‑side storage methods such as Cookies and LocalStorage with server‑side solutions like Redis and MySQL, offering guidance on choosing the appropriate approach based on business scale and reliability requirements.
In e‑commerce platforms, the shopping cart is a core feature that lets users add items for later selection and checkout, directly impacting user experience and sales.
The typical shopping cart provides the following functions:
Add items with quantity selection.
Delete unwanted items.
Modify item quantities.
View all items in the cart.
Proceed to checkout with selected items.
Cart data can be stored for logged‑in users or as a temporary (guest) cart. For example, JD.com allows both, while Taobao requires login before adding items.
1. Implementation of Temporary (Guest) Cart
When users are not logged in, cart data is usually stored on the client side, with each client maintaining a unique cart. Common client‑side storage solutions are Cookies and LocalStorage.
1.1 Cookie‑Based Cart Data
Each request automatically includes Cookie data, allowing the server to read the cart information stored in the Cookie. This simple approach enables the server to handle all cart operations (add, remove, merge) based on the Cookie contents.
1.2 LocalStorage‑Based Cart Data
Data stored in LocalStorage can only be accessed by the client, which must then transmit it to the server. Although implementation is more complex because both client and server need business logic, LocalStorage offers a much larger capacity than Cookies (which are limited to ~4 KB) and does not add overhead to every request, saving bandwidth.
Choosing between Cookie and LocalStorage depends on the business scenario:
For small‑scale e‑commerce sites, Cookies are a simple and effective choice.
For larger platforms with many items per user, LocalStorage is more suitable.
2. Implementation of Logged‑In Cart
For logged‑in users, the cart data must be stored on the server, either in a cache such as Redis or in a database like MySQL. The diagram below shows a design that stores cart data in Redis.
If a temporary cart exists, it needs to be synchronized to the server after the user logs in. The following flow diagram illustrates the synchronization process for Cookie‑based guest carts.
After a successful login, the client sends the cart data to the server, which parses the Cookie, saves the data to Redis, and clears the Cookie, achieving data merging.
3. Server‑Side Cart Data Storage Options
Cart data can be stored in MySQL or Redis, depending on the scenario:
MySQL : Offers high reliability and rich query capabilities, making it convenient for statistics and reporting. The example table design is shown below.
Redis : Provides fast, high‑throughput read/write operations. Suitable when absolute reliability is not critical and occasional data loss is acceptable, significantly improving performance and user experience.
For scenarios requiring both reliability and performance, a combined Redis + MySQL approach can be adopted.
Lobster Programming
Sharing insights on technical analysis and exchange, making life better through technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.