We are all building apps in the cloud. Accounting apps, to-do apps, word processing apps everything. With clients trusting you with their data, the question becomes how to keep their data safe and separate. Keeping your clients data separate is the difference between living in a group commune and being a tenant in an apartment. This is called multi-tenancy.
Multi-tenancy refers to a principle in software architecture where a single instance of the runs on a server, serving multiple customers (tenants). This is a really important part of important feature of cloud computing. This is important because in multi tenant environment customers do not share or see each other’s data.
There are three main ways to build multi tenant databases in MongoDB. The first is by putting all tenants in a single database. The second is putting each tenant in their own individual database, and finally, each tenant in its own collection.
PUTTING ALL TENANTS IN A SINGLE DATABASE
This is the most common form of multi-tenancy and where most web apps start. Putting all your tenants together is a lot simpler and we do not even think about calling it multi-tenancy. Putting all tenants in a single database requires putting the multi tenancy logic into the application level. Enforcing security at the application level can be something as simple as place a enforcing user or customer filters on all data queries, eg. prefixing every database query with a user id.
For a “freemium” business, this will be a better model, since each MongoDB database occupies at least 32MB. Creating hundreds of databases for hundreds of non paying customers can waste a lot of resources.
GIVING EACH TENANT A SEPARATE COLLECTION
This is probably the worst way for MongoDB for a couple of reasons. I won’t go into detail, but this is the method you really want to avoid.
First, Collections in the same database share the same database Lock. MongoDB concurrency has been steadily improving, but it is still there. Second, the default MongoDB
nssize setting limits the number of collections in a database to 24,000. You can go up to 3 million by changing the
nssize setting in the configuration.
GIVING EACH TENANT A SEPARATE DATABASE
The may be the best way depending on your app. Giving each tenant their own databases gives you flexibility in managing and optimising your MongoDB setup. By having a separate database per customer, things like great for moving , managing and deleting client databases become trivial. Since each database is separate, you can create different indexes for different clients depending on their needs.
The downside to this is that each client takes space. If you clients are paying customers, this is not a problem. If you have a free service, then each client will use 32 MB of disk space which is quite a lot if you have a lot of inactive clients.
Even with multi tenancy, it can be hard to pick a shard key. The hashed shard key in MongoDB can provide performance even at scale ( depending on your application )
For most things, performance is application specific. Especially with MongoDB, and the advice here needs to be seen in the light of your application. As always, your mileage may vary. I you are starting an app, for development, you can certainly use one large database, while writing you app logic to support One database per tenant. You may not use this initially with your app, but by putting multi database logic in your app code from the start will save you a lot of heartache if you have any sort of success. And one last thing. Shard Early Picking a shard key is something that is hard to change later