Deploying a Spring Boot + MongoDB application to Azure Web Apps + CosmosDB
Tags: spring, azure, java, cosmosdb
Published: 2019-07-02 20:24:43

---
title: Deploying a Spring Boot + MongoDB application to Azure Web Apps + CosmosDB
published: true
description: This study uses a normal Spring Boot and MongoDB application, and deploys it to Azure Web Apps and CosmosDB. It provides many performance tests, in order to better understand the expected behavior and pricing of the application under high load.
tags: spring, azure, java, cosmosdb
---

## A Spring Boot and MongoDB application story

---

__UPDATE :__ there's a follow up to this post [here](https://dev.to/azure/going-full-reactive-with-spring-webflux-and-the-new-cosmosdb-api-v3-1n2a), where we migrate this application to Spring Webflux and the new CosmosDB SQL SDK to go full reactive, and study the pros/cons of this migration.

---

This blog post comes from a discussion with a client, who uses [JHipster](https://www.jhipster.tech/) extensively to generate Spring Boot microservices, using MongoDB databases. As he is an Azure customer, we had several questions:

- Can he host his whole architecture "as a service"? The Spring Boot applications would be automatically managed and scaled by [Azure Web Apps](https://azure.microsoft.com/en-us/services/app-service/?WT.mc_id=devto-blog-judubois), as well as his databases, using [CosmosDB](https://azure.microsoft.com/en-us/services/cosmos-db/?WT.mc_id=devto-blog-judubois). That means a lot less work, stress and issues for him!
- CosmosDB supports the MongoDB API, but how good is this support? Can he just use his current applications with CosmosDB?
- What is the performance of CosmosDB, and what is the expected price?

So I created a sample Spring Boot + MongoDB application, available at [https://github.com/jdubois/jhipster-cosmosdb-mongodb](https://github.com/jdubois/jhipster-cosmosdb-mongodb). This application is build with [JHipster](https://www.jhipster.tech/) and uses one of our sample models, called the "bug tracker", and which is [available here](https://github.com/jhipster/jdl-samples).

## Migrating from MongoDB to CosmosDB

Our application isn't trivial: it has several collections, with relationships between them. However, all those API calls are supported by CosmosDB, so migrating to CosmosDB is extremely simple: just use the correct connection string, and that's all!

First I created a CosmosDB instance using the [Azure Portal](https://portal.azure.com/?WT.mc_id=devto-blog-judubois), and made sure it used the MongoDB API:

![](https://thepracticaldev.s3.amazonaws.com/i/huheofuhvo96shxnh1ie.png)

Then I selected that instance to get the connection string to my CosmosDB instance:

![](https://thepracticaldev.s3.amazonaws.com/i/j83hkcm1mz1wrwhccqxr.png)

And all I needed to do, in [this commit](https://github.com/jdubois/jhipster-cosmosdb-mongodb/commit/2e68dfb3ff49d4ba8b397b17d2b789563d2a9a2e), was to use that connection string with Spring Boot.

Please note that we use Spring Data MongoDB here, and that it works perfectly fine with CosmosDB.

## Number of collections and pricing considerations

The generated database has 8 collections:

![](https://thepracticaldev.s3.amazonaws.com/i/748xzjyhhizop86mqs8p.png)

Please note that 2 of those collections, `dbchangelog` and `mongobeelock` are used by JHipster to update the database automatically (if you come from the SQL database world, you might know Liquibase which does the same thing). Also, we have 2 collections created by JHipster, `jhi_authority` and `jhi_persistent_audit_event`, which are not 100% useful.

With CosmosDB, you can either pay by collection or by database: the default configuration is to pay by collection, and it is also recommended if you don't have pricing issues, as it will help your application scale better. But in that case, the 4 collections we have just noticed, which aren't really useful, will cost you money.

So if you migrate from MongoDB to CosmosDB, one of the important things to work on is the number of collections: only keep the ones that are truly useful, or this will cost you money for no business value.

## Deploying to Azure Web Apps

As explained on the [JHipster documentation](https://www.jhipster.tech/azure/), this is just a matter of adding the right plugin, which is done in [this commit](https://github.com/jdubois/jhipster-cosmosdb-mongodb/commit/15b7628ad794391ebad8351d21a2ebe6a44511e9).

We are using here [Azure Web Apps](https://azure.microsoft.com/en-us/services/app-service/?WT.mc_id=devto-blog-judubois) as we want to have fully-managed applications. Please note that this allows to "scale up" (= use a bigger server) or "scale out" (= add more servers on the fly), and we will use that later option to scale without coding anything.

## First tests, with 100 users

It was surprisingly easy to deploy our application, but does it scale well? For this, let's use the [Gatling load testing tool](https://gatling.io/), as JHipster automatically generated a load testing configuration in [src/test/gatling](https://github.com/jdubois/jhipster-cosmosdb-mongodb/tree/master/src/test/gatling).

We'll focus on the `Product` entity:

- As soon as the entity is used, CosmosDB will generate a collection for it. Be careful, as by default it will have 1,000 RU/s, so it costs some money.
- This entity also has an issue: it is not paginated! Pagination is useful from a business perspective (if we have 10,000 projects, showing them all to the end-user is not possible), but also from a performance point of view. As we have a limited number of request units per second, let's not waste them by requesting too many data! So we limited the request to 20 projects, in [this commit](https://github.com/jdubois/jhipster-cosmosdb-mongodb/commit/741c3ce29133785b96e9f634c9af56001dad8878).

Our first test uses only 100 concurrent users, and we had a peak at 21 request/second. This isn't a big load, so everything went fine:

![](https://thepracticaldev.s3.amazonaws.com/i/seowmxi3c4zgtrklj7z1.png)

## Going to 500 users

As the first test was too simple, we decided to push the system to 500 users. Everything went well, and we got a peak at 136 requests/second, but the application started to show some issues.

![](https://thepracticaldev.s3.amazonaws.com/i/pii9xql169de6p6ezuz4.png)

In fact, the system is having some slow requests because the authentication process is taking too much time :

![](https://thepracticaldev.s3.amazonaws.com/i/kypd37zjjgpaoxdsnbwn.png)

## Reaching 1,000 users

To go higher, we decided to upgrade our RU/seconds to 5,000 for the authentication collections and the `Project` collection. That's the great thing about paying per collection: you can choose which collection you want to prioritize, depending on your business needs.

This allows us to reach 1,000 users, but we still have the authentication issue, probably because this process is too costly for just one Web application instance. That's why the first requests are slow, and then we go much faster, with a peak at 308 requests/second :

![](https://thepracticaldev.s3.amazonaws.com/i/mds1wvpoiqtves6c75bl.png)

## Up to 10,000 users

With the previous configuration, it was impossible to go above 2,500 users, as the application server wasn't able to handle that load. The good thing with Azure Web App is that you can use it to automatically scale your Web application, using a simple screen :

![](https://thepracticaldev.s3.amazonaws.com/i/owuujs58timwg9hijqh9.png)

This solves the authentication issue, which needs some processing power, as we share it across several servers. This also allows us to support more concurrent requests.

Using this setup, we are able to reach 10,000 active users :

![](https://thepracticaldev.s3.amazonaws.com/i/o6juyruykdas37lel7f4.png)

Also, we had a peak at 1,029 requests/seconds :

![](https://thepracticaldev.s3.amazonaws.com/i/wyurxdkyst4wignz43sr.png)

However, we had 2% of errors here, as we reached 10,000 users too fast for the system to handle all of them correctly.

## Reaching 900 request/second without any issue

As our issue here is the authentication process, and that only used raw Web application power, we did a similar test with 1,000 users again, but this time doing 10 times more requests each. And here we have a very smooth and stable application, which consistently delivers 900 requests/second :

![](https://thepracticaldev.s3.amazonaws.com/i/sm56qyc5v3zys8g5lvge.png)

## Conclusions and thoughts

Our tests show that:

- Running CosmosDB with the MongoDB API works very well at the API level, and is totally able to scale to handle the load
- Azure Web Apps allows to host and scale easily Spring Boot applications

We have gone from 100 to 10,000 users without much trouble, excepted that we had to increase the number of request units for CosmosDB, and configure auto-scaling for Azure Web Apps. This allowed us to keep using Spring Boot and Spring Data MongoDB as usual: both have developer-friendly, concise API that many people love. If we needed to, we could probably have gone further and handle even more users, as long as we are ready to pay for it: the good news is that scalability works well and seems to be linear.