Questions on Multitenancy
Please note that all opinions are that of the author.
Last Updated On: 2025-11-26 06:51:47 -0500
So I passed my huge multitenancy blog post to another developer on the project and he sent me a bunch of questions. Since I’m writing this in public and I’ve made the code available to people, I thought the very first questions should be publcic also.
- He passed a link to the apartment gem. Me: Apartment is well known but hasn’t been touched in 2 plus years. My gut feel was that a generalized gem would be harder to update than sitting down from scratch and building this from first principles. Given my experience writing this, I am now unsure. Now, that said, I don’t regret writing it from scratch. I’ve wanted to personally build this architecture from the ground up since I first read the Live Journal overviews by Brad Danga 20 years ago and then from when I learned that WordPress is multitenant with individual MySQL databases for everyone.
- Will we have backups in case someone accidentally drops their table? Me: There is no way for an individual to drop their database but, yes ,we need a backup strategy. Added to my kanban board.
- I’ve never used sidekiq so I would like to see how it will be used for migrations(if that’s the route we’re taking). Me: I didn’t go that route handling it as part of deploy but I’ll get you some training on Sidekiq.
- On top of that, do migrations happen at the time they are created or will there be something like a batch of migrations that are ran? Me: Migrations are triggered as part of a Kamal deployment hook.
- What do you think about this gem https://github.com/rails-on-services/apartment Me: See #1.
- Or this one to clean up DBs in tests https://github.com/DatabaseCleaner/database_cleaner Me: Interesting; unclear.
- Are you wanting to stick with the Rake you setup or would you want to add a service layer? I don’t know what the best practices are when it comes to this. I was more wondering when is the right time to use a service. Me: The Rake task is coupled into the deploy engine now so its what we are going to go with. When we get to the level of hundreds of customers then we can re-evaluate. I have personal concerns about service objects and you and I can go voice on that sometime. I know they are popular but they feel more to me like Architecture Astronauts run amok.
- Lastly, at what scope will we have to make big changes. After we have the MVP how many users can this accommodate? I’ve never deployed software so I don’t know how crazy things get if you don’t scope things properly. It was always drilled into me that I should write code that can scale but I don’t know where the bottle necks are. Me: Scalability mostly comes down to database stuff and multitenancy is designed to avoid the issues of crazy amounts of data per user. When we hit scalability issues, we simply add a database host column to the tenant registry and move the data to that database server. And we can (and will) add caching.
- Overall the concept of multitenancy sounds like a no brainer for situations where users will be writing to DB constantly. I’m sure there are pitfalls that I am not seeing but I’m on board with this. Me: Excellent.