Varnish Cache empties cart in Magento

Varnish Cache empties cart in Magento

August 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Varnish Cache empties cart in Magento

Varnish Cache Issues with Magento: Why Your Cart Keeps Clearing

Are you facing persistent cart clearance issues while using Varnish cache with your Magento store? You’re not alone. Let’s dive into the problem and explore possible solutions.

The Problem: Persistent Cart Clearance in Magento + Varnish

Wolfgang, a fellow developer, recently faced this frustrating issue:

We are running an Amazon Web Service EC2 Magento instance with RDS and Varnish Cache and nginx. We are currently facing the problem that the cart is empty when a user tries to add a second item to the cart. The session is dropped because a new Cookie is set.

This happens due to a common misconfiguration in the VCL (Varnish Configuration Language) file.

Understanding the VCL File: Key Sections

The provided VCL file contains several sections that can affect your Magento + Varnish setup:

1. Vcl_recv

This section handles incoming requests. It checks for specific conditions and decides whether to pass, pipe, or return a request based on those conditions.

sub vcl_recv {
    #...
}

The Culprit: Unnecessary Cookies

In your VCL file, you have this line:

unset req.http.Cookie;

This line removes all cookies from the request before it reaches Magento. While this is useful for caching static content, it wipes out essential session data when a user adds items to their cart.

To resolve this issue, you need to ensure that Varnish preserves necessary cookies while still providing caching benefits:

  1. Add an Exception for Session Cookies

    Update your vcl_recv section to exclude session cookies from being unset:

    if (!req.http.Cookie ~ "session_id=") {
        unset req.http.Cookie;
    }
    
  2. Use Varnish’s Built-in Magento Integration

    Consider using Varnish’s official Magento integration, which handles caching and cookie management more effectively.

    You can find detailed guides on this at https://laravelcompany.com/varnish-magento. This plugin ensures optimal performance without compromising session data or user experience.

  3. Review Your Varnish Configuration

    Ensure that your vcl_recv, vcl_fetch, and other relevant sections are correctly configured to handle Magento’s dynamic content and sessions.

Conclusion: Fixing the Issue

To fix this persistent cart clearance problem:

  • Update your vcl_recv section to preserve session cookies.
  • Consider using Varnish’s official Magento integration plugin for better handling of caching and cookie management.
  • Review your entire Varnish configuration to ensure it aligns with Magento’s requirements.

Remember, proper configuration is key when combining powerful tools like Varnish and Magento. Always refer to the documentation and official guides to maintain an optimal setup.

Happy coding!

Checkout Cache Configuration Deployment

Our network