Question

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

Was it helpful?

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top