DP-200 & DP-201 Azure Data Engineer Associate Certification: Part 2 Cosmos DB




Hello Reader,

This is the second post in the series on Azure Data Engineer Certification. In this post we are going to deep dive into the most advanced data base services offered by Azure Cosmos DB, so let's get started.

Cosmos DB is a high-available, reliable, multi-model and

globally distributed NoSQL database. Cosmos DB is indicated for IoT, Web, and large-scale mobile applications use cases. below the cosmos DB key features:

  • Global Distribution: it is possible to distribute the database between different regions with just one click. It offers five consistency levels (Strong, Bounded Stateless, Session, Consistent Prefix, Eventual)
  • Reliability: the offered SLA is 99.999% availability, <10ms latency
  • Elasticity: horizontal partitioning, transparent replication
  • Multi-Model: it supports SQL API, Cassandra API, MongoDB API, Gremlin API (Graph), Table API
  • Automatic Indexing: automatically indexes every property for all items in your container 

Global distribution

 

Azure Cosmos DB is a globally distributed database system, it allows reads and writes from the local replicas. Azure Cosmos DB is a globally distributed database service designed to provide low latency, elastic scalability of throughput, well-defined semantics for data consistency, and high availability.

 

The collections, tables, and graphs created are stored as Cosmos containers. Containers are completely schema-agnostic providing scope for queries. Data in a Cosmos container is indexed automatically upon ingestion, Automatic indexing enables users to query the data without the worry about schema or index management. Below a couple of key points about containers:

  • Local distribution: In a given region, data within a container is distributed by using a partition-key, defined by the user. The row distribution is transparently managed by the underlying physical partitions.
  • Global distribution: Each physical partition is also replicated across geographical regions.

 Cosmos DB transparently handles partition management operations (split, clone, delete) across all the regions. A physical partition is implemented by a group of replicas, called a replica-set. Each machine hosts hundreds of replicas that correspond to various physical partitions within a fixed set.


Partitioning

 

Selecting your partition key is a simple but important design choice in Azure Cosmos DB. Once you have selected your partition key, it is not possible to change it. If you need to change your partition key, you should move your data to a new container with your new desired partition key.

 

Every object in Cosmos DB must have a partition key, the key is used to distribute data across the logical partitions. Important considerations when choosing partition keys are:

  • Consider how the data will be queried (most frequent queries)
  • Try to achieve the maximum distribution of the data among the logical partitions
  • Partition keys are immutable once the table is created
  • A partition can contain a maximum of 10GB of data

 

Azure Cosmos DB uses hash-based partitioning to spread logical partitions across physical partitions. The hashed partition key value of an item determines the physical partition.  

 

In partitioning, the items in a container are divided into distinct subsets called logical partitions. Logical partitions are formed based on the value of a partition key that is associated with each item in a container. All the items in a logical partition have the same partition key value.

 

A container is scaled by distributing data and throughput across physical partitions. Internally, one or more logical partitions are mapped to a single physical partition. Physical partitions are an internal implementation entirely managed by Azure Cosmos DB.

 

Each physical partition can provide a throughput of up to 10,000 request units per second and can store up to 50GB of data.

 

Partition-set Replica Set

 

Cosmos DB’s global distribution relies on two key abstractions – replica-sets and partition-sets. A replica-set is a modular Lego block for coordination, and a partition-set is a dynamic overlay of one or more geographically distributed physical partitions

 

A physical partition is materialized as a self-managed and dynamically load-balanced group of replicas spread across multiple fault domains, called a replica-set. 




Consistency Model

 

Distributed databases must make a fundamental tradeoff between the read consistency, availability, latency, and throughput as defined by the CAP Theorem


Azure Cosmos DB offers five well-defined levels. From strongest to weakest:

  • Strong
  • Bounded staleness
  • Session
  • Consistent prefix
  • Eventual
 

Strong: read and writes are fully aligned, data can be read-only when is fully replicated in all regions/partitions. Most expensive less performant.

Bounded staleness: Guarantee the order of the records. The reads might lag writes by at most "K" versions (that is, "updates") of an item or by "T" time interval, whichever is reached first. Bounded staleness is frequently chosen by globally distributed applications that expect low write latencies but require a total global order guarantee. Bounded staleness is great for applications featuring group collaboration and sharing, stock ticker etc.



Session: order guaranteed within a session, read-your-writes, and write-follows-reads guarantees. This assumes a single "writer" session or sharing the session token for multiple writers. Session consistency is the most widely used consistency level for both single regions as well as globally distributed applications. It provides write latencies, availability, and read throughput comparable to that of eventual consistency but also provides the consistency guarantees that suit the needs of applications written to operate in the context of a user




Consistent prefix: Consistent prefix consistency level guarantees that read never see out-of-order writes.



Eventual: There's no ordering guarantee for reads. In the absence of any further writes, the replicas eventually converge. Eventual consistency is the weakest form of consistency because a client may read the values that are older than the ones it had read before. Eventual consistency is ideal where the application does not require any ordering guarantees. Examples include a count of Retweets, Likes, or non-threaded comments



Throughput

 

The cost of all database operations is normalized by Azure Cosmos DB and is expressed by Request Units (or RUs, for short). Request unit is a performance currency abstracting the system resources such as CPU, IOPS, and memory that are required to perform the database operations supported by Azure Cosmos DB. There are three main throughput modes provided by Cosmos:

  

Provisioned throughput Mode: number of RUs billed on a per-second basis in increments of 100 RUs per second


Serverless Mode: billed at the end of the period for the number of Request Units consumed.

 

Autoscale Mode:  automatically and instantly scale the throughput (RU/s) of your database or container based on its usage, without impacting the availability, latency, throughput, or performance of the workload. This model is well suited for mission-critical workloads that have variable or unpredictable traffic patterns.


Azure Table Storage

 

Azure Table storage is a particular service part of the Azure Storage account, is a scalable NoSQL key-value store, implements a strong consistency model, and can be used in Cosmos DB via Table API. Used when you need to store a large amount of structured, semi structured data to be accessed through simple queries. The difference between Table storage and Cosmos Table AI is that table API does not provide cross-region replication and the service quotas are different (e.g. 1 Mb size per item in Table Storage vs 2 Mb in Cosmos Table API)


Multimode

 

Cosmos DB offers several APIs to access and access data

  • Core (SQL): default API that offers a SQL-like query language. Provides a Document data model where the data is stored in JSON format. Used JavaScript type, evaluation, and functions
  • MongoDB: Provides a document data model compatible with MongoDB, includes a built-in find method.
  • Cassandra: Provides a columnar model, compatible with Cassandra Query Language (CQL) 
  • Azure Table: Provides a key-value data model based on Azure Table store, no index management required
  • Gremlin (Graph): provides a graph data model

Disaster Recovery & High Availability

 

High Availability is the ability of a system to continuously provide a service without interruptions. Cosmos DB provides high availability once configured for multi-region replication and locally creating four replicas of the data allowing: 

  • 99.99% uptime SLA on a single region
  • 99.99% write and 99.999% read SLA on a multi-region single write
  • 99.999% uptime SLA on multi-region writes

Disaster Recovery: backups are taken every 4 hours (the frequency can be increased) and stored in Azure blob storage (to increase the resilience the storage can be replicated in multiple regions).



Recommended Resources


Courses


ACloudGuru


Comments