e.fu

e.fu

[Guide] Implementing Multi-Tenancy in Phoenix 1.8 - Single vs Multi-Organization Approaches

Hello Elixir Community!

I’m excited to share a comprehensive resource I’ve put together on implementing multi-tenant applications in Phoenix 1.8. If you’re building a SaaS application or any system that needs to handle multiple organizations/tenants, this might save you a lot of research time!

What’s covered?

I’ve documented two distinct approaches to multi-tenancy with complete implementation guides:

  1. Single-Organization Model - Each user belongs to exactly one organization at a time

    • Simpler implementation with organization_id directly on users table
    • Straightforward role-based permissions within the single organization
    • Includes system administrator implementation that spans across organizations
  2. Multi-Organization Model - Users can belong to multiple organizations simultaneously

    • Uses a join table (organization_memberships) for flexible organization membership
    • Different roles in different organizations (admin in one, member in another)
    • Organization switching functionality

Both approaches leverage Phoenix 1.8’s new Scopes feature, which is a game-changer for implementing secure multi-tenancy by helping ensure proper data isolation between tenants.

Implementation Details

The documentation includes:

  • Complete database schema designs and migrations
  • Integration with phx.gen.auth
  • Role-based authorization using Bodyguard
  • LiveView implementations with Phoenix 1.8 components
  • Detailed implementation checklists to guide the development process
  • System administration functionality for cross-organization management

Repository Link

You can find everything here: GitHub - ZenHive/OrgsDocs: This repository contains comprehensive guides and implementation strategies for building multi-tenant applications using Phoenix 1.8. The documentation focuses on two different multi-tenancy approaches with role-based authorization. · GitHub

Which Approach Should You Use?

  • Single-Organization: Great for simpler applications where users typically belong to just one organization at a time (like most internal team tools)
  • Multi-Organization: Better for platforms where users regularly collaborate across multiple organizations (like freelancer platforms, consultancies, etc.)

I’d love to hear your thoughts, experiences, or questions about implementing multi-tenant applications in Phoenix. Have you tried different approaches? Any challenges you’ve faced? Any improvements you’d suggest to the implementations?

Happy coding!

Showing Posts 1 to 3

mbuhot

mbuhot

Looks like a great resource, thanks for sharing it!

I’ve recently implemented the Multi-Organization Model with schema-per-tenant approach in the database and Organization slug used as subdomain in the URL.

Getting cookie sessions and content security policies all working correctly when redirecting between subdomains was a little tricky :sweat_smile:

Ash made the remaining work fairly simple, just had to map the Conn.assigns.scope to the tenant and actor opts when calling into the application layer from the web layer.

One challenge was how to implement tests that would create new tenants and trigger the tenant schema migrations, which can be quite slow. In the end we added a test_seeds script to pre-create a few tenants and randomly select one in each test. Seems to work well with the ecto sandbox.

dhony

dhony

Hello.

Thanks for your content. I’ve managed to create a tiny Multi-tenancy app following your instructions:

https://github.com/dhonysilva/bywater

boriscy

boriscy

Excellent thanks for this article. In my case I have to create a user for each organization since I’m making an ecommerce platform with multitenant similar to Shopify for my country Bolivia. I think in this case since almost each organization will have it’s own domain it makes sense to not create many to many relationship usrs < - > org. I can reevaluate and think better how to use scopes on with multitenant and I was having some trouble with some concepts but your article is fresh air and gets me thinnking my approach.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
maennchen
:warning: Security advisory: Decimal DoS vulnerability A vulnerability has been published for decimal where very large exponents can cau...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews