Quick Start With Azure Cosmos DB - Day Three

In this article, we will understand the business model & requirement and create Cosmos DB and collection accordingly using the portal as well as through .NET Code.

Introduction

In this article, we will understand the business model and requirement and create Cosmos DB and collection accordingly with the use of the portal as well as .NET Code.

Please go through my previous articles of this series,

Business Model

There is a product-based company in India that provides value-added services (VAS), like mobile recharge, bill payment, and so on. The entire recharge process covers the following procedure,

  1. The end user (who wants to recharge his mobile) visits the recharge store and asks the store-keeper to make a recharge, for example, a Vodafone top-up of Rs. 100.
  2. The storekeeper sends a request to this company.
  3. The company sends a request to the service provider (Cyberplat).
  4. The service provider (Cyberplat) sends a request to the provider (Vodafone).
  5. The provider (Vodafone) makes a recharge and sends the response back to the service provider (Cyberplat).
  6. Service Provider (Cyberplat) sends the same response to the company and the company reverts to the recharge store and finally sends it to the end user.

Requirement

As mobile is very common for individuals, hence the volume of recharge transactions also will be high and it requires high storage to persist all the transactions and it should be accessible without latency. 

So let's see how we can create database and collection for the same.

Step 1

  • Login to Azure Portal
  • Click on 'Azure Cosmos DB' service under all services
  • It shows the Cosmos Account which we have created in Day 1
  • Click on the 'utilitybillpayments' account

    Azure Cosmos DB

Step 2

  • Click on 'Overview' and it will show account basic information like Resource Group, Subscription URI etc.
  • Click on 'AddCollection'

    Azure Cosmos DB

Step 3

  • Insert a suitable name for Databaseid, in our scenario, we have defined 'Recharge' or select existing if already created
  • Insert collectionid, here we have inserted 'Prepaid'
  • As of now keep storage capacity to Fixed, in the coming articles will discuss in detail about this.
  • Throughput - Let's keep it 1000 as of now.
  • Click on 'OK' button and it will take some time to create database 'Recharge' and collection 'Prepaid'.

    Azure Cosmos DB

Step 4

  • Now you can see Database 'Recharge' and under that 'Prepaid' as a collection
  • Explore 'Prepaid' collection and you can find 'Documents'
  • Click on 'Documents' and it will explore all existing documents, currently, we haven't created any document hence showing empty.

    Azure Cosmos DB

In the third step, we have seen throughput (400 - 10000 RU/s) and we kept 1000 as it is which is by default selected. The question is what is 'RU/s' and how can we calculate for our requirement.

RU/s means Request Units per Second. On an average how many requests we expect for our Cosmos DB irrespective of Create, Read, Update and Delete. We do not need to worry as we have online calculator through which we can get throughput value.

Go to the below link,

https://www.documentdb.com/capacityplanner

Step 1

  • This is the online calculator which provides Request Units in Azure Cosmos DB
  • The first thing it asks to 'Upload Document' to consider the size

    Azure Cosmos DB
  • So as per our business model and requirement, we have decided to capture operator, provider, region, mobile number, transaction amount, transaction status etc.
  • Final JSON document will be as below,
    1. {  
    2.     "id""3d3e9785-66ff-4560-8a16-99fa80c69401",  
    3.     "Operator""JIO",  
    4.     "Provider""CyberPlat",  
    5.     "Region""Telengana",  
    6.     "Mobile""9769496026",  
    7.     "Amount""500",  
    8.     "Status""Pending",  
    9.     "ExecutionTime""500 ms",  
    10.     "CreatedDate""2018-06-23T22:01:44.3895961+05:30"  
    11. }  
  • Save above JSON as Transaction.json and upload as the first step of this calculation.

    Azure Cosmos DB

Step 2

The number of documents - As per our business analysis, we assume that max 100000 transactions need to be captured.

Azure Cosmos DB

Step 3

Expecting max 100 requests we will get per second to write in the collection,

Azure Cosmos DB

Step 4

Expecting max 500 requests to read for report or query purpose,

Azure Cosmos DB

Step 5

Expecting max 10 documents needs to be updated per second,

Azure Cosmos DB

Step 6

Upload the same Transaction.json to consider update document size, as we are going to change only value, not the entire document, we can select the same JSON document.

Azure Cosmos DB

Step 7

  • Expecting max 5 documents need to be deleted per second
  • Click on 'Calculate' button

    Azure Cosmos DB
  • It will show the required Request Units per second and total data storage,

    Azure Cosmos DB

The same thing i.e creation of database and collection which we have created, you want to create through .net code or say through your application.

Step 1

  • Open visual studio and create one console application
  • Right click on 'References' and click on 'Manage NuGet Packages'
  • Under 'Browse' search for 'cosmosdb'
  • Select 'Microsoft.Azure.DocumentDB' and click on 'Install'

    Azure Cosmos DB
  • Create Database.

Step 2

  • The first thing we have to connect to our Cosmos DB account and for that we need two things,

    • URL
    • AuthKey

  • URL and Authkey, we can get under keys section which we have already seen in Day 2 article.
  • Create document client using URL and Authkey
  • Call 'CreateDatabaseAsync' method where you need to pass database name
    1. string db = "Recharge";  
    2. string endpointUrl = "https://restapidetails.documents.azure.com:443/";  
    3. string authorizationKey = "pPTTE4JK89I8KH5Ap1folb";  
    4. DocumentClient ddbClient = new DocumentClient(new Uri(endpointUrl), authorizationKey);  
    5. Database ddbDatabase = await ddbClient.CreateDatabaseAsync(new Database() {  
    6.     Id = db  
    7. });  
  • Create Collection

Step 3

  • The same document client will be used to create collection
  • Call 'CreateDocumentCollectionAsync' method where you need to pass database and collection name
    1. string db = "Recharge";  
    2. string collection = "Prepaid";  
    3. string endpointUrl = "https://restapidetails.documents.azure.com:443/";  
    4. string authorizationKey = "pPTTE4JK89I8KH5Ap1folb";  
    5. DocumentClient ddbClient = new DocumentClient(new Uri(endpointUrl), authorizationKey);  
    6. var docCCollection = await ddbClient.CreateDocumentCollectionAsync("dbs/" + db, new DocumentCollection {  
    7.     Id = collection  
    8. });  

In the next article, we will see document creation through portal as well as through .NETapplication.