Magento 2 varnish not caching, delivers new cookie each time

Magento 2 varnish not caching, delivers new cookie each time

July 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 varnish not caching, delivers new cookie each time

Varnish Caching for Magento 2: Troubleshooting Cookie Issues

Caching is a critical performance optimization technique used by e-commerce platforms like Magento 2 to improve page load times and reduce server resource usage. When configured correctly, Varnish cache can significantly enhance the user experience of your online store. However, when facing issues such as different cookies being set each time or slow website loading even after reloading, it’s essential to troubleshoot and address these concerns efficiently.

Caching Configuration Analysis

Upon examining your provided configuration files, several observations can be made:

  1. Varnish Configuration: Your VCL (Varnish Configuration Language) file appears well-structured, following best practices for Magento 2 integration. It includes proper handling of cookies, compression filters, and cache invalidation mechanisms.

  2. Cache Status: The output from bin/magento cache:status indicates that most of the critical caches are enabled, including config, layout, database, EAV, and translations.

  3. Configuration Settings: The configuration parameters for full page caching and Varnish settings look appropriate, with the caching application set to 2 (which is the recommended value for production mode).

Common Issues and Solutions

  1. Cookie Management

The primary issue in your case seems to be related to cookie management. Varnish, by default, does not cache pages that set or change cookies. However, in your scenario, new PHPSESSID cookies are being generated with each request, even though you have configured Varnish to handle them appropriately.

To address this issue, ensure that the following steps are implemented:

a. Clear all Magento 2 caches using the command bin/magento cache:flush.

b. Verify that the session save handler in your php.ini file is set to either “files” or “memcache” (not “redis” for Varnish integration). You can check this by running php -i | grep session.save_handler.

c. If using Redis as a session storage, ensure proper configuration and compatibility with your Varnish setup.

  1. Slow Website Loading

There could be several reasons behind the slow loading of your Magento 2 website even after reloading:

a. High Server Load: Check the server load by running top or htop commands in the terminal to determine if the server resources are being overutilized.

b. Database Performance: Analyze the database performance using tools like MySQL EXPLAIN statements or slow query logs to identify any bottlenecks.

c. Misconfigured Caching: Verify that your Varnish cache is properly configured and functioning as expected. Monitor the cache hit rate using varnishstat tool to ensure efficient caching.

d. Third-party Extensions: Disable any third-party extensions temporarily to rule out performance issues caused by them.

Detailed Investigation Steps

To further investigate the issue, follow these steps:

  1. Enable Varnish cache debugging:

    • Add ?X-Magento-Debug=Y at the end of your Magento 2 URL.
    • Check the HTTP headers for X-Cache and X-Cache-Hits to determine if Varnish is caching the requests correctly.
  2. Analyze Varnish logs:

    • Enable detailed logging in your Varnish configuration by adding log { std; } at the end of the file.
    • Monitor the logs to identify any errors, warnings, or critical information related to cache behavior.
  3. Verify cookie handling:

    • Inspect the cookies set by your Magento 2 store using browser developer tools (Chrome DevTools or Firefox Network tab).
    • Ensure that the PHPSESSID and other relevant cookies are being handled correctly by Varnish according to your configuration settings.
  4. Test cache invalidation:

    • Attempt to purge the cache for specific URLs or resource types using varnishadm tool or Magento 2’s built-in cache management system.
    • Observe if the new PHPSESSID cookies are generated after the cache is cleared.

Best Practices and Further Resources

  1. Magento 2 Full Page Cache Configuration: Review the official documentation on full page cache configuration for Magento 2 at https://docs.magento.com/user_guide/performance/full-page-cache.html.

  2. Varnish with Magento 2 Guide: Refer to the comprehensive guide provided by Laravel Company, which covers various aspects of integrating Varnish with your Magento 2 store at https://laravelcompany.com/magento-varnish-guide.

  3. Performance Optimization Techniques: Explore additional performance optimization techniques such as code minification, image optimization, and CDN integration for further improvement in your Magento 2 website’s loading speed.

Conclusion

In conclusion, addressing the cookie management issue and performing a thorough analysis of potential bottlenecks can help resolve the caching-related performance concerns in your Magento 2 store. Implementing best practices, utilizing official documentation, and leveraging expert guides like those provided by Laravel Company will enable you to achieve optimal Varnish integration for enhanced user experience and improved e-commerce performance.

Remember to periodically review and update your cache configuration settings as per the latest best practices and recommendations from Magento and Varnish communities. Regular monitoring of your store’s performance metrics, including cache hit rates and server resource utilization, is crucial for maintaining an efficient and responsive online shopping platform.

Magento 2 Catalog Cache Database

Our network