Does keycloak support mongodb as a database?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Keycloak is an open-source Identity and Access Management (IAM) tool that provides features such as single sign-on with identity brokering, user federation, client adapters, and more. It is especially popular due to its flexibility and integrative nature, seamlessly working with several authentication protocols like OAuth 2.0, OpenID Connect, and SAML 2.0.
Does Keycloak Support MongoDB as a Database?
As of the latest updates, Keycloak does not natively support MongoDB as a database for its data storage. Keycloak primarily relies on relational databases (RDBMS) for handling its data where it can effectively manage and query relational data structures. Officially supported databases include:
- H2 (embedded, primarily for development purposes)
- PostgreSQL
- MySQL
- MariaDB
- Oracle
- Microsoft SQL Server
Technical Explanation
Keycloak uses relational databases for persistence due to the complex nature of relationships between entities like Users, Clients, and Roles that align well with SQL-based systems. These relationships make a relational approach more efficient for data integrity and transaction management compared to NoSQL counterparts like MongoDB, which utilizes a document-oriented storage model.
Attempted Workarounds and Limitations
While MongoDB itself isn’t directly supported, developers have attempted workarounds, such as using MongoDB as an external store while maintaining metadata in a relational database. This is not a straightforward process and requires a substantial understanding of both Keycloak’s internal processes and the MongoDB schema.
Challenges:
- Relational Queries: Keycloak extensively uses complex queries that join multiple tables. Reframing such queries for a document-based database like MongoDB can lead to inefficient data processing.
- Transactions: MongoDB does not support multi-document transactions as naturally as SQL systems did initially. This can pose issues given Keycloak's dependency on transaction management.
- Stability and Compatibility: Even if MongoDB is integrated, the lack of official support means more work to ensure stability and maintenance across Keycloak updates.
Current Alternatives and Extensions
Given these limitations, those looking to utilize MongoDB for Keycloak might consider alternative methods that better align with MongoDB's architecture or explore using it for specific components of an identity architecture without replacing the primary Keycloak database.
The focus has instead been on enhancing Keycloak with extensions or custom integrations that leverage its SPI (Service Provider Interfaces) to meet additional needs without altering the fundamental database system.
Summary Table
| Aspect | Keycloak Support for MongoDB |
| Native Support | No |
| Database Types Supported | RDBMS (e.g., Postgres, MySQL) |
| Primary Challenges | Relational queries, transactions Compatibility issues |
| Workarounds | External document storage along with SQL (Limited adoption) |
| Recommendation | Use official RDBMS support for better integration and stability |
Conclusion
In summary, while MongoDB offers powerful capabilities for certain use cases, Keycloak is designed to function efficiently within a relational database environment. This is due to its requirement for complex relational data manipulation and transactional integrity, which aligns more suitably with RDBMS rather than NoSQL systems like MongoDB.
For administrators and developers considering implementing MongoDB, it may often be practical to leverage Keycloak’s official support for RDBMS while architecting ways to incorporate MongoDB where document-oriented storage is optimized—without altering the core database that Keycloak depends on for its most crucial functions. Such an approach ensures that you maintain compatibility and receive ongoing updates from the Keycloak community.
Related reading
- Does MySQL foreign_key_checks affect the entire database?
- Does MySQL ignore null values on unique constraints?
- Does MySQL index foreign key columns automatically?
- Does MYSQL replication work in real time?
- Does zmq need integrity checks in application layer?
- downloading ResNet50 in Keras generates SSL CERTIFICATE_VERIFY_FAILED
- Does Redis support cross replication between master/slave nodes?
- does slave-skip-errors avoid remove errors from the logs

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.