Drupal on MongoDB: more than just a new database option
Accelerating performance for authenticated users
For site owners who prioritize personalized user experiences, systems that rely on authenticated users are essential. Drupal stands out as a robust platform, particularly for its extensive out-of-the-box features tailored to environments like digital workplaces and intranets. As a content management framework, Drupal is uniquely equipped with ready-to-use building blocks and highly granular role management, making it an excellent choice for secure and dynamic systems. To unlock Drupal’s full potential or to scale it for even more complex data storage challenges, I believe it is essential to explore enhancements to the database backend, ensuring it continues to excel in meeting evolving data demands.
The solution? Drupal on MongoDB.
![avatar](https://p-techblog-cms.finalist.nl/sites/default/files/2024-11/drupal-picture.png)
Who am I?
My name is David Bekker (a.k.a. daffie), and I’m a seasoned Drupal core contributor with over 600 commit credits. I maintain Drupal’s Database API and database driver modules. My passion lies in open-source development, driven by a desire to create maximum business value.
When I sought a high-impact project to work on, I chose to develop a MongoDB driver for Drupal. One that stores entity instances as JSON objects. This project addresses Drupal’s evolving needs in a meaningful way.
User-centric innovation: Drupal’s next evolution
To understand why this shift is crucial, let's compare this transition to a challenge Drupal faced years ago: optimizing sites for mobile devices. Back then, significant changes were needed to enhance mobile usability. Now, we face a similar paradigm shift as the market evolves from sites for anonymous users to those centered on authenticated users. Drupal must adapt, and Drupal on MongoDB is the key to this transformation.
Beyond traditional use cases, Drupal on MongoDB is also ideal as a backend for iOS, Android, and JavaScript applications, providing personalized and scalable solutions.
Redefinition of data storage and retrieval
Drupal on MongoDB is more than just a new database option. It enhances Drupal’s ability to compete in a changing digital landscape.
Drupal’s robust entity system provides a solid foundation where everything is structured as an entity. Traditionally, Drupal leverages relational databases like MySQL or MariaDB, efficiently managing data across multiple tables. This approach performs well for sites with a large number of anonymous users. However, for sites with many authenticated users, the complexity of retrieving entity data from multiple tables can introduce performance challenges. Optimizing data retrieval can significantly enhance the user experience, making Drupal even more powerful for dynamic, user-centric applications.
With MongoDB, every Drupal entity instance is stored as a single JSON object, including all revisions, translations, and field data. This streamlined data structure allows for significantly faster retrieval, making Drupal a stronger solution for personalized, user-focused experiences.
As the market shifts toward authentication-driven sites, supporting MongoDB ensures that Drupal remains a competitive and scalable option. Rather than replacing Drupal’s strengths, this integration enhances them, allowing Drupal to meet modern performance demands while maintaining its flexibility and power.
Scalability and efficiency: why MongoDB makes sense for large Drupal projects
The scalability of NoSQL databases like MongoDB sets them apart from traditional relational databases such as MySQL or MariaDB. While relational databases typically rely on a single-server model, MongoDB supports horizontal scaling, enabling distributed setups with thousands of servers acting as a unified database. This architecture provides the performance needed for large-scale projects with millions of authenticated users.
As community-driven software, Drupal is built to support interactive, user-focused experiences, including forums, profiles, and content management. Traditionally, its relational model organizes data across multiple tables—similar to storing the chapters of a book separately in a library. This approach ensures data consistency and flexibility, making it highly effective for managing structured content.
However, as the demand for authentication-heavy sites grows, the way data is stored becomes a crucial factor in performance. MongoDB offers a more efficient alternative by storing entire entities as JSON objects—just like keeping an entire book intact rather than splitting it into separate chapters across different locations. This eliminates the need for complex table joins, significantly accelerating data retrieval and making MongoDB well suited for personalized dashboards and dynamic content feeds.
For small-scale sites, both relational and NoSQL approaches work. But when scalability, speed, and efficiency become priorities—particularly for sites with millions of authenticated users—MongoDB provides a natural and powerful solution for taking Drupal to the next level.
Example of a user entity stored in MongoDB
{
_id: ObjectId('664afdd4a3a001e71e0b49c7'),
uid: 1,
uuid: '841149cd-fe56-47c4-a112-6d23f561332f',
langcode: 'en',
user_translations: [ {
uid: 1,
uuid: '841149cd-fe56-47c4-a112-6d23f561332f',
langcode: 'en',
preferred_langcode: 'en',
name: 'root',
pass: '$2y$10$kjGuIsPOTDa2TseuWMFGS.veLzH/khl0SfsuZNAeRPRtABgfq5GSC',
mail: 'admin@example.com',
timezone: 'Europe/Amsterdam',
status: true,
created: ISODate('2024-05-20T07:37:54.000Z'),
changed: ISODate('2024-05-20T07:42:08.000Z'),
access: ISODate('2024-05-20T08:46:47.000Z'),
login: ISODate('2024-05-20T07:44:16.000Z'),
init: 'admin@example.com',
default_langcode: true,
user_translations__roles: [ {
bundle: 'user',
deleted: false,
langcode: 'en',
entity_id: 1,
revision_id: 1,
delta: 0,
roles_target_id: 'administrator'
} ]
} ],
login: ISODate('2024-05-20T07:44:16.000Z'),
access: ISODate('2024-05-20T08:46:47.000Z')
}
Optimizing data storage for performance
Switching to MongoDB alone is not enough to make Drupal a top-tier solution for sites with a high number of authenticated users. Developers must rethink how data is stored.
In traditional Drupal setups optimized for anonymous users, caching mechanisms like Redis compensate for slow database queries. However, for authenticated users, where content is dynamic and personalized, this approach falls short. Drupal itself needs to be fast, not just its caching layer.
MongoDB enables developers to store data in the way the application uses it, reducing the need for complex queries that slow down performance. Instead of relying on costly operations like joins and subqueries, simple and efficient queries should be the norm. Tools like materialized views—precomputed query results stored as database tables—help achieve this, ensuring faster data retrieval while keeping the database structured for high performance.
Why MongoDB for Drupal?
While many databases support JSON storage, MongoDB is the only one that fully meets Drupal’s needs. Its capabilities extend beyond basic JSON support, making it the optimal choice for storing entity instances efficiently.
Additionally, MongoDB offers several key advantages that align with Drupal’s evolving requirements:
- Horizontal scaling: Easily distribute database load across multiple servers, making it scalable for large user bases.
- Integrated file storage: Store user-uploaded files directly in the database instead of on the web server, simplifying hosting.
- Built-in full-text search: Eliminates the need for separate search solutions like SOLR, reducing infrastructure complexity.
- AI capabilities: Supports AI vectors, allowing for features like advanced search and personalization tailored to a site’s content.
Performance testing
Performance tests comparing MariaDB and MongoDB for loading Drupal nodes show a significant advantage for MongoDB:
- MongoDB 8.0: 1.355 microseconds average query time
- MariaDB 10.6: 14.596 microseconds average query time
Tests were conducted on 50K nodes with identical field structures. Even without specific optimizations, MongoDB outperformed MariaDB, demonstrating its potential to enhance Drupal’s speed and scalability.
Current status
- This website is already running entirely on MongoDB :-)
- The MongoDB driver for Drupal is available as a contrib module for Drupal 11, with over 99% of core tests passing.
- Discussions are ongoing to merge MongoDB support into Drupal core, pending community contributions.
Conclusion
As the web moves toward more personalized, user-centric experiences, Drupal must evolve to remain competitive. MongoDB is a key enabler of this evolution, providing faster, more scalable solutions for authenticated user-heavy sites.
By embracing MongoDB, Drupal developers can unlock new performance possibilities, simplify infrastructure, and build future-ready web applications.
Want to get involved? Join the conversation in the Drupal community via Slack in the #mongodb and #contribute channels. Let’s shape the future of Drupal together! 🚀