Varnish not keeping cache for more than a few minutes

Solution 1:

If you're using the default VCL from Magento and have changed TTL to highest in Magento admin, the only plausible explanations of early expired pages would be:

  1. Not enough storage allocated to Varnish. That is, a newly cached page will evict another page's cache due to cache storage starvation. To check if such a thing is happening, run varnishstat and look for MAIN.n_lru_nuked. If it's present and has a non-zero, positive value, it means that more than likely your cache storage size setting for Varnish is not enough to hold all the pages in the cache.

  2. Something clears up your cache. This may be a custom cron for clearing caches set up by devs, third-party modules with bugs, custom code, etc. Here's for example, SwissUpLabs SEO module, at some point, did it.

To troubleshoot and find the "something that clears cache", you might have to resort to... not surprisingly, a third-party plugin. The Full Page Cache Warmer for Magento 2, among its features, has one excellent where you can see the log of cache clears done by Magento code, including plugins. By investigating it, you can find if it's a faulty module that clears cache unnecessarily.

But before you resort to using the mentioned module, you can run the following for some time, e.g. 10 minutes:

varnishlog -g request -q 'ReqMethod eq "PURGE" or ReqMethod eq "BAN"'

If anything shows up when running this command, you might want to continue with the warmer plugin to investigate what exactly is issuing those requests that result in cleared caches.