Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb single database vs collection per client vs database per client

I want to build a webservice using mongodb and lithium. What is better:

  • Store the data of all the clients in one collection (e.g. all the transaction documents of multiple clients in one collection transactions)

  • Create multiple collections e.g. transactions_client1, transactions_client2 etc.

  • One db per client

the system must be scalable and must be easy to deploy

like image 715
ynh Avatar asked Nov 29 '11 08:11

ynh


2 Answers

To provide direction, one would have to know a lot more about what you want to achieve, exactly.

However, I'd rule out option 2 (multiple collections) because I don't really see any advantages of this approach: you'll have to determine the collection name at run-time, it's a bit more tricky to write your queries and there's a hard limit on the number of collections you can have (roughly 1.5M collections with a 2GB namespace file)

Option 1 can help to isolate clients and might improve security, but there's still the risk of programming errors in selecting the database. Isolating that code is easy, however.

Both of these options have the downside that you can't query across all collections. However, you'll always want to have access to some collections that are essentially shared across all clients (e.g. logs, stats, etc.) so you have the additional burden of separating these.

That is why I'd usually go for option 1.

like image 50
mnemosyn Avatar answered Nov 26 '22 01:11

mnemosyn


I suggest to start from one db and one collection. It usually simpler manage one database. In the future if you will need create separate database(to speedup some client) you can dump data of this client and put it to separate database server (or just in separate collection). Just take this feature in the mind when you will design architecture of your application. So, my opinion is Option 1. From another point of view you can scale with one collection via sharding and replication.

the system must be scalable and must be easy to deploy

Your database will be scalable just because you are using mongodb, that was designed to scale. And you can easily deploy database because it is schemaless. In general, you may need simple copy some data from one server to another. There are a lot ways to do it: copy collection, data import/export, copy database.

like image 25
Andrew Orsich Avatar answered Nov 26 '22 02:11

Andrew Orsich