DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Java and MongoDB Integration: A CRUD Tutorial [Video Tutorial]
  • MongoDB to Couchbase for Developers, Part 1: Architecture
  • MongoDB to Couchbase: An Introduction to Developers and Experts
  • Manage Hierarchical Data in MongoDB With Spring

Trending

  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  • Hybrid Cloud vs Multi-Cloud: Choosing the Right Strategy for AI Scalability and Security
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  • A Modern Stack for Building Scalable Systems
  1. DZone
  2. Data Engineering
  3. Databases
  4. How ACID is MongoDB?

How ACID is MongoDB?

Find out how MongoDB ranks with atomicity, consistency, isolation and durability (ACID).

By 
Giorgio Sironi user avatar
Giorgio Sironi
·
Jan. 01, 13 · Analysis
Likes (5)
Comment
Save
Tweet
Share
81.4K Views

Join the DZone community and get the full member experience.

Join For Free

Relational databases usually guarantee ACID properties related to how reliably transactions (both reads and writes) are processed. MySQL and PostgreSQL are examples of database that provide these properties as a selling point.

The NoSQL movement trades off ACID compliance for other properties, such as 100% availability, and MongoDB is the leader in the field. I'm not saying that is bad for Mongo not to provide these guarantees, since they are not the most important constraint in its use cases; just that when transitioning from MySQL and similar products in a production system, you must be aware of what you are sacrificing.

Actually some variations of these properties map to object-oriented programming better than their classic counterpart; for example, document-based transactions are more in line with the Domain-Driven Design Aggregate pattern than the arbitrary wide transaction of MySQL.

The Original

Here is a summary fo how the ACID properties are interpreted by a relational DBMS:

  • Atomicity requires that each transaction is executed in its entirety, or fail without any change being applied.
  • Consistency requires that the database only passes from a valid state to the next one, without intermediate points.
  • Isolation requires that if transactions are executed concurrently, the result is equivalent to their serial execution. A transaction cannot see the partial result of the application of another one.
  • Durability means that the the result of a committed transaction is permanent, even if the database crashes immediately or in the event of a power loss.

Atomicity

MongoDB provides only a document-wide transaction: writes are never partially applied to an inserted or updated document. The operation is atomic in the sense that it either fails or succeeds, for the document in its entirety.

Thus at least Mongo it's not as low-level as using a bunch of files, since the equivalent would be a set of files each with its own lock.

There is no possibility of atomic changes that span multiple documents or collections: either you model the state changes of your application as additional documents, or you can't use Mongo where these database transactions are required. A classic example is to model the operations of a bank account with movement documents, instead of with a single account one: the insertion of a movement either succeed or fails.

If you would have to implement 2 phase commit by yourself, just stick to a relational database for that persistence component of your application (I'm not saying anything on the rest of the data.)

Consistency

Even in replica set configurations, the primary Mongo server is targeted with all the writes; single server consistency is easy to guarantee.

The secondary nodes may be out of date with respect to the primary, as eventual consistency only guarantees that if after a long enough period with no writes, they will get up to date with respect to the primary. However by default the secondary servers cannot answer reads, so you are able to distribute your traffic with the penalty of inconsistency only if you want to and configure them to do so. Consistency and availability are incompatible due to the CAP theorem - you have to choose.

Isolation

Up to a few months ago, MongoDB had a server-wide write lock! I guess you can say it's a perfect isolation mechanism. Read locks instead can be taken by multiple connections at the same time, as long as no one is writing.

From the 2.2 version, Mongo started to use database-specific write locks, and many operation started to yield locks upon encountering slow events such as page faults. Mongo will move in the future at least to collection-specific locks.

However, keep in mind that the Mongo model is similar to transaction auto commits for relational databases: you can't really talk about isolation since every operation is immediately visibile to any other connection.

Durability

Durability of writes is the biggest issue with Mongo. After the 2.0 version, the situation of a single server is:

  • database files are committed every 60 seconds.
  • The journal of operations (a write-ahead log) is committed every 100 milliseconds.

These parameters are configurable with the syncdelay and journalCommitInterval configuration options. Committing a file means issuing the OS sync command over it; the journal is, as for all databases, synced very frequently so that in the event of a crash or a forced shutdown the database can be rebuilt from it.

What MySQL do is committing the journal after every write operation (actually every committed transaction). The Mongo developers say they don't do this because in many scenarios the OS doesn't write the file on disk even after syncing (hardware buffering), and because time spent waiting for recovering would impact availability. Only a battery-backed disk controller could guarantee these writes aren't lost upon a failure, but it isn't a common configuration. Turning off hardware buffering would be "very slow".

So if the server crashes, writes accepted after the last commit of the journal will be lost. Rare, but possible case.

Across multiple servers, it is possible for the server to die before transmitting updates to any secondary, which are synced asynchronously by default. These updates can be merged back if the failed primary is recoverable.

However, you can specify to replicate a write to at least N secondaries before considering a write finished, with the write concern options (clustered durability). Write concerns can even be customized for single insertions. It's a bit strange to skip writing on disks only to wait for network calls to be finished, anyway.

Thus once upon a time, Mongo only supported clustered durability, by replicating everything to secondary servers. Under the assumption of unrelated failures, this protects you because two servers will never fail in the same time span (meaning one fails and the other fails before the first is repaired.) But if your data center loses power, only journaling, introduced in Mongo 1.8, will be able to save you.

The take-away from this discussion is that Mongo does not provide durability by default (outdated post), but lets you tune the configuration of a replica set  in order to achieve it if you want to sacrifice enough performance.

Database MongoDB Relational database

Opinions expressed by DZone contributors are their own.

Related

  • Java and MongoDB Integration: A CRUD Tutorial [Video Tutorial]
  • MongoDB to Couchbase for Developers, Part 1: Architecture
  • MongoDB to Couchbase: An Introduction to Developers and Experts
  • Manage Hierarchical Data in MongoDB With Spring

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!