Search code examples
mongodblithium

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


Solution

  • 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.