RavenDB Multi Tenancy
Originally posted at 11/27/2010
One of the features that people asked for RavenDB is the notion of multi tenancy. The idea is that I can (easily) create a new database (there is the RavenDB Server, and the server contains Databases, which contain indexes and documents) for each tenant. As you can imagine from the feature name, while it is actually an implementation of several databases on the same server, the usage goal is to have this for different tenants. As such, the RavenDB implementation is aimed to handle that exact scenario.
As such, it is expected that some users will have hundreds / thousands of databases on a single instance (think shared hosting or hosted raven). In order to support this, we need to have a good handle on our resources.
RavenDB handles this scenario by loading databases on demand, and unloading them again when they aren’t used for a long enough period. Using this approach, the only databases that consume resources are the ones actually being used.
In order to use the multi tenancy features, all you need to do is call:
documentStore.DatabaseCommands.EnsureDatabaseExists("Northwind"); var northwindSession = documentStore.OpenSession("Northwind");
You can now work in a separate database from all other databases, with no potential for data to leak from one tenant to another. Creating a database on the fly s a very cheap operation, as well, so you can create as many of them as you want.