Why MongoDB is Consistent not available and Cassandra is Available not consistent?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
MongoDB and Cassandra are popular NoSQL databases that have been widely adopted for their scalability, performance, and flexibility. However, they are designed with different trade-offs in mind, particularly in the context of the CAP theorem, which addresses the trade-offs between Consistency, Availability, and Partition tolerance in distributed systems. Understanding these trade-offs helps to clarify why MongoDB is considered Consistent but not always Available, whereas Cassandra is Available but not necessarily Consistent.
The CAP Theorem
The CAP theorem states that in any distributed database system, you can provide only two out of these three guarantees at any given time:
- Consistency: Every read receives the most recent write or an error.
- Availability: Every request receives a response, without guarantee that it contains the most recent write.
- Partition Tolerance: The system continues to operate despite the presence of network partitions.
Both MongoDB and Cassandra are partition-tolerant systems, but they prioritize different CAP properties.
MongoDB: Consistency over Availability
MongoDB is designed to be a CP (Consistency and Partition tolerance) system. Here is how MongoDB manages consistency:
- Read Preferences: MongoDB ensures consistency using a primary-secondary model. By default, all writes and consistent reads go to the primary node. Optionally, reads can be performed against secondaries with
readPreferenceconfigurations, but this might lead to eventual consistency in exchange for potential availability improvements. - Write Concerns: MongoDB uses write concerns to ensure consistency. You can set the write concern level to ensure that writes have been committed to the journal or have been replicated to a specified number of nodes before acknowledging it to the client. This ensures data consistency even at the cost of write availability during network partitions or failures.
- Election Process: If the primary node goes down, a new primary is elected immediately, ensuring data consistency. However, during this election period, the system may not be available for writes.
Although MongoDB performs excellent consistency operations, it may sacrifice availability temporarily, especially during network partitions or primary elections. This is why it's characterized as consistent but not always available.
Cassandra: Availability over Consistency
Cassandra, in contrast, is an AP (Availability and Partition tolerance) system. Here's how Cassandra manages availability:
- Eventual Consistency: Cassandra employs an eventual consistency model that allows for high availability. In this model, all updates to a data location are eventually propagated and reconciled in all of its replicas. This provides high availability during partitions at the cost of temporary inconsistency.
- Tunable Consistency Levels: Cassandra allows the configuration of consistency levels per query, including options like ONE, QUORUM, and ALL. With tunable consistency, users can decide the level of consistency versus availability trade-offs they are willing to accept.
- Cluster Layout: Cassandra's decentralized architecture and peer-to-peer design ensure that the failure of one or more nodes does not impact the availability of the system, thereby allowing it to serve requests at all times.
During network partitions, Cassandra prioritizes serving requests over guaranteeing strict consistency immediately, which might lead to reading stale data in exchange for higher availability.
Summary Table
Below is a table summarizing the key differences between MongoDB and Cassandra based on the CAP theorem:
| Feature | MongoDB (CP) | Cassandra (AP) |
| Consistency | Strong Consistency through primary-secondary architecture and configurable write concerns | Eventual Consistency with tunable consistency levels |
| Availability | May sacrifice availability during network partitions or during primary elections | Prioritizes availability over immediate consistency |
| Partition Tolerance | Ensures data integrity across partitions but may delay availability | Continues to serve requests even during partitions |
| Architecture | Primary-Secondary model | Peer-to-Peer model |
| Use Cases | Suitable for applications requiring strict data consistency such as financial transactions | Suitable for applications that need high availability like social media feeds |
Conclusion
When choosing between MongoDB and Cassandra, it's essential to consider the specific needs of your application in terms of consistency and availability. MongoDB is an excellent option for applications requiring reliable consistency, whereas Cassandra is ideal for systems needing high availability and can tolerate eventual consistency. Understanding the underlying architecture and trade-offs of these databases will help you make an informed decision that aligns with your project's goals and requirements.

