← All posts

When should I use Cosmos DB?

That is a common question and to get this question right can save you valuable time. Following I will show you four selling points for Cosmos DB and also drawbacks that will come with it

Selling Points

Scale Out Architecture

The first selling point of Cosmos DB is the scale out architecture, so no matter how much your workload increases you will not face any scaling issues concerning the database layer. If you use SQL Server or Postgres instead the scaling would be your concern, and you would need DBA’s to adjust to the increasing workload.

Two examples profited from this kind of architecture are Vercel and OpenAI. Both experienced scaling problems and solved it by switching to Cosmos DB.

Global Distribution

The second selling point is that Cosmos DB can be easily globally distributed with multi write regions. So if you know that you want to build a global app from day one, then it could be worth a consideration as well. Global distribution is solved out of the box, so you free up resources to tackle other problems instead.

Flexible Schemas

The third selling point is more about NoSQL in general. If you need flexible data schemas it is also worth a consideration to use Cosmos DB. For example if you need to store AI outputs or are part of an early startup team, where data models are frequently changing, you can profit from the flexibility.

Low Latency

The last selling point is that Cosmos DB has a low latency out of the box. This can be useful for storing AI memory or for ecommerce apps who need fast response times even in busy times of the year.

Combined with the scale out architecture you can serve as many customers as you have in a timely manner.

Drawbacks

No Joins

If you have an application with a lot of relationships and you are used to joining tables together, this does not work in Cosmos DB.

Here you have two options.

Option 1: Join manually in code.

Option 2: Denormalize your data models.

The first one costs less time for the development but adds up to the bill later. The second option costs way more in development time but keeps the bill lower.

So just be aware if you have a relationship heavy use case, to avoid Cosmos DB or if the selling points outweigh it how to deal with it.

No Transactions

Another drawback is that you lose transactions. There are transactions in Cosmos DB but just inside one partition key of one container.

Here you could try to implement transactions via code, saga pattern with compensations yourself if the selling points are too compelling.

Otherwise, you are best advised to just use a SQL database instead in Azure because it saves a lot of development time not having to implement database transactions yourself.

Summary

In my opinion Cosmos DB has a strong use case in 4 scenarios:

  • AI agents
  • Seasonal apps like ecommerce or ticketing apps
  • IoT data storage
  • Global apps

So if your app can profit from Cosmos DB’s scale out architecture combined with low latency in writes and reads, then Cosmos DB is a great choice.

If you have a regional app with plannable scale or a relationship/transaction heavy app, Cosmos DB might not be your best option.