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.

Drupal on MongoDB is more than just a new database option—it redefines how Drupal stores and retrieves data. Traditional Drupal relies on relational databases like MySQL or MariaDB, where data for a single entity is fragmented across multiple tables. For sites with high numbers of authenticated users, this approach can lead to unacceptable latency.

With MongoDB, each Drupal entity instance is stored as a single JSON object, including all revisions, translations, and fields. This simplifies data retrieval, ensuring a performance boost for authenticated user-heavy sites.

If Drupal is to remain competitive, it must embrace this approach to support the growing demand for personalized, user-centric experiences.

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.

SQL vs. NoSQL databases

The scalability of NoSQL databases like MongoDB sets them apart from traditional relational databases such as MySQL or MariaDB. While relational databases are typically single-server solutions, MongoDB supports horizontal scaling—allowing for distributed setups with thousands of servers acting as a unified database.

Relational vs. document-based storage

In Drupal’s relational model, entity data is scattered across multiple tables. For instance, different chapters of a book (representing an entity) are stored separately, making retrieval inefficient. MongoDB, in contrast, stores entire entity instances in a single JSON object, akin to keeping a complete book in one place. This approach is inherently more natural and efficient, especially for sites with numerous authenticated users.

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@finalist.nl', 
       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@finalist.nl', 
       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 isn’t enough to optimize Drupal for authenticated users. Developers must rethink how data is stored. Traditional caching mechanisms like Redis work well for anonymous users but fall short for authenticated ones due to dynamic content requirements.

MongoDB’s flexibility allows developers to design storage schemas tailored to application usage, enabling faster queries with minimal complexity. Tools like materialized views can further optimize data retrieval by precomputing and storing the results of complex queries.

Why MongoDB for Drupal?

MongoDB offers unique advantages that align with Drupal’s evolving needs:

  1. Horizontal scaling: Easily scale databases by adding servers, supporting massive concurrent user bases.
  2. Integrated file storage: Store user-uploaded files directly in MongoDB, simplifying hosting setups.
  3. Built-in full-text search: Eliminate the need for separate search solutions like SOLR.
  4. AI capabilities: Leverage MongoDB’s support for AI vectors to integrate advanced search and personalization features directly into Drupal.

Performance testing

In comparative tests for loading nodes:

  • MongoDB (7.0): Average query time of 7.364 microseconds
  • MariaDB (10.6): Average query time of 14.596 microseconds

Both databases handled 50K nodes with identical fields. While traditional setups like MariaDB have been optimized for decades, MongoDB’s out-of-the-box performance still surpasses them, even without dedicated optimization.

Current status

  • The MongoDB driver for Drupal is available as a contrib module for Drupal 11.
  • Over 99% of core tests pass on MongoDB, with a few exceptions requiring minor adjustments.
  • The project is poised for inclusion in Drupal core, pending community support.

Conclusion

The demand for personalized user experiences is reshaping the web development landscape. Drupal must evolve to meet this demand, and Drupal on MongoDB is a pivotal step forward. By embracing NoSQL, developers can unlock new performance and scalability possibilities while simplifying hosting setups and enabling advanced features.

If you believe in the potential of Drupal on MongoDB, join the conversation in the Drupal community. Together, we can shape the future of Drupal for the next generation of web applications.

For more details, find me on Drupal Slack in the #mongodb and #contribute channels.