Using MongoDB to Optimize Drupal Performance
Processing-intensive scripts or a sudden increase in traffic will inevitably result in your website slowing down. In our projects we’ve tried several ways to solve this problem:
- Tweaking the database server and setting up additional database servers;
- Using Memcached data caching system;
- Optimizing the MySQL server load by storing “hot” data in MongoDB.
Today’s Post is for everyone who wants to increase the performance of a website using MongoDB.
One of the biggest bottlenecks in Drupal is the storage organization for entities (nodes). Too many nodes in a project will result in very intensive queries to the database, especially if different node types are linked to each other. If we profile the queries, we’ll find out that most resources are consumed by retrieving the complete record for a node and its linked entities. Thus, the recommended way to load data is to use MongoDB as follows:
If a MongoDB server is unavailable, the data will be loaded from MySQL. This gives your website an extra layer of reliability – the system will be slower, but it will still work. It’s also important to note that we don’t store any data separately in MySQL and MongoDB. All the data is stored in MySQL and we only keep data in MongoDB, when absolutely necessary as illustrated by the next picture:
The above method is not difficult to implement and is well suited for any Drupal project, given the following recommendations:
- Don’t try to completely load a node from MySQL, only load nid to speed up the query;
- Use mongo_node_load ($nid) to load the node;
- Use views row style plugin for the views, i.e. load the nids from MySQL, the rest of data from MongoDB, then do the theming in the appropriate build mode.
This approach is suitable for any high load project, not necessarily a Drupal and PHP project. The key point here is the logic: a relational DB is only used to get the IDs for documents (nodes) and the actual documents are loaded from MongoDB. Besides, MongoDB has many functions, that you may find useful depending on your goals and the design of your application.
If you plan to use MongoDB, start using it from the earliest stages of your project. For example, if a site database is going to be huge and the estimated page generation time is 0.5 seconds you may have performance problems during the load testing phase. Consider using MongoDB to improve your site performance.
To keep this post short I will not go into the details any further, but I am ready to answer any questions you might have on the subject.