Multi-Tenant SaaS at Scale with Azure Cosmos DB
Explore Azure Cosmos DB's limitless scale & security for high-performance, cost-effective storage in multi-tenant SaaS platforms with strict isolation.
About this talk
Cosmos DB Conf 2023
SaaS platforms with many tenants need to be able to scale, and also to maintain isolation between customers, while keeping costs under control. This session explains how we exploited Azure Cosmos DB's effectively limitless scale and security features to provide high-performance, cost-effective storage while maintaining strict security and performance isolation between tenants.
Read the transcript
Introduction
Hello, I hope you're enjoying the conference, and this talk is about building large-scale multi-tenant SaaS systems with Azure Cosmos DB. I'm Ian Griffiths and I'm a technical fellow at endjin, a UK-based consultancy specializing in systems that can make effective use of large volumes of data. And we think the key to working effectively at large scales is to use technologies that can amplify your power. Our goal is to enable small teams to achieve big things, and Cosmos DB is a great way to do that.
So I'm going to start with a quick description of a multi-tenant software as a service application that I'll be using to illustrate how we use Azure Cosmos DB. I'll then talk about the challenges specific to building these kinds of applications, especially the conflicts between the need to scale, the need to isolate customers effectively and the need to manage costs. And then I'll describe the Azure Cosmos DB features we use to achieve this.
A white label application for financial advisors
I need to be a little bit vague to avoid breaking NDAs, but I can tell you that the application I'll be mostly referring to is for financial advisors. And that industry has many small firms, most of which can't afford to develop their own software. So we helped to develop a white label solution addressing the common needs of thousands of small to medium-sized companies, and each of those firms is what we're calling a tenant of the system. Now, developing one system for use by multiple firms consolidates the software development costs and the operational costs.
Now, there's a conflict in any multi-tenant system between the demands of the customers and the operator's goal of minimizing costs. Each tenant wants a system that seems to be their own. They'll demand total security isolation: there's going to be no possibility that their data can become mixed up with that of other customers. They'll also want performance isolation, so if one tenant happens to have a very busy day, our other customers will be unhappy if they pay a performance penalty for that.
Customers also tend to have very high expectations around scale. They will expect to be able to grow to any size, but at the same time, individual small customers will not be prepared to pay the kind of money necessary to run a multi-million user system if they've only got three users. SaaS solutions need to have a low entry price. Now, these demands are in direct conflict with the operator's ambitions to minimize operational costs through consolidation. So while each customer wants its own little world, the operator will want to support many customers off a small number of server resources to keep costs low.
Why Azure Cosmos DB?
So where does Azure Cosmos DB fit in? First, its scalability makes it a good choice. If you want to support many millions of users, it can do that easily. More subtly, Azure Cosmos DB makes performance guarantees at scale, even under the highest load, which makes it an excellent choice for a multi-tenant system. You can fully exploit the capacity you've paid for without performance dropping off as you approach the limits.
Finally, Azure Cosmos DB's design provides us with some options for how we can split up our tenants' data for isolation purposes, and this flexibility is important because the trade-offs tend to be application specific. So let's quickly remind ourselves of what the bare minimum Azure Cosmos DB setup involves. You'll need an Azure subscription. There must be an Azure Cosmos DB account containing a database, which will hold a container, and that's where our data will live.
So how might we adapt this picture to support multiple tenants? We could have multiple Azure subscriptions. For example, if you have some very high value customers, they might have negotiated organization level Azure discounts that they want to exploit in your multi-tenant system. Another option would be to have multiple Azure Cosmos DB accounts. This won't usually be the whole answer, because it doesn't provide a way to share operational costs across tenants. However, as we'll see, it can form an important part of a multi-tenancy strategy, and the same applies with creating multiple databases within an account.
Now, perhaps the most obvious way to handle multiple tenants is at the container level, and this is what we did in our financial advisor application. Each tenant has its own container. It's not the only option, but it has much to commend it. As we'll see, there are reasons to go further: you might want a single container to host multiple tenants. There is no one-size-fits-all solution, which is why it's helpful to have a few choices.
Azure Cosmos DB limits
So how do you decide which is right for your app? Azure limits the number of resources you can create. If you're looking to support hundreds of thousands or even millions of tenants, these constraints will affect your design. The first limit is that an Azure subscription won't let you create more than 50 Azure Cosmos DB accounts, but if you contact support, you can raise this limit to a thousand. Our application has many thousands of tenants, so giving each tenant their own Azure Cosmos DB account would have needed multiple Azure subscriptions.
In practice, one account per tenant would be an unusual choice, because you can't share provisioned capacity across accounts, and that would limit your ability to consolidate costs. Now, within one account you can have at most 500 databases, but the limit that really matters here is that any single Azure Cosmos DB account can have at most 500 containers. That limit applies whether that's one database with 500 containers, or 500 databases, one container each, or some other mix.
Since our application gives each tenant its own container, we needed multiple Azure Cosmos DB accounts. A single account would support just 500 tenants, but with multiple accounts you can host half a million tenants in a single Azure subscription, and you can go much higher than that if you also split across subscriptions.
Now, the last row of this table is important. To have 500 containers under one database, each container needs its own dedicated capacity. But if you want to share capacity across containers (and remember, consolidation is a key goal here), then you need to provision capacity at the database level, and you're only allowed to share that capacity across 25 containers max. If you expect significant differences in load between tenants, you may need to think carefully about how you distribute tenants across databases. Now, instead of paying for capacity up front, you can configure Azure Cosmos DB in a pay-as-you-go serverless mode. Now, this tightens the limit to 100 containers per account.
Multiple tenants per container?
Now, these needs lead you to consider isolating tenants at a more fine-grained level. A single container can support enormous workloads, so by putting multiple tenants inside a single container, we could support many more tenants in a single account, and this can also share provisioned capacity more widely. In the one container per tenant model we use in our white label app, provisioned capacity can be shared across at most 25 tenants, but with multi-tenant containers there's effectively no limit on capacity sharing.
There are some downsides. Azure Cosmos DB relies on partitioning to scale, making partition key design critical to performance. So how should we partition multi-tenant data? Broadly speaking, there are two strategies. One is that all the data for a tenant ends up in one partition, and the other is that a single tenant will use multiple partitions.
Using one partition per tenant has its problems. Any single tenant will be subject to Azure Cosmos DB's per partition limit. Now, if you only need to store a few tens of gigabytes per tenant, and you don't need more than a few thousand RUs of capacity for any single tenant, then you'll have plenty of headroom. But if your system has a few outsized tenants, those limits can be a problem. Also, a one partition per tenant approach can make your application more susceptible to noisy neighbor problems, in which one particularly active tenant causes performance problems for others.
But there are problems with distributing a single tenant's data across partitions in a multi-tenant container. First, it makes certain kinds of queries much more expensive. If the results of a query might live in multiple partitions, you will need that query to run on multiple partitions, increasing the cost and reducing the effective scalability, although hierarchical partition keys, which are a preview feature as I record this, can partially mitigate that problem. But it also makes it harder to enforce strict isolation.
Data isolation at runtime
It's critically important not to leak data from one tenant to another. Imagine you're using a hosted database offered by some cloud provider and you notice a query returning data that's not yours. This would be extremely disturbing. You'd wonder who else might be seeing your data. If customers are not confident that a system can protect their confidential data, they will likely choose a competitor's product. And with data regulation laws such as the GDPR, there are often serious legal consequences to data leaks, in addition to commercial ones.
The problem is that consolidation tends to create opportunities for this kind of leak. For example, although our system provisions one container per tenant, we don't spin up new server farms for each tenant. Any single server in our web API is, at any given moment, processing requests simultaneously for several different tenants. The servers in this tier necessarily have access to all of the data, because they can serve requests for any tenant. This means that a coding error has the potential to cause a leak.
Isolation implementation options
So how can we prevent tenant isolation failures? Well, the expensive way is to provision an environment for each tenant, and for some applications that will be a good answer. It's expensive, but some customers will be prepared to pay for that, so you can't reject it out of hand as a design option. However, it does mean abandoning any hope of consolidation.
So what else might we do? Well, we could endeavor to write code that has absolutely no bugs. Let me know how that goes. Or perhaps we could get Azure Cosmos DB to help us. Azure Cosmos DB itself is a multi-tenant system, and considerable resources have been dedicated to ensuring that it offers extremely robust isolation. We should take advantage of that to enforce our own tenant boundaries.
You can do this by ensuring that any code that talks to the data store uses tenant specific credentials. This mitigates bugs in our code where a query asks for data from a different tenant than the one we're currently serving. Azure Cosmos DB itself will prevent data from leaking, because we have used credentials that only provide access to one tenant's data. Of course, bugs can still cause problems. If we accidentally use the wrong credentials, then we're in trouble.
So the code that determines which credentials to use is security critical, but that's a much better situation than having every single line of code that works with the data store being a cross-tenant leak waiting to happen. By leaning on Azure Cosmos DB's own security, we can protect ourselves from some of our own mistakes.
Per-tenant authentication and intra-collection separation
Now, there are a couple of options here. Azure Cosmos DB supports role-based access security, or RBAC for short, in which clients authenticate with Azure AD, and it offers an alternative approach referred to as resource tokens. And in our application we use resource tokens, because it meant we didn't need to provision new identities in Azure AD during tenant onboarding.
Resource tokens can also offer more fine-grained security. If you use RBAC, the narrowest unit of isolation available is the container. So this would have worked in our application, because we use one container per tenant, but if you have multi-tenant containers, resource tokens can do better. These can be narrowed down not just to a container but to a specific partition key. So if you have chosen an approach where each tenant uses exactly one partition key, you can create a resource token that provides access only to one tenant's data. So even if your data access code has cross-tenancy bugs, Azure Cosmos DB will have your back.
If a one-to-one mapping between tenants and partitions won't work for you, then currently Azure Cosmos DB can't provide quite as much support for you. You'll most likely end up with credentials scoped to a particular container. This still limits the blast radius in the case of coding errors, so it's well worth doing. You should never use the all-powerful primary connection string to talk to your database.
You might have hoped that the hierarchical partition key feature (in preview as I record this) would solve this problem, and you can create a resource token that specifies only part of the partition key. Unfortunately, this only works for data change operations and point queries. It does not currently work for queries that might return multiple results. Now, if that limitation is lifted, then it will become possible to create access tokens that completely implement tenant-specific access restrictions, even when that tenant's data spans multiple partitions.
Summary
So to sum up, Azure Cosmos DB is very well suited to large-scale multi-tenant applications, because it can scale to an extraordinary degree, and it's able to provide performance guarantees even under very high load. It has numerous options for splitting up your data, and its security features can do a lot of the heavy lifting required to provide the security isolation your customers will demand. Thank you for listening, and enjoy the rest of the conference.