Azure Cosmos DB Workshop
Azure Cosmos DB Workshop
Azure Cosmos DB
A globally distributed, massively scalable, multi-model database service
Column-family
Document
Key-value Graph
MongoDB
Table API
Column-family
Document
Key-value Graph
Multi-homing APIs
Black Friday
12000000
10000000
Transparent server-side partition management
8000000
6000000
4000000
Elastically scale storage (GB to PB) and throughput (100 to 100M req/sec)
across many machines and multiple regions
2000000
Physical index
Multi-model, multi-API
Database engine operates on Atom-Record-Sequence type system
Azure Cosmos DB
Azure region B (app + session state)
Azure Storage
(logs, static Azure Cosmos DB
catalog content) (session state)
Retail Order Processing Pipelines
...
Azure Functions Azure Functions Azure Functions
(Microservice 1: Tax) (Microservice 2: Payment) (Microservice N: Fufillment)
Real-time Recommendations
Online Recommendations Service
Shoppers
E-commerce Store Apache Spark on
Azure Databricks
Order Transactions
Multiplayer Gaming
Azure CDN
Azure Storage
(game files)
MLlib
Spark Spark GraphX
(machine
SQL Streaming (graph)
learning)
Scale-out Database
Azure Cosmos DB
Let’s zoom in Azure Cosmos DB
Resource Model
Account
Database
Container
Item
********.azure.com
Account
Database IGeAvVUp …
Container
Item
Account
Database
Container
Item
Account
Database
Container
Item
Account
Database
Container
Item
Account
Database
Container
Item
Account
Database
Container
Item
Account
Database
Container
Item
Account
Database
Container
Item
Account
Database
Container
Item
Account
Database
Container
Item
Account
Database
Container
Item
Account
Database
Container User
Item Permission
Account
Database
Item
Account
Database
Container
Item
Account
Database
Container
Item
Account
Database
Container
Note: Throughput can also be
shared across a set of collections
Item
Account
Database
Container
Item
Account
Database
Container
Item
Account
Database
Container
Database
Container
Tenants
Follower
K
K
V
V
Follower
Lead
K V
1 RU = 1 read of 1 KB record
POST
Query
…
Request Units
Provisioned in terms of RU/sec
Rate
limit
Rate limiting based on amount of throughput provisioned
Throughput Cost
Operation Type Number of Requests per Second Avg RU's per Request RU's Needed
Create 100 5 500
Read 400 1 400
Dharma
Andrew
Shireesh
Karthik
Rimma
….
Mike
Bob
Alice
Carol
… …
Dharma
Andrew
Shireesh
Karthik
Rimma
….
Mike
Bob
Alice
Carol
… …
Rimma
Alice
Shireesh + Karthik
Carol
… … …
Rimma
… … …
General Tips
• Don’t be afraid of having too many partition keys
• Partitions keys are logical
• More partition keys => more scalability
Object Model Design
A few notes about containers
Ability to query across multiple entity types with a single network request.
Ability to query across multiple entity types with a single network request.
{
{
"id": "Andrew",
"id": "Ralph",
"type": "Person",
"type": "Cat",
"familyId": "Liu",
"familyId": "Liu",
"worksOn": "Azure Cosmos DB"
"fur": {
}
"length": "short",
"color": "brown"
}
}
Ability to query across multiple entity types with a single network request.
{
{
"id": "Andrew",
"id": "Ralph",
"type": "Person",
"type": "Cat",
"familyId": "Liu",
"familyId": "Liu",
"worksOn": "Azure Cosmos DB"
"fur": {
}
"length": "short",
"color": "brown"
}
}
We can query both types of documents without needing a JOIN simply by running a query without a filter on type:
{
{
"id": "Andrew",
"id": "Ralph",
"type": "Person",
"type": "Cat",
"familyId": "Liu",
"familyId": "Liu",
"worksOn": "Azure Cosmos DB“
"fur": {
}
"length": "short",
"color": "brown"
}
}
If we wanted to filter on type = “Person”, we can simply add a filter on type to our query:
Ability to query across multiple entity types with a single network request.
Consistency w.r.t. Transactions is NOT the same thing as Consistency w.r.t. Replication.
this is about moving from one valid state to this about getting a consistent view across
another for a single given tx replicated copies of data
(West US)
(East US)
(North Europe)
Value = 5
Value = 5
Value = 5
Value = 5 6
Value = 5
Value = 5 6
Value = 5
Value = 5
What happens when a network partition is introduced? Reader: What is the value?
Should it see 5? (prioritize availability)
Or does the system go offline until network is restored? (prioritize consistency)
Brewer’s CAP Theorem: impossible for distributed data store to
simultaneously provide more than 2 out of the following 3 guarantees:
Consistency, Availability, Partition Tolerance
Latency: packet of information can travel as fast as speed of light.
Replication between distant geographic regions can take 100’s of milliseconds
Value = 5 6
Value = 5
Reader A: What is the value?
Value = 5 6
Value = 5
Value = 5
Choice for
most
distributed
apps
• Clear tradeoffs
• Latency
• Availability
• Throughput
Consistency Level Guarantees
Consistent Prefix Reads will never see out of order writes (no gaps).
Eventual Potential for out of order reads. Lowest cost for reads of all consistency levels.
Bounded-Staleness: Bounds are set server-side via the Azure Portal
Session Consistency: Session is controlled using a “session token”.
• Session tokens are automatically cached by the Client SDK
• Can be pulled out and used to override other requests (to preserve session between multiple clients)
string sessionToken;
client.ReadDocumentAsync(
documentLink,
new RequestOptions { ConsistencyLevel = ConsistencyLevel.Eventual }
);
Indexing
Schema-agnostic, automatic indexing
Automatically index every property of every record without having to
define schemas and indices upfront.
Physical index
SQL API
Query Demo:
https://www.documentdb.com/sql/demo
SQL API
Example: SQL Parameterization
Example: LINQ
SQL API
Query Results are paginated:
Cross-Partition Queries
Concurrency
Write Optimized Database Engine
Designed for sustained large write volume without any term locality
{
"id": "2c9cddbb-a011-4947-94c2-6f8ccf421d2e",
"_rid": "o8ExAJlS4xRIAAAAAAAAAA==",
"_self": "dbs/o8ExAA==/colls/o8ExAJlS4xQ=/docs/o8ExAJlS4xRIAAAAAAAAAA==/",
"_etag": "\"2e004542-0000-0000-0000-5af31e8c0000\"",
"_attachments": "attachments/",
"_ts": 1525882508
}
Optimistic Concurrency Control
Optimistic Concurrency Control
Transactions
JavaScript Language Integrated Transactions
Context Pool ACID transactions over multiple records scoped to a partition key
… Compiled JavaScript Rich programming model via Stored Procedures
… …
Exposed via a JavaScript as a modern day T-SQL
Store
REPLACE
REPLACE
QUERY
Persistent Microservice
Event Store #1
Microservice #2
New Event
…
Microservice
#N
Materializing Views
Application
Cosmos DB
Materialized View
Subscription User Create Date …
Replicate Records
CRUD Data
Working with Change Feed
Working with Change Feed
Source: http://lambda-architecture.net/
Lambda Architecture Simplified
1. All data pushed into both batch and speed 1
layer for processing new
data
2. The batch layer has a master dataset collections
(immutable, append-only set of raw data) CosmosDB
computed RT
and pre-compute the batch views change feed
2 4 4
master dataset
3. The serving layer has batch views so data
available for fast queries. 3 computed batch
3
4. The speed layer compensates for
processing time (to serving layer) and pre-compute
5 batch
deals with recent data only. 2
Repeated unnecessary calls to Queries? Ingestion Path? Skew in the data? Read-only Stored
“Master” resources? - Create/Replace/Upsert - Is there an uneven distribution of data Procedures?
- Read Collection and/or requests across partition keys?
- Read Offer - Is the cardinality of the partition key
- Sproc are optimized for
- Read Database too low?
atomically and
- Multiple Client instances transactionally writing
multiple records
- Not ideal for read-only
procedural logic
- Refactor Data Model Design - Re-write read-only logic
Navigate to Navigate to - Follow this guide for best practices on as query/read operations
- Re-use singleton
Troubleshooting Troubleshooting partitioning
instance of client and
refactor redundant Query Ingestion -https://docs.microsoft.com/en-
calls Performance Performance us/azure/cosmos-db/partition-data
slide slide
Troubleshooting Query Performance
High RU Charges for High Query
Query Operations? latencies?
Queries on id? Are these aggregate Sorting on a field Ask for QueryMetrics - Are there a large number of cross Mongo API?
queries? with a large number https://docs.microso partition queries? - Cross partition queries in
of distinct values? ft.com/en- - Are there a large number of physical Mongo are serial. These
e.g. timestamp? us/azure/cosmos- partitions for the collection? queries are expected to
db/sql-api-sql-query- have high latencies.
metrics#query-
execution-metrics
- Control the degree of parallelism
instead of setting - Contact support for help
- Use read document Materialized a view to offload some - Use read document MaxDegreeOfParallelism to -1 re-tuning queries
computation from read path to - This will ensure x number of
over query over query
document write path document partitions are executed against
- Run aggregations client side in parallel
- Contact support to - Contact support to
- Store results of aggregations in
preview upcoming a second collection preview upcoming
indexing - Use ChangeFeed library to indexing
improvements reduce latency for generating improvements
real-time views
https://docs.microsoft.com/en-
us/azure/cosmos-db/change-
feed
Troubleshooting Ingestion Performance
High RU Charges for Ingestion
Operations?
General/High Level
Troubleshooting
What is the
CPU
utilization
How many threads are
on the VM?
being executed in
parallel?
- Does the VM have
How many client instances sufficient cores?
are being created per
process? Ideally, the number
of instances should be
limited to 1 per process.
Thank you and Q&A
Follow @AzureCosmosDB
cosmosdb.com #azure-cosmosdb
Use #CosmosDB