Lesson 1 of 3 Semi-Structured Data Solutions

Understanding Azure Cosmos DB

This lesson explains semi-structured data, the Cosmos DB resource model and its APIs.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify when semi-structured storage is appropriate.
  • Describe the Cosmos DB resource hierarchy.
  • Choose an API.
  • Compare Azure Table Storage with the Table API.

Semi-structured data

Semi-structured data has some organization, but no fixed schema. A JSON product record might contain the attributes that a particular product needs, and two products can have different fields. This flexibility lets applications evolve without schema migrations, and it fits data that is read as a whole, such as a customer profile or a shopping cart.

The choice of store depends on access patterns. If an application reads and writes individual items by key at high scale, a NoSQL database is suitable. If it needs complex joins across many entities with strong transactional guarantees, a relational database remains the better fit. Simple key and attribute data with modest needs may fit an inexpensive table store.

Sort into groups

Relational, NoSQL or table store?

Exam skills 2.2.1

Choose the kind of store each access pattern suits.

Log in to try this activity Interactive activities are free with an account.

What Azure Cosmos DB provides

Azure Cosmos DB is a fully managed NoSQL database with low latency, automatic indexing and turnkey global distribution. Data can be replicated to any number of Azure regions by selecting them, and multi-region writes can be enabled. Microsoft offers an SLA of up to 99.999% availability for multi-region write accounts. Indexes are created automatically for all properties by default, and vector search features support AI applications.

The resource hierarchy is straightforward. An account is the top-level resource and defines the regions, the default consistency and the API. A database is a namespace for containers. A container holds items, and it is the unit of partitioning and throughput. An item is a document or row. Most design decisions are made at container level.

Choosing an API

Cosmos DB exposes several APIs, chosen when the account is created. An API determines the data model and wire protocol that applications use.

APIData modelChoose when
NoSQLJSON documents with SQL-like queriesBuilding a new application; receives new features first
MongoDBBSON documentsMigrating a MongoDB application with existing drivers
Apache CassandraWide columnMigrating a Cassandra workload using CQL
Apache GremlinGraphTraversing relationships such as social networks
TableKey and attributeUpgrading an Azure Table Storage application

The selection rule is simple: new applications use the NoSQL API, and existing applications use the API that matches their current database, so that drivers and skills are preserved.

The following diagram compares two NoSQL stores for two different needs.

Two NoSQL needs, two stores. Player profiles on three continents need flexible documents and responses under 20 ms, so Azure Cosmos DB for NoSQL with a region on each continent, and multi-region writes if players write locally. Five million simple device records looked up by ID at the lowest cost suit Azure Table storage.
The scenario is illustrative.
Match the pairs

Which Cosmos DB API?

Exam skills 2.2.1

Match each workload with the Cosmos DB API it should use.

Log in to try this activity Interactive activities are free with an account.

Related services

Three related services are easily confused with Cosmos DB accounts. Azure Table Storage is part of Azure Storage, offering an inexpensive key and attribute store with a single region of writes. The Cosmos DB Table API is wire-compatible, and offers global distribution and low latency at a higher cost. Azure DocumentDB is a separate service based on the vCore model, compatible with the MongoDB wire protocol and built on the PostgreSQL engine. It suits MongoDB workloads that prefer vCore-based pricing and has a 99.995% SLA.

The following comparison sets out the choice between Table Storage and the Cosmos DB Table API, a frequent exam distinction.

Azure Table Storage
CostVery low
Global distributionGeo-redundant read only
Secondary indexesPartition and row key only
Throughput guaranteeNo
Choose when Simple data at the lowest cost
Cosmos DB Table API
CostHigher, based on throughput
Global distributionAny number of regions
Secondary indexesAll properties, automatically
Throughput guaranteeYes, with an SLA
Choose when Global reach and guaranteed performance
Scenario questions

Table Storage or Cosmos DB?

Exam skills 2.2.1

Choose between related key and attribute stores.

Log in to try this activity Interactive activities are free with an account.
Worked example

Choosing a store

A gaming company is building a player profile service for players on three continents, each of whom expects response times under 20 milliseconds. Profiles vary by game and change often. Separately, an internal application stores 5 million simple device records, looked up by a device ID, and must be as cheap as possible. What should you recommend?

The profile service needs global, low-latency access to flexible documents, so Azure Cosmos DB for NoSQL suits it, with regions on each continent and multi-region writes if players should write to their nearest region.

The device records are simple, keyed lookups with no global requirement, and cost is the deciding qualifier, so Azure Table Storage is the recommendation. Using Cosmos DB for the device records would work but costs more than necessary, and Azure SQL Database would add a fixed schema and relational features that neither workload needs.

Check yourself

Which API should a new application use, and why?

Show answer

The NoSQL API. It is Cosmos DB's native API, and other APIs exist mainly to support migrations from their source databases.

Exam Tips

Remember for the exam

  • New applications use the NoSQL API; migrations use the API matching the existing database.
  • The container is the unit of partitioning and throughput.
  • Table Storage is cheapest; the Cosmos DB Table API adds global reach and guaranteed throughput.
  • Azure DocumentDB is a separate vCore service with MongoDB compatibility.