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,
- 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.
- The storekeeper sends a request to this company.
- The company sends a request to the service provider (Cyberplat).
- The service provider (Cyberplat) sends a request to the provider (Vodafone).
- The provider (Vodafone) makes a recharge and sends the response back to the service provider (Cyberplat).
- 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
Step 2
- Click on 'Overview' and it will show account basic information like Resource Group, Subscription URI etc.
- Click on 'AddCollection'
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'.
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.
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
- 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,
- {
- "id": "3d3e9785-66ff-4560-8a16-99fa80c69401",
- "Operator": "JIO",
- "Provider": "CyberPlat",
- "Region": "Telengana",
- "Mobile": "9769496026",
- "Amount": "500",
- "Status": "Pending",
- "ExecutionTime": "500 ms",
- "CreatedDate": "2018-06-23T22:01:44.3895961+05:30"
- }
- Save above JSON as Transaction.json and upload as the first step of this calculation.
Step 2
The number of documents - As per our business analysis, we assume that max 100000 transactions need to be captured.
Step 3
Expecting max 100 requests we will get per second to write in the collection,
Step 4
Expecting max 500 requests to read for report or query purpose,
Step 5
Expecting max 10 documents needs to be updated per second,
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.
Step 7
- Expecting max 5 documents need to be deleted per second
- Click on 'Calculate' button
- It will show the required Request Units per second and total data storage,
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'
Step 2
- The first thing we have to connect to our Cosmos DB account and for that we need two things,
- 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
- string db = "Recharge";
- string endpointUrl = "https://restapidetails.documents.azure.com:443/";
- string authorizationKey = "pPTTE4JK89I8KH5Ap1folb";
- DocumentClient ddbClient = new DocumentClient(new Uri(endpointUrl), authorizationKey);
- Database ddbDatabase = await ddbClient.CreateDatabaseAsync(new Database() {
- Id = db
- });
Step 3
- The same document client will be used to create collection
- Call 'CreateDocumentCollectionAsync' method where you need to pass database and collection name
- string db = "Recharge";
- string collection = "Prepaid";
- string endpointUrl = "https://restapidetails.documents.azure.com:443/";
- string authorizationKey = "pPTTE4JK89I8KH5Ap1folb";
- DocumentClient ddbClient = new DocumentClient(new Uri(endpointUrl), authorizationKey);
- var docCCollection = await ddbClient.CreateDocumentCollectionAsync("dbs/" + db, new DocumentCollection {
- Id = collection
- });
In the next article, we will see document creation through portal as well as through .NETapplication.