Consistency models, also known as consistency levels, provide a way for developers to choose between high availability and better performance. Usually the choice is between two extremes but Cosmos DB provides a granular scale of consistency levels that can be configured to suit specific business requirement.
Image source: https://docs.microsoft.com/en-us/azure/cosmos-db/consistency-levels
Let’s have a look at each of the consistency models available in Cosmos DB:
- Strong: This consistency model guarantees that every time a read operation occurs, it fetches the most recent version of the data. For this to work, Cosmos DB must ensure that each write operation is propagated to all replicas before the operation is considered complete. The cost of read operation is higher compared to other consistency models
- Bounded Staleness: In this consistency model, there is an option to set a time lag between the write and the read operation. This can also be set based on item versions instead of a time interval. This consistency level is suitable for scenarios where availability and consistency have equal priority.
- Session: This is the default and the most popular consistency level in Cosmos DB. The concept of this consistency level is based on regions. A user accessing data from the same region where the write was performed, will see the latest data. Users from other regions can only read lag data. This guarantees consistency for each client session. It offers the lowest latency reads and writes among all the consistency levels.
- Consistent Prefix: This consistency level guarantees that users do not see out-of-order writes but there is no time bound replication of data across regions. E.g. if data is written is 1,2,3 users can see 1,1,2 or 1,2,3 but never out-of-order 2,1,1 or 3,2,1
- Eventual: As the name suggests, this consistency level does not guarantee any time bound or version bound replication. This means if there is no new data written, then eventually all replicas will be synced and have the latest data. This means there could be out of order reads as well. It has the lowest read latency but also the lowest level of consistency.
2 thoughts on “Azure Cosmos DB Consistency Models”