0

In rare cases we have category pages showing "We can't find products matching the selection" while actually the category contains products. This happens when the category is visited while Elasticsearch is doing a reindex. The empty page with the message is then cached instead of the page containing products. If we manually remove the page from cache it is showing products again. While this might be expected behaviour (Varnish just caches the page), I would like to know if someone has an idea on how to fix this. For example, could we let Cache/Varnish skip pages that contain this message?

1 Answer 1

0

Varnish is capable of looking at the status code and the reason message. If those are specific, a so-called hit for miss can be triggered which temporarily prevents Varnish from caching.

Can we assume the error while ElasticSearch is indexing will be a 404 Not Found? If that's the case, here's the VCL code to use:

sub vcl_backend_response {
    if(beresp.status == 404) {
        set beresp.ttl = 120s;
        set beresp.uncacheable = true;
        return(deliver);
    }
}

This code prevents any HTTP 404 from being cached. If the reason string is specific to the ElasticSearch indexing, we can adapt the VCL code as follows:

sub vcl_backend_response {
    if(beresp.status == 404 && beresp.reason == "some-specific-value") {
        set beresp.ttl = 120s;
        set beresp.uncacheable = true;
        return(deliver);
    }
}

Varnish has no way of accessing the response body as a string, so for now the status code and reason string are the only mechanisms to identify the pages to exclude from the cache. You can also try to match response headers if they expose the desired behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.