App Center: High-performing backend as a service

App Center: High-performing backend as a service

App Center is growing to become the one-stop-shop for mobile development. Regardless of the framework you're using to build your app, App Center has got you covered for your Build, Test, Distribution, Analytics and Diagnostics. Last year, Microsoft released Push Notifications (Preview) as the first building block of their Mobile backend as a Service. This year, during an awesome session at Microsoft Build, they announced to be extending their Mbaas offering with Data and Auth! In this post I will cover the Data Service and the basics of Auth Service.


Almost every app needs a backend (API) to store and/or receive data. Developing such backend can be challenging, especially when it needs to process a lot simultaneous requests. Besides scalability, security should be also taken into consideration when developing a backend. Security breaches or leaked data may have a catastrophical impact on the success of your app.

When you've created a solid backend, you also need to think about hosting this application and a way to manage the data in your backend. Managing data is often solved by building some kind of Content Management System. All together, building a solid backend may take up a lot of time (and money) that can also be spend on creating awesome apps!

Data Services

To save you from spending a lot of time on developing your backend, Microsoft offers Data Services (early preview). With Data Services you get a backend that:

  • has low latency
  • has high availability
  • is scaleable
  • is secure
  • is maintained (by Microsoft)
  • offers a web interface to manage the data

This is all achieved by leveraging the power of Azure Cosmos DB. Cosmos DB enables you to scale storage across any number of Azure’s geographic regions. You can store unstructured or semi-structured data. When modelling your data, it's recommended to create self-contained entities as JSON documents. More best practices on modelling your data can be found on the Microsoft docs website.

Data Services SDK
In order to consume the data in your application, Microsoft also published a SDK. This SDK allows you to sync your data with a few lines of code. The SDK will take care of authentication, consistency, offline storage & much more. Currently the SDK is available for native Android, native iOS, Xamarin.Forms, Xamarin,iOS & Xamarin.Android.

Partitioning
In order to get high performance, the data in Cosmos DB is separated into partitions. This means, that for every document you want to save, you also need to provide a PartitionKey. At this moment there are two DefaultPartitions you can use: UserDocuments & AppDocuments. To get a high performing backend, choosing the right partition key is very important. You can find best practices on this subject on the Microsoft Docs website.

Authentication
If you don't enable authentication on your App Center Data Service, it will be publicly available. For some data this might be fine, but with privacy in mind, it's often a good idea to secure access to your backend. App Center offers another backend service called Auth that can be used to setup authentication for your backend. This allows you to create private documents, which can only be accessed by the user that created them. Currently, private and public are the only supported accessibility levels, but the App Center team is also looking to implement "group" option. This option would allow you to configure permissions for a group of users, instead of single users.

The easiest way to enable Auth, is by connecting App Center to Azure AD B2C. Azure AD B2C is an identity management system, focused on business-to-consumer (b2c) applications. If you prefer to use a different identity management system, you can still use the Auth features, but then you'll need to provide info about the user using the SDK.

Besides protecting your backend, Auth is enabling you to do even more with App Center. A few examples:

  • Relate crashes to a user instead of a device.
  • Relate events  to a user instead of a device.
  • Send push notifications to a specific user.
  • and more..

Pricing of Data Services
The prices are based on the pricing model from Azure Cosmos DB. You'll pay for the number of Request Units (RU) that you use. For example: Reading 1kb from your backend will cost you 1 RU. The costs are based on a few things: region, hours and RU's. The best way to get an indication for your costs is by using the Microsoft Pricing Calculator.


Getting Started

Setting up your app with App Center Data is pretty straight forward. You'll only need an App Center account and an Azure subscription with some credits.

Configuring the backend

In order to store data with the Data Service, you'll need to connect your App Center app to an Azure Cosmos DB. This can be achieved with the following steps:

  1. Create an app in App Center (with these steps)
  2. Navigate to the app in App Center
  3. Go to Data in the left menu
  4. Click on "new database". In the future, you can also use an existing database if you want, but this is currently not supported.
  5. Click on "Add Subscription" and select the Azure subscription that you want to use.
  6. In the next screen you'll need to provide a name for your database and select the region where you want it to be located.
  7. On the following screen you can choose the required performance. If you're just starting and don't have a clue on what the usage will be, you should choose the "I'm just getting started option". This is the cheapest configuration. You can always change this afterwards in the Azure portal.
  8. On the final screen you'll get a summary of your backend configuration. If everything is good, hit "Create Database".
  9. If creation of the database was initiated successfully, you'll see a message "Connecting your database. This may take a few moments.". When it's done, it'll display a message "Done connecting.".

Now, your backend is all set up!

Setting up your backend in App Center

Data Services in your app

As mentioned before, Microsoft published a SDK to easily consume/send data from/to your App Center backend. Integrating this in your app is similar to other App Center services, and can be achieved with the following steps (for Xamarin.Forms):

  1. Add App Center Nuget package & App Center Data Nuget package to your Android, Forms and iOS project.
  2. Add the following code to the OnStart method in your App.cs (in your Forms code project):
AppCenter.Start("ios={Your Xamarin iOS App Secret};android={Your Xamarin Android App secret}", typeof(Data));

note: you can find the secrets in the App Center portal.

3. Now the SDK is ready to send data to your backend. Wait, it's that easy? Yep!

Storing data in App Center can be done with Unstructured or structured data. The easiest way to get started, is to create classes that contain the data that you want to store. If you want to save Todo items for example, you can create the following class:

public class TodoItem
{
    public Guid ID => Guid.NewGuid();
    public string Name { get; set; }
    public string Notes { get; set; }
    public bool Done { get; set; }
}

4. After you specified a model or multiple models for your data. You can Get, Get multiple, Create, Update & Delete these items with the SDK from/to your backend. Creating a TodoItem would require the following code:


var item = new TodoItem
{
    Name = "Test",
    Notes = "Test notes",
    Done = fale
};

await Data.CreateAsync(item.ID.ToString(), item, DefaultPartitions.UserDocuments, new WriteOptions(_cachingTime));
              

Samples for Getting, Updating and deleting, can be found on GitHub.

Conclusion

Building a resilient and secure backend can definitely be challenging. The MBaas offering from Microsoft makes this a lot easier. The mobile SDK will take care of some complex tasks like offline support, authentication and data synchronisation.

Basically if you need a backend to safely store data in the cloud without a lot of custom business logic, App Center Data Services can be a great solution. Because this service is based on Azure Cosmos DB, you’ll get great performance out-of-the-box. A drawback might be that Cosmos DB is a bit more expensive than other storage solutions. In my opinion, it would be great if you can pick from different storage options for your Data Service!

With Auth Service, Microsoft introduces a way to identify your users more easily. When the user information is known in App Center, this can be used throughout al their services! Integrating Auth can be done with Azure AD B2C, but Microsoft will also allow you to use your own identity provider. This makes it very powerful for (almost) every project!

Bas de Cort

About Bas de Cort

Bas is a software developer from The Netherlands with a great passion for mobile and innovative technology. This blog will mostly cover mobile technology, especially Xamarin.

Comments