In a huge website, database's tables storing cache may become very large : is there a way to store cache in file system instead of DB ?

I'm talking about a solution that doesn't use external systems like varnish, simply an option provided by Drupal (a config for example). This doesn't seem documented and I'm not sure it exists !

Thanks !

  • 3
    The issue of cache tables becoming very large is fixed now, see the change record Database cache bins are now fixed size — no more unlimited growth – 4k4 Dec 29 '17 at 16:58
  • 1
    Ok, good improvement ! So with drupal there is no foreseen way to store cache into file system ? – Charles Dexter Ward Dec 31 '17 at 16:09
  • 1
    My comment was not to answer this, I don't know if this is a good idea and how to do it without writing your own cache backend. It was about how you started the question, if there is still a reason why you want to do this? – 4k4 Dec 31 '17 at 16:32

Drupal comes with a php-file based cache backend by default: \Drupal\Core\Cache\PhpBackend, the factory is available as the cache.backend.php service.

You would configure that to use that by default with $settings['cache']['default'] = 'cache.backend.php';.

That said, I wouldn't really recommend using that. PHP files are going to fill your disk as well (without a cleanup-mechanism that the database now has) and if you have a very large site then you probably also have multiple servers and then cache invalidation becomes a problem again, clearing the cache on one server won't clear it on others unless you use a shared file system and that is again slow.

The recommended approach IMHO for a big site is to use an in-memory cache backend like Redis or Memcache (varnish is not a cache backend that is used by Drupal, it's something that sits in front of it). The big advantage of those two is that they store their data only stored in memory (configurable in case of redis) and very fast to access due to that but still easy to for multiple servers. They also have built-in purging of data and a fixed size, so they will automatically delete less-frequently used cache entries. And while not perfect, they are far better at that than the default database backend now which just deletes old cache entries and keeps a fixed amount of caches.


To reduce the overall size of your caches you might want to try to compress them. There's a module for that which works with default database if you have zlib available with your php. Module: compressed_cache

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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