What are the best practices for managing multi-tenancy in a Phoenix application?

Hi everyone,

I’m building a collaborative text editor SaaS with Phoenix, similar to Notion, where users can create teams and invite others. I’m facing a challenge with managing multi-tenancy and specifically, how to reliably determine the current team (tenant) for a user across sessions.

Here’s a breakdown of my setup and the issue:

  • Database Structure:
    • I have a users table and a teams table.
    • Each user must belong to at least one team (their default team, created during onboarding).
    • Users can belong to multiple teams.

  • Current Flow:
    • During onboarding, a default team is created for the new user.
    • I’m considering storing the user’s currently selected team (tenant) in the session.
    • On subsequent requests, I would retrieve the team from the session for context.
  • The Problem:
    • When a user logs out and back in, the phx.gen.auth functionality renews the session, causing the selected team to be lost.
    • This results in queries failing, as the application can’t determine the correct tenant.
  • Potential Solution (and Concerns):
    • I’ve considered storing the user’s current team selection in the database.
    • However, I’m unsure if this is a good practice and how to best structure the database to support this.
    • I am unsure of the best way to handle the setting and retreival of the users current selected team.

My Questions:

  • What are the best practices for managing multi-tenancy in a Phoenix application, specifically for handling user team selection across sessions?
  • Is storing the current team selection in the database a viable approach? If so, what is the best way to implement it?
  • Are there other strategies that people have used to solve this problem?

I understand this is more of an architectural question than a Phoenix-specific one and it’s probably a very common solved problem, but it’s my first application with this architecture and I’d greatly appreciate any insights or guidance you can offer.

  • What are the best practices for managing multi-tenancy in a Phoenix application, specifically for handling user team selection across sessions?
  • Is storing the current team selection in the database a viable approach? If so, what is the best way to implement it?

i think your second point kind of answers your first point; if you want the last_team_id to be persisted when the user logs out and logs back in, then you’ll need to store it somewhere (could be in a cookie or in the database).

i can’t think of any problems with saving the last_team_id in the database. That approach also has the benefit of being pretty straightforward, meaning it would be relatively easy to pivot away if you realize it leads to problems later on.

  • Are there other strategies that people have used to solve this problem?

i mentioned the cookie approach, but it comes with more downsides than it you were to save the last_team_id in the database. for instance, cookies are saved on individual devices; that means that if User #123 logged in through their phone and picked Team #2, then the cookie used to save that info won’t be available if User #123 logged in through their laptop later on.

1 Like

Yeah i think this is the most viable solution as long as i do the updates correctly when users switches the team, thanks bro!