Building Planet Scale Mobile App using Azure Cosmos DB

Earth.png

How COSMOS DB originated in Microsoft?

In 2011 it was introduced as “Project Florence” to address developer pain-points that are faced by big Internet-scale applications across various developer teams inside Microsoft. Experiencing these problems were not unique to Microsoft’s applications, Microsoft decided to make Azure Cosmos DB generally available to external developers in 2015 in the form of Azure DocumentDB. Document DB is used internally within Microsoft, and is one of the fastest-growing services used by Azure developers world wide.

Azure Cosmos DB is a global distributed, multi-model database that is used in a many types of applications and use cases. It is a perfect choice for any serverless application that needs low order-of-millisecond response times, and needs to scale rapidly and globally.

It supports multiple data models (key-value, documents, graphs and columnar) and many APIs for data access including MongoDB, DocumentDB SQL, Gremlin, and Azure Tables natively, and in an extensible manner

Before I start explaining Azure Cosmos DB in detail , let us have a bird eye view on how Azure Cosmos DB is linked to Mobile Application Development and some of amazing capabilities of Azure Cosmos Db in short.

  • Azure Cosmos DB allows performance to be scaled up or down elastically. This allows Mobile Apps to handle updating profile and stats from dozens to millions of simultaneous Mobile App users by making a single API call.
  • Azure Cosmos DB supports millisecond reads and writes to help avoid any lags during Mobile Apps content rendering or Mobile gaming.
  • Azure Cosmos DB’s automatic indexing allows for filtering against multiple different properties in real-time, e.g. locate players by their internal player IDs, or their Game Center, Facebook, Google IDs, or query based on player membership in a guild. This is possible without building complex indexing or sharding infrastructure.
  • Social features including chat messages, player guild memberships, challenges completed, high-score leader boards, and social graphs are easier to implement with a flexible schema.
  • Azure Cosmos DB as a managed platform-as-a-service (PaaS) required minimal setup and management work to allow for rapid iteration, and reduce time to market.

Azure Cosmos DB capabilities for Mobile Apps.

documentdb-for-mobile

Content Publishing – an example scenario suggested by Microsoft for Cosmos DB

Bow let’s have a look at real world scenario to describe how you can use globally distributed multi-region/multi-master read write patterns with Azure Cosmos DB. Consider a content publishing platform built on Azure Cosmos DB. Here are some requirements that this platform must meet for a great user experience for both publishers and consumers.

    • Both authors and subscribers are spread over the world
    • Authors must publish (write) articles to their local (closest) region
    • Authors have readers/subscribers of their articles who are distributed across the globe.
    • Subscribers should get a notification when new articles are published.
    • Subscribers must be able to read articles from their local region. They should also be able to add reviews to these articles.
    • Anyone including the author of the articles should be able view all the reviews attached to articles from a local region.

Assuming millions of consumers and publishers with billions of articles, soon we have to confront the problems of scale along with guaranteeing locality of access. As with most scalability problems, the solution lies in a good partitioning strategy.

Automatic regional failover for business continuity in Azure Cosmos DB

Azure Cosmos DB simplifies the global distribution of data by offering fully managed, multi-region database accounts that provide clear tradeoffs between consistency, availability, and performance, all with corresponding guarantees. Cosmos DB accounts offer high availability, single digit ms latencies, well-defined consistency levels, transparent regional failover with multi-homing APIs, and the ability to elastically scale throughput and storage across the globe.

Cosmos DB supports both explicit and policy driven failovers that allow you to control the end-to-end system behavior in the event of failures. In this article, we look at:

  • How do manual failovers work in Cosmos DB?
  • How do automatic failovers work in Cosmos DB and what happens when a data center goes down?
  • How can you use manual failovers in application architectures?

Automatic Failovers

Although it is rare but when there is any Azure regional outage or data center outage, Microsoft Azure Cosmos DB automatically triggers failovers of all Azure Cosmos DB accounts with a presence in the affected region.

What happens if a read region has an outage?

Microsoft Azure Cosmos DB accounts with a read region in one of the affected regions are automatically disconnected from their write region and marked offline. The Cosmos DB SDKs implement a regional discovery protocol that allows them to automatically detect when a region is available and redirect read calls to the next available region in the preferred region list. If none of the data center in the preferred data center/region list is available, calls automatically fall back to the current write datacenter or region. No changes are required in your application code to handle regional failovers. During this entire process, consistency guarantees continue to be honored by Cosmos DB.

Once the affected region recovers from the outage, all the affected Cosmos DB accounts in the region are automatically recovered by the service. Cosmos DB accounts that had a read region in the affected region will then automatically sync with current write region and turn online. The Cosmos DB SDKs discover the availability of the new region and evaluate whether the region should be selected as the current read region based on the preferred region list configured by the application. Subsequent reads are redirected to the recovered region without requiring any changes to your application code.

Azure Cosmos DB support various data models such as key/value, columnar, document and graph.

Key/value (table), columnar, document and graph data models are all natively supported because of the ARS (atoms, records and sequences) design that Azure Cosmos DB is built on. Atoms, records, and sequences can be easily mapped and projected to various data models. The APIs for a subset of models are available right now (Document DB, MongoDB, Table, and Graph APIs) and others specific to additional data models will be available in the future.

Azure Cosmos DB has a schema agnostic indexing engine capable of automatically indexing all the data it ingests without requiring any schema or secondary indexes from the developer. The engine relies on a set of logical index layouts (inverted, columnar, tree) which decouple the storage layout from the index and query processing subsystems. Cosmos DB also has the ability to support a set of wire protocols and APIs in an extensible manner and translate them efficiently to the core data model and the logical index layouts making it uniquely capable of supporting multiple data models natively.

Here is a very simple Code Snippet using C# which creates

  1. A database,
  2. A collection,
  3. An order in database
  4. and query the order from Cosmos DB using LINQ.
public class CosmosDBSample
{
        public void PerformOperations()
        {
            // The endpoint to your cosmosdb instance
            var endpointUrl = "[THE ENPOINT OF YOUR COSMOSDB SERVICE HERE]";
 
            // The key to you cosmosdb
            var key = "[THE KEY OF YOUR COSMOSDB SERVICE HERE]";
 
            // The name of the database
            var databaseName = "OrdersDB";
 
            // The name of the collection of json documents
            var databaseCollection = "OrdersCollection";
 
            // Create a cosmosdb client
            using (var client = new DocumentClient(new Uri(endpointUrl), key))
            {
                // Create the database
                client.CreateDatabaseIfNotExistsAsync(new Database() { Id = databaseName }).GetAwaiter().GetResult();
 
                // Create the collection
                client.CreateDocumentCollectionIfNotExistsAsync(
                    UriFactory.CreateDatabaseUri(databaseName),
                    new DocumentCollection { Id = databaseCollection }).
                    GetAwaiter().GetResult();
 
                // Create a Orderinstance
                var order= new Order() { Id = "Order.1", Title= "Chair", Details= "Study Chair" };
 
                // Sava the document to cosmosdb
                client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(databaseName, databaseCollection), student)
                    .GetAwaiter().GetResult();
 
                Debug.WriteLine($"Order was saved in the database with id: {order.Id}");
 
                // Query for the order by title
                var query = client.CreateDocumentQuery<Order>(
                        UriFactory.CreateDocumentCollectionUri(databaseName, databaseCollection))
                        .Where(f => f.Title== "Chair")
                        .ToList();
 
                if (query.Any())
                {
                    Debug.WriteLine("order was found in the cosmosdb database");
                }
            }
        }
}
 
/// <summary>
/// A simple class representing a Order
/// </summary>
public class Order
{
        public string Id { get; set; }
 
        public string Title { get; set; }
 
        public string Details{ get; set; }
}

How we can scale the backend of our Xamarin Mobile Apps ?

Scale on demand

Azure Cosmos DB is a managed database as a service. As your user base grows, you don’t need to worry about provisioning VMs or increasing cores. All you need to tell Azure Cosmos DB is how many operations per second (throughput) your app needs. You can specify the throughput via the Scale tab by using a measure of throughput called Request Units (RUs) per second. For example, a read operation on a 1-KB document requires 1 RU. You can also add alerts to the Throughput metric to monitor the traffic growth and programmatically change the throughput as alerts fire.

cosmos-db-xamarin-scale

Go planet scale

As request to the server of your app grows, you might gain users across the globe. Or maybe you want to be prepared for fault tolerance.

Here is how you can achieve this .

  1. Go to the Azure portal,
  2. Open your Azure Cosmos DB account.
  3. Click the map to make your data continuously replicate to any number of regions across the world.

This capability makes your data available wherever your users are. You can also add failover policies to be prepared for contingencies.

cosmos-db-xamarin-replicate

Attributes of Azure Cosmos DB that make it well-suited for high-performance applications with global ambition.

    • Azure Cosmos DB natively partitions your data for high availability and scalability. Azure Cosmos DB offers 99.99% guarantees for availability, throughput, low latency, and consistency.
    • Azure Cosmos DB has SSD backed storage with low-latency order-of-millisecond response times.
    • Azure Cosmos DB’s support for consistency levels like eventual, consistent prefix, session, and bounded-staleness allows for full flexibility and low cost-to-performance ratio. No database service offers as much flexibility as Azure Cosmos DB in levels consistency.
    • Azure Cosmos DB has a flexible data-friendly pricing model that meters storage and throughput independently.
    • Azure Cosmos DB’s reserved throughput model allows you to think in terms of number of reads/writes instead of CPU/memory/IOPs of the underlying hardware.
    • Azure Cosmos DB’s design lets you scale to massive request volumes in the order of trillions of requests per day.

These attributes are beneficial in web, mobile, gaming, and IoT applications that need low response times and need to handle massive amounts of reads and writes.

References : Official Azure Documentation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.