turpentine zero cart empty cart

Turpentine – Varnish

When it comes to performance improvement, no PHP-based Magento caching module can compare with Varnish, period! In short Varnish is  a reverse proxy and HTTP accelerator. Varnish stores and accesses cached content directly from system memory. That’s much different then storing and accessing cached content into/from files on a local disk/storage (Magento default). This article wont introduce Varnish itself it will give some insides on most common issues when Varnish software gets used with Magento 1.9.x.

varnish turpentine upment optimization

Without Varnish you wont be able to speed up page load, something that would impress your client big time! Fast page load is one of the most important ranking factor today. Make the store fast and everybody will be happy! If you want to read and learn more about Varnish software please visit.

Those luckiest who migrated their online stores to Magento2 already don’t have to worry about third party extensions and Varnish integration anymore. Magento 2 comes with built-in support for Varnish. You can check this article for some issues related to Varnish and Magento 2.

Turpentine Magento module out of the box

We have been using turpentine extension a lot as a connection between Magento1 and Varnish. This is one of the best extension to utilize caching between Magento1 and Varnish in order to significantly improve page load for your Magento 1 based online store. It comes with plenty of options that you can use in order to extend Varnish HIT rate.

Yes, but in order to get the best out of Varnish and Turpentine it you need to understand things behind the scene. That said many webmaster’s often give up on Varnish because they aren’t lucky enough to make Turpentine and Varnish working close together – out of the box. I find it very sad because Varnish isn’t beatable when it comes to speed optimization and caching. Here are some benchmarks based on Magento with and without Varnish.

Duplicate Frontend cookie (cart content is getting flushed)…Yikes!

This can be a real pain even for experienced Magento developers. You simple tried everything on staging envirnoment but you can’t replicate issue on your side!? On the other side your client reports back on daily basis that  a decent number of customers  can’t checkout – lots of people are seeing an empty cart at one point during checkout. Time for panic!

Don’t give up on Varnish at this point! If you use a cloud based Magento hosting with a load balancer in place  simple redirect traffic from Varnish  to one or to all of your backend  servers (Nginx) until you fix issue. To pass Varnish is relatively simple for an experienced system administrator and it shouldn’t take more then 1-2 minutes.

WHY The Heck is The Cart Getting Empty I Can’t Replicate Issue on My Side?!

There are many articles which describe this issue and you can find quite a number if you search google:  https://www.google.com/search?q=duplicate+cookie+issue+turpentine&pws=0&gl=us&gws_rd=cr   however most of these articles are old and outdated and you won’t find relevant informations.

This is considered to be a common problem for many Magento based online stores that use Turpentine and Varnish. The issue is related to  ‘Frontend’ cookie that gets duplicated at some point.  Magento Frontend cookie stores the current session ID. You can check all Magento standard cookies here. If another Frontend cookie gets issued the old session will break. Since the old session stores records of your cart content you will end up with an empty cart.

Why does the cart flush with Varnish in front?

In order to serve cached content directly to first time visitors we use Turpentine with the VCL fix disabled (please be aware that this will disable CSRF protection). This means that the generate_session subroutine in VCL will generate a Frontend cookie instead of Magento. If at some point Magento doesn’t notice that Varnish already issued a Frontend cookie, Magento will simple reissue another Frontend cookie.  This will break the initial session, as a result – empty cart content when you try to checkout.

You can see that duplicated Frontend cookie issue in Google Chrome inspector. Right click, anywhere in your chrome browser, inspect -> applications -> cookies, in the filter box search for ‘Frontend’. You will see this:

turpentine varnish cookie issue

As you can see from the image above this store has generated two frontend cookies. One cookie was created by Varnish (initial request, created for non-www domain) and one was created by Magento at some point (for www domain name). Both cookies were created with the same name but with different values. If we look closer we will notice that ‘Domain’ value is different: www.your-domain.com vs. your-domain.com! This is important!!

As we know a browser (IE, Chrome or FF) will hold only one session for the same domain and the session ID is stored in the Frontend cookie. If you have some items in your cart for an active session after your browser receives another cookie with the same name it will end – ‘overwrite’ the current session (cart content) and you will end up with a new session and zero items in your cart.

What is the solution?

The solution often depends on your current environment setup.  However, first thing you should check and which could lead to a successful solution is available within turpentine itself.

Normalize Host Target, Normalize Cookie Regex and Normalized Cookie Target are not set by default. Setting all tree fields to proper values will be the first thing to try. What we want is to force Magento to save session for a particular domain, either www or non-www version.

You can find settings to normalize cookies in magneto 1 backend:

system -> configuration -> turpentine -> caching options

Make sure that you set all 3 fields like pictured bellow:

normalize host tuprentine magento1

The setting above will force Magento1 to use only one domain value for the Frontend cookie! Magento1 wont conflict with Varnish, you will have one active session and your cart wont get flushed. Before you test new settings make sure that you flush all cookies in your browser . You shouldn’t be able to replicate duplicated cookie issue anymore.

Normalized Cookies Wont Help, Cart Content is Getting Flushed Again 🙁

Don’t worry, no need for panic yet. There is one more thing that you can check just after you tried to set turpentine properly. Many SEOs ( SEO stands from: Search Engine Optimizations) will advise that you should use either www or non-www version of your domain name. Whatever you decide make sure that you have a proper 301 redirect on place. Either from your www to your non-www domain or vice versa. This is an example of a wrong redirect:

This is not a critical error (example above) and as long as your visitors land on any of your store pages from the www domain version there won’t be any issues. Cart content will persist. But if any of your visitors land on a page from a non-www domain name the result will be a duplicated  frontend cookie with a different domain value. Cart content will flush again. You wont notice this issue if you pass requests to Varnish and if you disable turpentine. But we need Varnish 🙂

The new issued Frontend cookie will break the current session since it holds a new session ID and a new domain value. With Varnish in front and Magento1 in the backend that will happen for an improper redirect if a visitor lands on a new page from an external non-www backlink or a hard coded non-www on page link (any link within your store).

Check your redirects

Requests that come from a non-www domain value are requests from a completely different domain name, your-domain-name.com and www.your-domain-name.com are different for google. You will end up with an empty cart because your browser won’t keep two sessions open with the same cookie name: Frontend. Your session related to first Frontend cookie will be released (current cart content)  and a new session (empty cart content, new Frontend cookie) will be created.

There are different ways to fix an improper 301 or 302 redirect in Magneto in order to prevent a duplicated Frontend cookie issue when you use Turpentine and Varnish cache. It can be handled on code level or infrastructure level. This subject goes outside the scope of this article.

An example of a proper redirect would look like this:

You should be set now with Varnish and Turpentine. If this still didn’t fix the issue then it’s time consult an expert.

Share

Leave a Reply

Your email address will not be published.