3

Why Does Drupal put the cached data in the database? It's hard too understand it, because the database can easily cause performance bottleneck.

8

Drupal core comes out of the box with a good caching system that will handle low level usage. Using the database as cache, you could easily create a site with hundreds/thousands of daily visitors, which is enough for many. This is a good default, as it's easy to setup, but more advanced caching strategies for high performance are supported:

  • For both Drupal 6 and 7 there are modules to improve caching by using memcache instead the database for some of the caching.
  • For Pressflow 6 and Drupal 7 core you can setup external cache like Varnish.

So Drupal really has a lot of caching possibilities, but in order to use the more advanced for high performance, you need to do some extra work. This will also require various server setup anyways. But making the default easy to use really is preferred.

11

The question that asked here is not whether the database cache can be replaced by something else, but why Drupal caches database information in the database. A reason that is not mentioned here is that although database information is again cached in the same database, it is not cached in the same format. The original data, such as node information or theme information, is stored in a kind of normalized form, which makes it easy to edit and query data. The cached values are stored in a way that reduces the amount of processing needed to display a page.

Let's say a page view consists of the following simplified steps:

  1. Fetch information from database
  2. Manipulate information to make it suitable for display
  3. Display the result

Now Drupal stores the result of step 2 into the database, so on subsequent page views, steps 1 and 2 do not have to be executed. This saves time and processing power. When the original data changes, the cache needs to be cleared.

  • A good, though this answers how, not why – googletorp Aug 8 '11 at 7:08
  • 1
    I have added that it saves time and processing power, thus giving the reason why caching in the database is useful. – Jorrit Schippers Aug 8 '11 at 7:56
  • You could cache the same thing else where. It's not unique for the database to cache something ready for rendering. That's exactly what Varnish does, only a lot faster, since no processing at all is needed. – googletorp Aug 8 '11 at 8:15
5

Fetching cached data from the database is easy and fast enough because they are simple SQL requests. Also, most of the results are cached in memory by the SQL caching system.

There are also other caching system, but they are not as good as database for a default caching system:

  • File system: you have to deal with data coherence, file permission, file lock, file management.
  • Memory: is faster than database, but you need extra setup. Thus this one should not be the default caching system.
  • Nice point there, the first answer which explains why the default behaviour is default. – Scorchio Aug 9 '11 at 14:45
4

Well in fact the drupal cache system is pluggable in drupal6 and drupal7, which means that you can put any kind of caching system behind it.

By default drupal uses a database based caching system, but you can replace it for example with the memcache module with a memcache, which scales much better then the database for the cache.

If you want you could even write a file based one, which will probably be must slower.

2

A very simple explanation of why is that it provides a caching interface which improves performance which by default doesn't add any more layers to a Drupal site.

Other caching mechanisms may be better, faster or whatever but they would introduce additional requirements which would not be needed for many Drupal sites.

Luckily Drupal is made so that the caching system can be swapped for those who need it.

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.