How Carts Remember Items
How a Shop Knows Which Cart Belongs to You
How do shopping carts work? A shop gives each visitor an identifier, and the server uses it to find that visitor's cart on every request. Without an identifier, the shop would have no way to link one page view to the next.
The identifier is usually a cookie, a small piece of data the browser stores and sends back with each request to the same site. The cookie holds a session ID, a random code that points to a session record on the server. The session record holds the cart contents.
This is why a cart survives clicking from page to page. HTTP, the protocol browsers use, treats each request as separate, so the shop needs this link to recognise a returning visitor. For the basic role of the cart itself, see what shopping cart software does.
Where the Cart Data Is Stored
Shops choose where to keep cart data, and the choice affects reliability and speed.
- Server session storage: the cart is held in memory or session files. This is fast, but the cart can be lost when a session expires or the server restarts, and it does not follow the shopper to another device.
- Database storage: the cart is saved as records in the database, linked to a customer account or session. It lasts longer and can appear on another device, but each change is a write to the database.
- Browser storage: the cart is kept in the browser's local storage. This needs no server memory, but the shop must still check prices and stock on the server, and the cart is lost if the browser data is cleared.
Many platforms combine these methods. A cart might sit in the database for a logged-in customer and in a session for a guest. Carts that are saved but never bought are a common issue, explained in cart abandonment.
Why Cart Pages Cannot Be Cached
Caching stores a copy of a page so it can be served again without rebuilding it. Product pages suit caching, because every visitor sees the same content. A cart page does not, because its content depends on the visitor's session. Serving a stored copy could show one shopper another shopper's items, so cart and checkout pages must be generated fresh for each request.
The result is a steady load on the server. Each cart update and checkout step runs application code and reads or writes the database. This is part of why stores need more capacity than brochure sites, as described in ecommerce hosting requirements.
Frequently asked questions
What Happens to My Cart If I Clear My Cookies?
If the cookie holding the session ID is deleted, the browser stops sending it. The shop can no longer link the visit to the old session, so the cart appears empty. Carts saved against a logged-in account are usually still available after signing in again.
Why Does My Cart Empty After a Few Days?
Sessions expire after a period of inactivity, and browser-based carts can be removed when browser data is cleared. Shops may also delete unused carts from the database after a set time. The exact period depends on the platform and its settings.