cosmos-db
  1. cosmos-db-sla-and-consistency-models

SLA and Consistency Models - (Azure Cosmos DB)

Introduction

Azure Cosmos DB provides global distribution, low latency, and easily scalable throughput, making it a preferred choice for many applications.

This tutorial covers the Service Level Agreement (SLA) and the Consistency Models in Azure Cosmos DB.

SLA

Azure Cosmos DB provides an industry-leading SLA that guarantees less than 10ms read and write latencies at the 99th percentile, 99.999% availability, and automatic guarantees for data durability, throughput, and storage.

Consistency Models

Cosmos DB provides five well-defined consistency models that allow developers to choose the desired level of data consistency based on their application needs. These models include:

  1. Strong Consistency
  2. Bounded Staleness Consistency
  3. Session Consistency
  4. Consistent Prefix Consistency
  5. Eventual Consistency

1. Strong Consistency

With strong consistency, a read operation always returns the most recently written value. This consistency model is best suited for applications that need immediate and strong guarantees for data consistency.

Syntax:

{
   "Consistency": "Strong"
}

Example:

// Gets the most recently written value
{
   "id": "example1",
   "name": "John",
   "age": 30
}

Output:

// Returns the most recently written value
{
   "id": "example1",
   "name": "John",
   "age": 30
}

2. Bounded Staleness Consistency

With bounded staleness consistency, a read operation returns the most recently written value with a specific lag tolerance. For example, if the lag tolerance is set to 5 minutes, the read operation returns the latest value that is no older than 5 minutes.

Syntax:

{
   "Consistency": "BoundedStaleness",
   "stalenessIntervalInSeconds": 300,
   "maxStalenessPrefix": 100
}

Example:

// Gets the most recently written value with a 5-minute tolerance
{
   "id": "example1",
   "name": "John",
   "age": 30
}

Output:

// Returns the most recently written value with a 5-minute tolerance
{
   "id": "example1",
   "name": "John",
   "age": 30
}

3. Session Consistency

With session consistency, a read operation returns the most recently written value within the same session. This consistency model is suitable for scenarios where multiple requests from the same client need to be consistent.

Syntax:

{
   "Consistency": "Session"
}

Example:

// Gets the most recently written value within the same session
{
   "id": "example1",
   "name": "John",
   "age": 30
}

Output:

// Returns the most recently written value within the same session
{
   "id": "example1",
   "name": "John",
   "age": 30
}

4. Consistent Prefix Consistency

With consistent prefix consistency, a read operation returns a prefix of the most recently written values. For example, if a client has written five values, a consistent prefix with a value of three returns the most recently written three values.

Syntax:

{
   "Consistency": "ConsistentPrefix",
   "maxStalenessPrefix": 100
}

Example:

// Gets a prefix of the most recently written values
{
   "id": ["example1", "example2", "example3", "example4", "example5"],
   "name": ["John", "Sarah", "David", "Nina", "Adam"],
   "age": [30, 25, 40, 35, 45]
}

Output:

// Returns a prefix of the most recently written values
{
   "id": ["example3", "example4", "example5"],
   "name": ["David", "Nina", "Adam"],
   "age": [40, 35, 45]
}

5. Eventual Consistency

With eventual consistency, a read operation may return stale or outdated data. This consistency model is suitable for scenarios where the application can tolerate eventual consistency.

Syntax:

{
   "Consistency": "Eventual"
}

Example:

// Gets data with eventual consistency
{
   "id": "example1",
   "name": "John",
   "age": 30
}

Output:

// May return stale or outdated data
{
   "id": "example1",
   "name": "John",
   "age": 29
}

Use and Important Points

  • Choose the appropriate consistency model based on your application's requirements.
  • Consider the impact of consistency on performance and cost.
  • SLA guarantees high availability and low latency for read and write operations.

Summary

This tutorial covered the Service Level Agreement (SLA) and the Consistency Models in Azure Cosmos DB. Cosmos DB provides five well-defined consistency models that allow developers to choose the desired level of data consistency based on their application needs. Consistency needs to be balanced against performance and cost requirements, and the provided SLA builds trust and reliability in the service.

Published on: