chapter sixteen

16 Cosmos DB, CQRS, and other database types

 

This chapter covers

  • Introducing NoSQL databases and how they differ from relational databases
  • Exploring the features that the NoSQL database called Cosmos DB
  • Performance tuning the Book App using EF Core Cosmos DB database provider
  • Considering the differences and limitations around using Cosmos DB with EF Core 5
  • The issues you might hit when swapping from one database type to another

The Book App has been a constant theme throughout this book, and up until now it has used a SQL Server database to store the books data in. In this chapter we are going to performance tune the Book App by combining the original SQL Server database with another database called Cosmos DB. In chapter 14 we performance tuned the Book App to handle 100,000 books and in this chapter we take the number of books to ½ million books with the same or better performance by using Cosmos DB.

Cosmos DB is relatively new (it only came out in 2017) and some readers won’t have used Cosmos DB yet. So as well as using this database to improve performance and scalability, I also point out the differences between Cosmos DB, which is a NoSQL database, and the more traditional relational databases such as SQL Server.

16.1  The differences between Relational and NoSQL databases

16.2  Introduction to Cosmos DB and its EF Core provider

16.3  Building a CQRS system using Cosmos Db

16.4  The design of a two-database CQRS architecture application

16.4.1         Create an event to trigger when a change in the SQL Book entity happens

16.4.2         Add events to the Book entity send Add, Update, or Delete integration events

16.4.3         Use the EfCore.GenericEventRunner to override your BookDbContext

16.4.5         Create the Cosmos event handlers

16.5  Understanding the structure and data in a Cosmos DB account

16.5.1         The Cosmos DB structure as seen from EF Core

16.5.2         How the CosmosClass is stored in Cosmos DB

16.6  Displaying books via Cosmos DB

16.6.1         Cosmos difference: The Cosmos DB only provides async methods

16.6.2         Cosmos difference: There are no database-created primary keys

16.6.3         Cosmos difference: Complex queries might need breaking up

16.6.4         Cosmos difference: Skip is slow and expensive

16.6.5         Cosmos difference: By default all properties are indexed

16.6.6         Cosmos/EF Core differences: Migrating a Cosmos database

16.6.7         EF Core 5 limitation: Counting the number of books in Cosmos DB is SLOW!

16.6.8         EF Core 5 limitation: Many database functions not implemented

16.6.9         EF Core 5 limitation: EF Core 5 cannot do subqueries on a Cosmos DB database