Cosmos DB

Overview

This example defines an Cosmos DB implementation for DocumentDB

{
	"cosmos_db": {
		"name": "cosmos-pepega-{env}",
		"default_consistency_level": "Session",
		"geo_redundant_locations": [
			{ "name": "northeurope", "priority": 0, "redundant": false }
		],
		"mode": "Provisioned",
		"databases": [
			{
				"name": "workge",
				"containers": [
					{
						"name": "ci-bzgdesign",
						"partition_key": "/partner_Id",
						"unique_keys": ["/skills", "/account"],
						"autoscale_throughput": { "max": 1000 },
						"indexing_policy": {
							"automatic": true,
							"included_paths": ["/*"],
							"excluded_paths": [
								"/partner_Id",
								"/skills",
								"/account"
							],
							"mode": "Consistent"
						}
					}
				]
			}
		],

		"system_managed_failover": true,
		"location": "northeurope",
		"external": false,
		"resource_group_name": "my-rg-name",
		"tags": { "example": "S0" }
	}
}

Cosmos DB Reference

KeyValueDescription
name (required)stringThe name of the Cosmos DB Account(see how to name)
default_consistency_level (required)Consistency LevelThe consistency level and configuration settings of the Cosmos DB Account
geo_redudant_locations (required)Geo Redundant LocationsLocations for geo redundancy
mode (required)ModePerformance tier of the Cosmos DB Account
databases (required)Databases[]The databases definition for the Cosmos DB Account
system_managed_failoverboolEnable system managed failover for regions, (defaults to true)
locationstringResource Location (defaults to the resource group location)
externalboolA reference to an existing Cosmos DB Account (defaults to false)
resource_group_namestringThe name of the resource group where the resource is located, only with external resources (defaults to the resource group of the deployment)
tagsobjectAdditional tags for the resource (defaults to no additional tags)

DefaultConsistencyLevel

ValueDescription
BoundedStalenessBounded Staleness{}
ConsistentPrefixSimilar to bounded staleness except, the operational or time lag guarantee
EventualThe first thing to consider in this model is that there is no guarantee on the order of the data and also no guarantee of how long the data can take to replicate
SessionThis level of consistency honors the client session. It ensures a strong consistency for an application session with the same session token
StrongThe data is synchronously replicated to all the replicas in real-time

For a better understanding about the consistency levels, see this link


GeoRedundantLocations

This example defines only one location for redundancy

{
	"cosmos_db": {
		"geo_redundant_locations": [
			{ "name": "northeurope", "priority": 0, "redundant": true }
		]
	}
}

This example defines two locations for redundancy

{
	"cosmos_db": {
		"geo_redundant_locations": [
			{ "name": "northeurope", "priority": 0, "redundant": true },
			{ "name": "westeurope", "priority": 1, "redundant": false }
		]
	}
}
ValueValueDescription
namestringThe location of the region that you want to use
priorityintThe failover priority for the region. A failover priority of 0 indicates a write region (See detailed information about the priority here)
redundantboolIndicate whether or not this region is an Availability Zone region

Mode

ValueDescription
ProvisionedIt defines request unit per second (RUs) throughput. Every second, the configured RUs are available for database activity
ServerlessIt does not use any provisioned capacity. It charges based on RUs consumed

Databases

ValueValueDescription
namestringThe name of the database
containersArray of Containers[]Where the data is stored

Containers

ValueValueDescription
namestringThe name of the container
partition_keystringSelecting your partition key is a simple but important design choice in Azure Cosmos DB Account. Once you select your partition key, it isn’t possible to change it in-place
unique_keysarrayWith unique keys, you make sure that one or more values within a logical partition is unique. You also can guarantee uniqueness per partition key
autoscale_throughputobjectMaximum autoscale throughput for the container (only for Provisioned accounts)
indexing_policyObject of Indexing PolicyEvery container has an indexing policy that dictates how the container’s items should be indexed
modeContainer ModeDefaults to Consistent

IndexingPolicy

Every container has an indexing policy that dictates how the container’s items should be indexed. The default indexing policy for newly created containers indexes every property of every item and enforces range indexes for any string or number. (See detailed information about indexing policies here)

ValueValueDescription
automaticboolWhether the indexing policy is automatic or not
included_pathsobjectThe paths included in the indexing process
excluded_pathsobjectThe paths excluded in the indexing process

ContainerMode

ValueDescription
ConsistentThe index is updated synchronously as you create, update or delete items
NoneThe index is updated synchronously as you create, update or delete items

BoundedStaleness

ValueDescription
SingleRegionBoundedStalenessFor a single-region account, Bounded Staleness provides the same write consistency guarantees as Session and Eventual Consistency. With Bounded Staleness, data is replicated to a local majority (three replicas in a four replica set) in the single region
MultiRegionBoundedStalenessFor multi-region write accounts with two or more regions, application servers should direct reads and writes to the same region in which the application servers are hosted

SingleRegionBoundedStaleness

For a single-region account, Bounded Staleness provides the same write consistency guarantees as Session and Eventual Consistency. With Bounded Staleness, data is replicated to a local majority (three replicas in a four replica set) in the single region

ValueValueDescription
max_intervalstringMax lag time (minutes). Valid ranges, Single Region: 5 to 84600. Multi Region: 300 to 86400
max_prefixstringMax stale requests. Valid ranges, Single Region: 10 to 2147483647. Multi Region: 100000 to 2147483647

This example defines only one region for the Bounded Staleness

{
	"cosmos_db": {
		"default_consistency_level": {
			"SingleRegionBoundedStaleness": {
				"max_interval": 5,
				"max_prefix": 10
			}
		},
		"geo_redundant_locations": [
			{ "name": "northeurope", "priority": 0, "redundant": true }
		]
	}
}

MultiRegionBoundedStaleness

For multi-region write accounts with two or more regions, application servers should direct reads and writes to the same region in which the application servers are hosted

ValueValueDescription
max_intervalstringMax lag time (minutes). Valid ranges, Single Region: 5 to 84600. Multi Region: 300 to 86400
max_prefixstringMax stale requests. Valid ranges, Single Region: 10 to 2147483647. Multi Region: 100000 to 2147483647

This example defines two regions for the Bounded Staleness

{
	"cosmos_db": {
		"default_consistency_level": {
			"MultiRegionBoundedStaleness": {
				"max_interval": 300,
				"max_prefix": 100000
			}
		},
		"geo_redundant_locations": [
			{ "name": "northeurope", "priority": 0, "redundant": true },
			{ "name": "westeurope", "priority": 1, "redundant": true }
		]
	}
}