Visit complete MongoDB roadmap

← Back to Topics List

Read / Write Concerns

Read and write concerns are crucial aspects of data consistency and reliability in MongoDB. They determine the level of acknowledgement required by the database for read and write operations. Understanding these concerns can help you balance performance and data durability based on your application needs.

Read Concern

A read concern determines the consistency level of the data returned by a query. It specifies the version of data that a query should return. MongoDB supports different read concern levels:

  • local (default): Returns the most recent data available on the primary node at the time of query execution. It does not guarantee consistency across replica sets.
  • available: The query returns the most recent data available on the queried node. This level is only applicable to sharded clusters.
  • majority: The query returns data that has been acknowledged by a majority of replica set members. It provides a higher level of consistency but may have higher latency.
  • linearizable: Ensures reading the most recent data that has been acknowledged by a majority of replica sets. This level guarantees the highest consistency but can be the slowest among all levels.
  • snapshot: Returns the data from a specific snapshot timestamp. This level is useful for read transactions with snapshot isolation.

Write Concern

A write concern indicates the level of acknowledgment MongoDB should provide when writing data to the database. It ensures that the data has been successfully written and replicated before acknowledging the write operation. The different write concern levels are:

  • w: 0: The write operation is unacknowledged, which means MongoDB does not send any acknowledgment. This level provides the lowest latency but carries the risk of losing data.
  • w: 1 (default): The write operation is acknowledged after being successfully written to the primary node. It does not guarantee replication to other replica set members.
  • w: majority: The write operation is acknowledged after being written and replicated to a majority of replica set members. This level provides better data durability but may have increased latency.
  • w: <number>: The write operation is acknowledged after being replicated to the specified number of replica set members. This level provides a custom level of data durability.

Additionally, the j and wtimeout options can be used to fine-tune the write concern:

  • j: true/false: Specifies whether the write operation must be written to the journal before acknowledgment. Setting j: true ensures the data is committed to the journal and provides increased durability.
  • wtimeout: <ms>: Specifies a time limit in milliseconds for write operations to be acknowledged. If the acknowledgment is not received within the specified time, the operation returns a timeout error. However, this does not mean the write operation failed; it may still be successful at a later point in time.

By configuring read and write concerns appropriately, you can manage the consistency and durability of your MongoDB database according to your application requirements.

Community

roadmap.sh is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Roadmaps Best Practices Guides Videos Store YouTube

roadmap.sh by Kamran Ahmed

Community created roadmaps, articles, resources and journeys to help you choose your path and grow in your career.

© roadmap.sh · FAQs · Terms · Privacy

ThewNewStack

The leading DevOps resource for Kubernetes, cloud-native computing, and the latest in at-scale development, deployment, and management.