There are many new types of databases available today, and EventStoreDB as a state transition database is one of them. In contrast, MongoDB has been around for a while and is a popular document database.
What is the relationship between EventStoreDB and MongoDB? What are their respective strengths? And do they work well together?
Keep reading to find out more!
EventStoreDB is a database built for event sourcing. It is a state transition database that stores historical events of facts that occurred in an application as an event log. Unlike traditional databases, which usually only store the current state and overwrite past data, EventStoreDB retains the past data.
A state transition database consists of two major components:
OrderPlaced
, OrderPaid
, OrderDelivered
). Because they capture business actions taken, they are more granular and fundamental than state changes that are based on database operations. (e.g. OrderInserted
, OrderUpdated
, OrderDeleted
)Strength | Explanation |
---|---|
Expose Reason Behind each Decision | Millions of decisions are made in an organization each year. EventStoreDB retains these decisions as events in named sequences/streams, which can expose key reasons of success and failures. |
Time Travel to the Past, Present, and Future | Storing the history of events allows EventStoreDB to reconstruct the past and present. Additionally, with a sufficient number of events, the future can be forecasted using machine learning. |
Great Support for Eventual Consistency | EventStoreDB provides excellent support for eventual consistent operations in downstream systems. This simplifies issues such as dual writes, message loss, duplication, and ordering. |
Decouple Complex and Monolithic Systems | EventStoreDB provides the freedom to reconstruct events into any schema, database model, or infrastructure, making it easy to decouple business functions from a specific system. |
Guide Developers to Better System Design | EventStoreDB aligns perfectly with practices such as Event Storming and Event Sourcing. These practices enable developers to design processes and application code that are more closely aligned to the business. |
EventStoreDB shines as the source of truth of core business data, particularly in complex systems that frequently change and have myriads of business constraints.
MongoDB is a document database that focuses on flexibility and scalability in data management. Unlike relational databases, MongoDB stores data in schema-less and JSON-like documents, allowing for more intuitive representation and querying of complex data structures.
A document database like MongoDB comprises two major components:
Insert
, Update
, Delete
, and Find
, which align with the Create, Read, Update, and Delete (CRUD) operations in database management.MongoDB also supports advanced features like aggregation for data analysis, replication for data availability and redundancy, and sharding for horizontal scalability.
Strength | Explanation |
---|---|
Flexibility of Documents | Documents in MongoDB are schema-less, which makes it easy to update to any structure during development and for future changes |
Developer Friendly JSON Data Model | Documents in MongoDB are structured in JSON, making them easy to process with modern languages like JavaScript. This reduces the need for developers to switch context between the application model and the database model. |
Great Support for Horizontal Scalability | MongoDB supports various scaling techniques, such as replication and sharding, which allow it to serve data to a large number of audiences. |
Rich Set of Query Operators | MongoDB supports a wide range of document query operations such as filtering, sorting, aggregation that makes it easy to perform data analytics |
MongoDB shines as a widely distributed storage for data with a simple model that doesn’t have complex transactional requirements but requires high scalability.
EventStoreDB and MongoDB work extremely well together in practice, where their benefits complement each other.
EventStoreDB is typically used as the source of truth or the system of record. It acts as an authoritative source where data is stored and serves as the only location for updates. Because:
MongoDB, on the other hand, is perfect as the cache or what we call the read model. A downstream read-only view or projection of the source of truth. Because:
For example, EventStoreDB is suitable for storing credit card transactions, while MongoDB would be perfect for storing account summary JSON tailored to a particular user web page.
At a high level, an application change is saved as an event in EventStoreDB. The event is then published to a custom projector service, which subscribes to new events and updates the MongoDB replica set.
Here is the process description in detail:
EventStoreDB and MongoDB work well together when EventStoreDB serves as the source of truth and MongoDB is used for projections or as a read model.
This maximizes the fidelity of the data stored in the source of truth, whilst enabling the distribution of that data to optimized forms to a large number of audiences.