Azure Cosmos DB is a globally distributed, multi-model database service. One of the unique features of Azure Cosmos DB is its comprehensive SLAs, which include consistency, tunable to five well-defined consistency levels namely: Eventual, Consistent Prefix, Session, Bounded Staleness, and Strong.
Understanding Consistency Levels:
- Eventual Consistency: With this level, there is a possibility of getting out-of-order or stale reads. But this level offers the lowest latency and the highest availability.
- Consistent Prefix: Guarantees that reads never see out-of-order writes. It means if writes were performed in the order A, B, C, then a read will return A, AB, or ABC but never A, C, or B, A, C.
- Session Consistency: This is a user-focused configuration. With session-level consistency, a single user session always perceives strong consistency.
- Bounded Staleness: This level guarantees a specific lag between reads and writes, allowing the system to be eventually consistent.
- Strong Consistency: This level guarantees linearizability, meaning once a write is performed, the read will always see the latest write.
Choosing the appropriate consistency level is a decision that balances latency, availability, and consistency based on your application needs.
Setting Consistency Levels:
In Azure Cosmos DB, setting the consistency level is a flexible process. You can choose a default consistency level for your Cosmos DB account, and also override this at a per-request level.
Here is how you can set the consistency level at the Cosmos DB account level using Azure portal:
- Navigate to Azure portal > Azure Cosmos DB account.
- In the left menu, under ‘Settings’, select ‘Default Consistency’.
- Choose your needed consistency level and Save the changes.
And this is how you can override consistency level at request level using .NET SDK:
using Microsoft.Azure.Cosmos;
var cosmosClientOptions = new CosmosClientOptions()
{
ConsistencyLevel = ConsistencyLevel.Session // setting Session consistency level
};
CosmosClient cosmosClient = new CosmosClient("connection-string", cosmosClientOptions);
It’s worth mentioning that the trade-off decision depends on the nature of your application. For example, if your application needs globally distributed high availability and can tolerate eventual consistency, then Eventual or Consistent Prefix would be suitable. On the other hand, Strong or Bounded staleness would be a good choice if your application needs a high consistency level.
To prepare for AZ-204 Developing Solutions for Microsoft Azure, understanding these consistency levels, knowing when to use each one, and how to configure them is crucial. Reviewing the official Microsoft documentation and practicing with real-world scenarios strengthen your chances of passing the exam.
Practice Test
True or False: The consistency level in Azure Cosmos DB determines how up-to-date and synchronized a document’s data is across the globe.
- Answer: True.
Explanation: In Azure Cosmos DB, the consistency level plays a key role in managing and ensuring that the data around the globe stays synchronous and updated.
Which of the following are valid consistency levels in Azure Cosmos DB?
- a) Eventual
- b) Session
- c) Sequential
- d) Strong
Answer: a), b), d)
Explanation: Azure Cosmos DB offers five consistency levels: Eventual, Session, Bounded staleness, Consistent prefix, and Strong. Sequential is not a valid consistency level.
True or False: Strong consistency level in Azure Cosmos DB provides the most substantial guarantee but can impact the performance and availability.
- Answer: True.
Explanation: Strong consistency offers a linearizability guarantee, which is the highest possible level of consistency. However, it can affect the performance and availability.
What is the default consistency level for Cosmos DB?
- a) Strong
- b) Eventual
- c) Session
- d) Bounded staleness
Answer: c) Session
Explanation: Session is the default consistency level for Cosmos DB. It ensures linearizability and read your own writes consistency guarantees within a single session.
True or False: The chosen consistency level has no impact on cost of operations in Azure Cosmos DB.
- Answer: False
Explanation: The consistency level directly impacts the cost of operations as it affects the amount of resources consumed for read and write operations.
Picking the correct consistency level is a trade-off between what factors?
- a) Performance and Availability
- b) Latency and Throughput
- c) Location and Cost
Answer: a) Performance and Availability
Explanation: The consistency level is a trade-off between the performance and availability. Strong consistency provides better performance but can affect availability.
True or False: Consistency level in Azure Cosmos DB can be configured at the account level and at individual request level.
- Answer: True
Explanation: The consistency level can be globally set at the account level and can be overwritten for an individual request, providing flexibility in managing consistency, performance, and availability.
The lowest level of consistency in Azure Cosmos DB, Eventual consistency is ideal for which scenarios?
- a) Bank transactions
- b) Social media feeds
- c) Online ticket reservations
Answer: b) Social media feeds
Explanation: Eventual consistency is ideal for scenarios where absolute order of operations is not critical, such as social media feeds where the order of posts being displayed is not of the utmost importance.
Which Azure Cosmos DB API does not support all five consistency levels?
- a) SQL API
- b) Cassandra API
- c) Mongo DB API
- d) Gremlin API
Answer: b) Cassandra API
Explanation: The Cassandra API for Azure Cosmos DB currently only supports Eventual and Session consistency levels.
In Azure Cosmos DB, what consistency level provides a balance between consistency, performance, and availability?
- a) Eventual
- b) Strong
- c) Session
- d) Bounded staleness
Answer: d) Bounded staleness
Explanation: Bounded staleness provides a middle point between strong and eventual consistency, providing a balance between consistency, performance, and availability.
Interview Questions
What is the default consistency level in Azure Cosmos DB?
The default consistency level in Azure Cosmos DB is Session.
How many consistency levels are supported by Azure Cosmos DB?
Azure Cosmos DB supports five consistency levels: Strong, Bounded staleness, Session, Consistent prefix, and Eventual.
What is the strongest consistency level in Azure Cosmos DB?
The strongest consistency level in Azure Cosmos DB is Strong consistency.
What is the eventual consistency level in Azure Cosmos DB?
The eventual consistency level in Azure Cosmos DB provides the lowest latency and highest availability. It guarantees that replicas eventually converge.
How can you set the consistency level in Azure Cosmos DB?
The consistency level in Azure Cosmos DB can be set on an account level at any time after its creation. It can also be overwritten on a per request basis.
What consistency level would you choose to maintain a balance between consistency and performance in Azure Cosmos DB?
Session consistency level is a commonly preferred choice as it ensures a balance between consistency and performance.
Does changing the default consistency level affect the existing data in Azure Cosmos DB?
No. Changing the default consistency level does not affect the existing data. It only influences how the new reads and writes are handled.
What does the ‘Bounded staleness’ consistency level guarantee in Azure Cosmos DB?
The ‘Bounded staleness’ consistency level guarantees a lag between the reads and writes, ensuring that the specified lag does not exceed certain time or update versions.
Which consistency level ensures the best availability and latency in Azure Cosmos DB?
The eventual consistency level ensures the best availability and latency in Azure Cosmos DB.
What is Consistent Prefix consistency level in Azure Cosmos DB?
Consistent Prefix consistency level maintains that reads never see out-of-order writes. That is, if writes were performed in the order A, B, C, then a client sees either A, AB, or ABC, but never A, C or B, AC.
Can a client override the default consistency level on a per request basis in Azure Cosmos DB?
Yes, a client can override the default consistency level on a per request basis for read operations only.
How is the consistency level ‘Session’ in Azure Cosmos DB different from ‘Strong’?
In ‘Session’ consistency level, the write latencies are lower as compared to ‘Strong’. Also, ‘Session’ guarantees linearizability only within a single session.
In which scenarios would the Strong consistency level be suitable?
The Strong consistency level is suitable in scenarios where strict consistency is required and an application cannot proceed without the latest data.
What are the trade-offs of using the Eventual consistency level in Azure Cosmos DB?
The trade-off for using the Eventual consistency level is that it provides higher availability and lower latency but does not guarantee immediate consistency of replicas with the primary.
How does the Bounded Staleness consistency level work in Azure Cosmos DB?
In the Bounded Staleness consistency level, Azure Cosmos DB guarantees a specific lag between reads and writes, ensuring that the specified lag does not exceed the maximum limit of either time (in seconds) or updates (version count).