Introduction to Cosmos DB
Introduction to Using Cosmos DB
Introduction to Creating an App with Cosmos DB
The course is part of these learning pathsSee 2 more
Cosmos DB is one of many database solutions in a crowded market. From DynamoDB to Cassandra to CockroachDB, the questions one would naturally ask when examining Cosmos DB are, “what makes it special, and how can I get started with it?”
This course answers both of those questions as thoroughly and concisely as possible. This course is for anyone with an interest in database technologies or creating software using Microsoft Azure. Whether you are a DevOps engineer, a database admin, a product manager, or a sales expert, this course will help you learn how to power your technology with one of Azure's most innovative data solutions.
From this course, you will learn how to make use of Cosmos DB's incredible flexibility and performance. This course is made up of nine comprehensive lectures to guide you from the basics to how to create your own app using Cosmos DB.
- Learn the basic components of Cosmos DB
- Learn how to use Cosmos DB via the web portal, libraries, and CLI tools
- Learn how to create an application with Cosmos DB as the data backend
- People looking to build applications using Microsoft Azure
- People interested in database technologies
- General knowledge of IT architecture
- General knowledge of databases
Section two, part two, Getting Started with Cosmos DB. The first and most obvious prerequisite for using Cosmos DB is having a Microsoft Azure account. Once that's ready, you can go ahead and create a Cosmos DB account. This is as simple as clicking Create a Resource in the top left of the web UI and then clicking on Databases or just starting a search for Cosmos DB by typing that in.
You will then be prompted to input some basic information about your new cosmos DB resource, a name to uniquely identify it with your account, as well as a resource group name, a region, and potentially an option for replica failover region. Also, most important, you'll have to select your API type. This is what's gonna define the data model as we said and recall that you can select from MongoDB, Cassandra, SQL, and Gremlin and Azure table.
Now, once everything is filled in click create and wait for a few minutes. It'll validate and your account will be generated. Now, remember at this point you have a Cosmos DB account resource that's ready to use but there's no actual database at this point. What we wanna do next is to use the web console to define a database and then a collection or a table to which we could write some data.
Now before we walk through the steps to do that it's important to clarify some of the nomenclature here. Different database engines use different terms to describe database schema. So for example, Cassandra uses terms like keyspaces and column families while Mongo uses terms like collections and documents, and in SQL generally, you have a database that has tables and then a row within a table to describe an individual item and that's kind of your hierarchy.
So depending on the API we pick, we'll actually see different terms used in the web UI. Now, using the core SQL API we're gonna see terms like database and container as our general terms. The latter container, this refers to the units of scalability and are analogous to MongoDB collections. So, the process for actually setting up our database in the web console goes something like this.
In the Cosmos DB dashboard we wanna select the Data Explorer option on the left menu. Now we could also start by selecting Add Container in the top menu; however, going through the data explorer makes what we're doing a little bit more transparent and obvious.
So, in the data explorer UI, we can do all sorts of typical database tasks such as executing queries and changing configuration. Now once you're in the data explorer page simply click on the New Database button to define your first database. All you need to fill in is a name and a number of RUs, request units, and remember that the RU value will define the kind of throughput performance you can expect.
Now once you have a database, its name should appear in the left side menu, the one that's to the right of the leftmost menu with the other Cosmos DB options. So click on the little three dots next to your database name and you'll get an option there to create a container.
So you give your collection a name, you give it an ID, and you give it a partition key, which should be optional, and then you're done. You've got a database and you've got a container within your database. Now, that's just the basic setup and with that clarified let's step back for a minute and try to fully understand how request units work, because this determines our throughput and it determines our cost to a large extent.
How many request units should we set for our account? What is the formula? Azure offers some really good general advice in the documentation. So of course, be sure to check out the links. They also offer a nice request unit calculator tool where you can upload a sample JSON document and input some parameters such as reads and deletes, updates per second, kind of your expected use case, and then the calculator will spit out an estimated optimal value for how many RUs you need.
So, without using that, the basic formula for determining RUs is to multiply the number of desired reads per second by the average item size in kilobytes and then perform the same calculation for desired writes per second and then add those two numbers.
So, for example, if your item size is two kilobytes on average and you need 300 reads per second and 500 writes per second you would get 600 and 1,000 by multiplying those two numbers by two, two kilobytes. And therefore in that scenario you add those two numbers together, you should opt for roughly 1,600 RUs, okay?
Now, there are a few important other variables to consider here; document indexing, complexity of query patterns, and critically the desired consistency level. Now remember there's five; strong, bounded staleness, session, consistent prefix, and eventual. All of these parameters will have an impact on the proper RU setting. So for this reason we strongly recommend taking a look at the documentation and testing out the RU calculator but do keep in mind the formula above. And that's basically it.
That's it for this lesson. Congrats, you now know how to set up the Cosmos DB account, the endpoint, and also calculate RUs, all of this using the GUI. You can create your basic config, you can execute queries, you can add arbitrary data. You're ready to handle basic maintenance for things like request units and other config. Very magical stuff, but of course we can't stop there. Unless you have a very unusual app, you probably want software to handle querying your database instead of humans. So, in the next lesson, we will learn all about the Cosmos DB API and how you can get computers to do all of this stuff for you. See ya there, space cowboy.
Jonathan Bethune is a senior technical consultant working with several companies including TopTal, BCG, and Instaclustr. He is an experienced devops specialist, data engineer, and software developer. Jonathan has spent years mastering the art of system automation with a variety of different cloud providers and tools. Before he became an engineer, Jonathan was a musician and teacher in New York City. Jonathan is based in Tokyo where he continues to work in technology and write for various publications in his free time.