What's the Best Partition Key for Cosmos DB in a Multi-Tenant App?
Asked by
cable splice
Hey everyone! I'm diving into Cosmos DB for my multi-tenant SaaS project, and I need some help with partition keys. Currently, I'm using `/instanceId` to keep tenant data separate. However, I'm a bit concerned since some of my tenants might grow significantly. Is using `/instanceId` a good strategy, or should I consider something more sophisticated like a compound or hashed key? I'd appreciate any straightforward advice! Thanks!
Answers
Hey! Using `/instanceId` is definitely a good starting point for isolating tenant data. But you're smart to think ahead. If you anticipate some tenants becoming quite large, you might want to switch to a composite key, something like `/instanceId-userId` or `/instanceId-type`. This way, you can help distribute the load and avoid hot partitions early on, which is a lot easier than trying to fix it later!
Reply to this answer
Here's a quick breakdown:
- If your tenants are generally small or medium-sized, sticking with `/instanceId` should work just fine.
- If some tenants are likely to grow larger, consider using compound keys or even salted keys to manage distribution better.
- And for those super large tenants? It might be best to put them in their own container to avoid performance issues down the line.
cable splice: Great advice, thanks!
Reply to this answer
Add your answer
Answers are reviewed before publication.
cable splice: Thanks for the tip!