Sitemap

An Overview About Mongodb And ACID

3 min readApr 2, 2025

--

MongoDB is a NoSQL document database, representing documents in a JSON-like format called BSON. In MongoDB, we have collections similar to tables in relational databases , and documents similar to rows in a relational database

Features of mongodb

Document-Oriented:

MongoDB stores data in documents using BSON, a binary format that was built to have more features than JSON and be faster. It has the ability to add types and other different metadata. MongoDB can enforce schema validations or keep data dynamic and flexible, which is different than how relational databases operate

High availability:

MongoDB support replication that can be configured and created when needed, by having at least 3 nodes: one primary and two secondary MongoDBs ensure availability and fault tolerance , by duplicating data between the different nodes, which minimize the downtime of a system if a one node fail

Scale out:

MongoDB can be configured to use sharding and distribute documents between different nodes in a cluster based on a specific shard key. this let us add more nodes and scale horizontally and going beyond the limits of a single machine

Query Optimization:

Using indexes to reduce the number of documents scanned and improve query performance.

Use aggregation pipelines to perform different operations on the documents, including filtering , grouping, and other complex operations

JSON vs BSON

BSON is a faster and more advanced version of JSON. while json is human-readable and easy to understand, it lacks the support of typing, binary data, dates, and other metadata

BSON solves this by supporting all that was mentioned above and giving the user the ability to specify schema and validate the data.

MongoDB also uses BSON to exchange data internally.

Mongodb have different drivers with the responsibility of being a bridge between the client code and the mongodb engine

ACID Transactions:

Mongodb support ACID for multi document transactions

Example applications that may benefit from multi-document transactions include:

Systems that move funds from one account to another (e.g., banking applications, payment processing systems, trading platforms).

Supply chain and booking systems where ownership of goods and services is transferred from one party to another.

Billing systems that store information in detailed records as well as summary records.

Atomicity: In a transaction, if one step fails, all previous operations should rollback, and everything should fail.

Consistency: Mongodb use these different strategies to maintain consistent data

  • Multi document transaction: atomic operations on different documents; this may impact performance
  • Embedded data: reduce the usage of aggregation lookups, which is similar to joins in relational databases, by duplicating data across documents
  • Atlast triggers: if an update ocure in a one collection that containe duplicate data, a trigger is used to to update this data in others collections (the updates can be delayed)

Isolation: each transaction is executed separately from others. If an operation tries to modify data that is being modified, it gets suspended using locks until the first transaction is done. MongoDB use these two techniques

  • snapshot: transaction get executed on specific snapshot of the db data at the time of transaction start; if there is a conflict, the transaction is aborted
  • Read/Write concerns: customize how consistency is handled; for example, configure with majority for writes; mean that the data in read operations must be replicated across a calculated majority of nodes to be considered as successful.

Durability: which means that when the data is persisted to the database, it should not be lost even if the servers crashes, power outages or other disasters

MongoDB uses OpLog to record write operations as idempotent entries; these logs can be replayed after crashes to recover the data.

Two-Phase Locking

A mechanism used to ensure that specific data can only be manipulated by a single transaction to ensure isolation, it has two phases:

  • growing phase transaction can not release the lock
  • shrinking phase transaction can not acquire the lock

Read Contention

Long-running transactions can block access to documents. MongoDB mitigates this with:

  • Snapshot Reads: Queries see data from a consistent point in time.
  • Document-Level Locking: Concurrency without locking entire collections

References:

--

--

No responses yet