#DocumentDB Loves Xamarin: Planet Scale Mobile App in Five Steps

Guest Blogger

This is a special guest post from the DocumentDB team, written by Kirill Gavrylyuk.

Kirill works at Microsoft on the DocumentDB team. You can find him on Twitter at @kirillg_msft.

Earlier this week we announced that DocumentDB, Azure NoSQL database-as-a-service, is now available to Xamarin developers. It has everything a mobile developer needs, a fully managed NoSQL database as a service that scales on demand, and can bring your data where your users go around the globe—completely transparently to your application. Here is what mobile developers get out of the box with DocumentDB:

Overview of DocumentDB feature set.

  • Rich queries over schemaless data. DocumentDB stores data as schemaless JSON documents in heterogeneous collections, and offers rich and fast queries without the need to worry about schema or indexes.
  • Speed, with SLA guarantees. It takes only few milliseconds to read and write documents with DocumentDB. Developers can specify the throughput they need and DocumentDB will honor it with 99.99% SLA.
  • Limitless Scale. Your DocumentDB collections will grow as your app grows. You can start with a small data size and 100s of requests per second and grow to arbitrarily large, 10s and 100s of millions of requests per second throughput and petabytes of data.
  • Globally Distributed. Your mobile app users are on the go, often across the world. DocumentDB is a globally distributed database, and with just one click on a map it will bring the data wherever your users are.
  • Built-in user authorization. With DocumentDB you can easily implement popular patterns like per-user data or multi-user shared data without complex custom authorization code.
  • Geo-spatial queries. Many mobile apps offer geo-contextual experiences today. With first-class support for geo-spatial types, DocumentDB makes these experiences easy to accomplish.
  • Binary attachments. Your app data often includes binary blobs. Native support for attachments makes it easier to use DocumentDB as a one-stop shop for your app data.

Follow the simple five steps below and build an planet-scale app!

Step #1: Getting Started

It’s easy to get started with DocumentDB. Simply go to Azure the portal, create a new DocumentDB account, go to the Quickstart tab, and download a Xamarin.Forms todo list sample connected to your DocumentDB account. 

DocumentDB quickstart page in the Azure Portal.

If you have an existing Xamarin app, you can just add this DocumentDB NuGet package. Today, we support Xamarin.iOS, Xamarin.Android, and Xamarin.Forms shared libraries.

Step #2: Working with Data

Your data records are stored in DocumentDB as schemaless JSON documents in heterogeneous collections. You can store documents with different structures in the same collection.

var result = await client.CreateDocumentAsync (collectionLink, todoItem);

In your Xamarin projects you can use language integrated queries over schemaless data:

var query = client.CreateDocumentQuery (collectionUrl)
    .Where (todoItem => todoItem.Complete == false)
    .AsDocumentQuery ();

Items = new List ();
while (query.HasMoreResults) {
    Items.AddRange (await query.ExecuteNextAsync ());

Step #3: Adding Users

Like many “get started” samples, the DocumentDB sample you downloaded above authenticates to the service using master key hardcoded in the app’s code. This is, of course, not a good idea for an app you intend to run anywhere except on your local emulator.

Instead, we want our app only to have access to the records of the logged in user. Below, for example, is how to modify our todo list app to be a multi-user todo list app. A complete version of the sample is available here

  • Add Login to your app, using Facebook, Active Directory, or any other provider.
  • Create a DocumentDB UserItems collection with /userId as a partition key. Specifying partition key for your collection allows DocumentDB to scale infinitely as the number of our app users grows, while offering fast queries.
  • Add DocumentDB Resource Token Broker, a simple Web API that authenticates users and issues short lived tokens to logged in users, with access only to the documents within the user’s partition. In this example, we host Resource Token Broker in App Service.
  • Modify the app to authenticate to Resource Token Broker with Facebook and request the resource tokens for the logged in Facebook user, then access the user’s data in the UserItems collection.

The diagram below illustrates the solution:

Diagram explaining the authentication flow with DocumentDB and Xamarin.

Step #4: Scaling on Demand

DocumentDB 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 DocumentDB is how many operations per second (throughput) your app needs. You can specify the throughput via the portal Scale tab using a measure of throughput called Request Units per second (RUs). For example, a read operation on a 1KB document requires 1 RU. You can also add alerts for “Throughput” metric to monitor the traffic growth and programmatically change the throughput as alerts fire.

Screenshot showing how to perform scaling in the Azure Portal for DocumentDB.

Step #5: Go Planet-Scale!

As your app gains popularity, you may acquire users across the globe—or maybe you just don’t want to be caught off guard if a meteorite strikes the Azure data centers with your DocumentDB collection. Go to the Azure portal, your DocumentDB account, and with a click on a map, make your data continuously replicate to any number of regions across the world. This ensures your data is available wherever your users are and you can add failover policies to be prepared for the rainy day.

Screenshot of Azure Portal showing how to replicate a DocumentDB instance globally.

We hope you find this blog and samples useful to take advantage of DocumentDB in your Xamarin applications! As always, let us know how we are doing and what improvements you’d like to see going forward for DocumentDB through UserVoice, StackOverflow #azure-documentdb, or reach out to me @kirillg_msft.


Discussion is closed.

Feedback usabilla icon