<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rajwinder singh</title>
    <description>The latest articles on DEV Community by Rajwinder singh (@rajwinder_singh_cd4283ac0).</description>
    <link>https://dev.to/rajwinder_singh_cd4283ac0</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2943598%2F8bf3a16d-9d13-4602-96bd-2499f79fc6da.jpg</url>
      <title>DEV Community: Rajwinder singh</title>
      <link>https://dev.to/rajwinder_singh_cd4283ac0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rajwinder_singh_cd4283ac0"/>
    <language>en</language>
    <item>
      <title>How I Designed a Multi-Organization System for my sass</title>
      <dc:creator>Rajwinder singh</dc:creator>
      <pubDate>Tue, 19 May 2026 19:21:30 +0000</pubDate>
      <link>https://dev.to/rajwinder_singh_cd4283ac0/how-i-designed-a-multi-organization-system-for-my-sass-51c2</link>
      <guid>https://dev.to/rajwinder_singh_cd4283ac0/how-i-designed-a-multi-organization-system-for-my-sass-51c2</guid>
      <description>&lt;p&gt;In this blog , I will explain how I designed a multi-organization architecture for my ticket management system and the challenges I faced while implementing secure tenant isolation.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Situation
&lt;/h2&gt;

&lt;p&gt;I was building a ticket management system where teams could collaborate on issues, assign tickets, and manage workflows.&lt;/p&gt;

&lt;p&gt;Initially, the application supported only a single workspace, but later I realized that real-world teams usually work inside organizations similar to GitHub, Supabase, or Jira.&lt;/p&gt;

&lt;p&gt;So I decided to redesign the architecture in a way where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A user can create multiple organizations&lt;/li&gt;
&lt;li&gt;Organizations can have multiple members&lt;/li&gt;
&lt;li&gt;Each organization has isolated tickets and resources&lt;/li&gt;
&lt;li&gt;Users can switch between organizations&lt;/li&gt;
&lt;li&gt;Roles and permissions can be managed independent&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal was to build a reusable multi-tenant foundation that could scale later.&lt;/p&gt;




&lt;h2&gt;
  
  
  Understanding Multi-Tenancy
&lt;/h2&gt;

&lt;p&gt;Before implementing the system, I first needed to understand what multi-tenancy actually means.&lt;/p&gt;

&lt;p&gt;A multi-tenant application is a system where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple organizations share the same backend infrastructure&lt;/li&gt;
&lt;li&gt;But their data remains logically isolated&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This means:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Organization A cannot access Organization B data
even though both use the same database and server.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Choosing architecture
&lt;/h2&gt;

&lt;p&gt;There are multiple ways to multi-tenancy , after some research i found 3 different Approch&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Separate Database per tenant: In this structure each organization have its own database which provide strict isolation and excellent scale but very complex , this architecture is used by enterprise SasS, banking system, when Physical isolation is required&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Separate Schemas per tenant : It bit complex but provide good isolation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Shard DB with tenantId: It is Easy to implement and less time consuming&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For my system , I Choosing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Shared Database + organizationId based on isolation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Because&lt;/strong&gt;:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;easier to scale Initially&lt;/li&gt;
&lt;li&gt;lower infrastructure cost&lt;/li&gt;
&lt;li&gt;faster development&lt;/li&gt;
&lt;li&gt;Easy migration&lt;/li&gt;
&lt;li&gt;Simpler to deploy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note:-Main trade off is enforcing organization-level filtering which need proper indexing&lt;/p&gt;

&lt;h2&gt;
  
  
  Database Schemas
&lt;/h2&gt;

&lt;p&gt;First step is to design Schema as shown&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;model User {
  id                String             @id @default(uuid()) @db.Uuid
  code              String             @unique
  email             String             @unique
  createdAt         DateTime           @default(now())
}

model Organization {
  id                String             @id @default(uuid()) @db.Uuid
  name              String
  createdAt         DateTime           @default(now())
  createdBy         String             @db.Uuid
}

model Membership {
  id             String        @id @default(uuid()) @db.Uuid
  organizationId String        @db.Uuid
  userId         String        @db.Uuid
  roleId         String        @db.Uuid
  createdAt      DateTime      @default(now())
  isSystem       Boolean       @default(false)

}

model Role {
  id             String        @id @default(uuid()) @db.Uuid
  code           String        @unique
  name           String
  permissions    Json
  createdBy      String        @db.Uuid
  organizationId String        @db.Uuid
  isSystem       Boolean       @default(false)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Membership table become core abstraction because it allowed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One user to join multiple Organization&lt;/li&gt;
&lt;li&gt;Different Roles per organization&lt;/li&gt;
&lt;li&gt;Easy invitation system or transfer organization&lt;/li&gt;
&lt;li&gt;RBAC support&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Organization Creation Flow
&lt;/h2&gt;

&lt;p&gt;When a user creates a new organization, the system automatically performs multiple operations behind the scenes.&lt;/p&gt;

&lt;p&gt;The goal was to ensure that every organization always has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a valid owner&lt;/li&gt;
&lt;li&gt;at least one role&lt;/li&gt;
&lt;li&gt;a membership relationship&lt;/li&gt;
&lt;li&gt;protected authorization rules&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The creation flow looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User Creates Organization
        |
        v
Create Organization Row
        |
        v
Create Hidden "Owner" Role
        |
        v
Create Membership Record
        |
        v
Assign User to "Owner" Role
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Request Cycle
&lt;/h2&gt;

&lt;p&gt;Every request in the system flow through this lifecycle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request
   |
   v
Authentication Middleware
   |
   v
Tenant Middleware
   |
   v
Validate Membership
   |
   v
Attach Tenant Context
   |
   v
Execute Scoped Database Queries
   |
   v
Response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  here it follow following steps
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Every request enters the system with the &lt;code&gt;x-organization-id&lt;/code&gt; header attached. This header identifies which organization the user is currently operating in.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The authentication middleware verifies the user and attaches user details to the request object.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;userdata&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The tenant middleware identifies the organization from the request headers and determines what permissions the user has within that specific organization.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The middleware attaches organization details to the request object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;organization&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;organization&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;isOwner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;member&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;OWNER&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;organizationId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Any controller can now directly access the organization ID and organization-related metadata from &lt;code&gt;req.organization&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By applying this approach, there is no need to send the organization ID in every route, such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/api/v1/:orgId/xyz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The active organization is decided by the frontend and passed through the &lt;code&gt;x-organization-id&lt;/code&gt; header, resulting in cleaner and more maintainable APIs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Isolation Strategy
&lt;/h2&gt;

&lt;p&gt;The main challenge in multi-tenancy is isolating each organization's data without leaking information across tenants. Since the database schema is shared among multiple tenants, the safest and most reliable approach is to always filter records by &lt;code&gt;organizationId&lt;/code&gt; during every CRUD operation.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ticket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findMany&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;organizationId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;organization&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By enforcing organization-level filtering consistently across all database operations, the application maintains strong tenant isolation while still using a shared database schema.&lt;/p&gt;

&lt;p&gt;Multi-tenant architectures offer great scalability, but they essentially put all your customers' eggs in one basket. If the "walls" between those eggs aren't reinforced at the code level, a single leak can become a catastrophic breach.&lt;/p&gt;

&lt;p&gt;In Addition of batter Security we need to implement &lt;strong&gt;Row Level Security&lt;/strong&gt; to minimize risk of data leak between tenant&lt;/p&gt;




&lt;h2&gt;
  
  
  Security Challenges in Multi-Tenant Systems
&lt;/h2&gt;

&lt;p&gt;In a multi-tenant environment, security revolves around &lt;strong&gt;Tenant Isolation&lt;/strong&gt; (keeping data separate) and &lt;strong&gt;Access Control&lt;/strong&gt; (ensuring users only do what they are permitted to do).&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Tenant Data Leakage (The "Missing Filter" Problem)
&lt;/h3&gt;

&lt;p&gt;This is the most frequent and severe risk. Because multiple tenants share the same database tables, a single forgotten &lt;code&gt;WHERE&lt;/code&gt; clause in a query can expose sensitive data to the wrong organization.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The Risk:&lt;/strong&gt; An API endpoint like &lt;code&gt;/api/customers&lt;/code&gt; might accidentally return &lt;em&gt;all&lt;/em&gt; customers in the database instead of just those belonging to the requester's &lt;code&gt;organization_id&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Fix:&lt;/strong&gt; Implement &lt;a href="https://devtiven.com/blog/row-level-security-tenant" rel="noopener noreferrer"&gt;&lt;strong&gt;Row-Level Security (RLS)&lt;/strong&gt;&lt;/a&gt; at the database level or use &lt;strong&gt;Global Query Filters&lt;/strong&gt; in your ORM (Object-Relational Mapper) to automatically inject the &lt;code&gt;tenant_id&lt;/code&gt; into every query.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  2. Horizontal Privilege Escalation
&lt;/h3&gt;

&lt;p&gt;This occurs when a user from &lt;strong&gt;Tenant A&lt;/strong&gt; attempts to access resources belonging to &lt;strong&gt;Tenant B&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The Scenario:&lt;/strong&gt; A user changes a URL from &lt;code&gt;tickets/123&lt;/code&gt; (their own) to &lt;code&gt;tickets/124&lt;/code&gt; (another tenant's).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Fix:&lt;/strong&gt; Never rely on the resource ID alone. Your backend logic must perform a "Double-Check":&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Does ticket &lt;code&gt;124&lt;/code&gt; exist?&lt;/li&gt;
&lt;li&gt;Does ticket &lt;code&gt;124&lt;/code&gt; belong to the &lt;code&gt;organization_id&lt;/code&gt; associated with the current user’s session?&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  3. Organization ID Spoofing
&lt;/h3&gt;

&lt;p&gt;Attackers often try to manipulate request headers, cookies, or JWT payloads to "masquerade" as a different organization.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The Risk:&lt;/strong&gt; Trusting an &lt;code&gt;X-Org-ID&lt;/code&gt; header sent by the frontend without server-side verification.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Fix:&lt;/strong&gt; &lt;strong&gt;Never trust the frontend context.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Store the &lt;code&gt;organization_id&lt;/code&gt; inside a &lt;strong&gt;secure, server-signed JWT&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;On every request, cross-verify that the user is a valid member of the organization they are claiming to represent.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  4. Vulnerabilities in the Invitation System
&lt;/h3&gt;

&lt;p&gt;The "Invite User" flow is a common entry point for attackers to bypass standard registration security.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Vulnerability&lt;/th&gt;
&lt;th&gt;Impact&lt;/th&gt;
&lt;th&gt;Mitigation Practice&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Expired Invites&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Unauthorized access via old links.&lt;/td&gt;
&lt;td&gt;Use strict &lt;code&gt;exp&lt;/code&gt; (expiration) timestamps.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Reused Tokens&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Multi-use of a single-use link.&lt;/td&gt;
&lt;td&gt;Implement "One-Time-Use" flags in the DB.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Predictable Codes&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Attackers can "guess" invite URLs.&lt;/td&gt;
&lt;td&gt;Use high-entropy UUIDs or cryptographically secure strings.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Role Escalation&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Invitee changes their role to 'Admin' in transit.&lt;/td&gt;
&lt;td&gt;Sign the role inside the token or re-validate roles on the backend.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;At first, implementing organizations looked simple.&lt;/p&gt;

&lt;p&gt;I thought adding an &lt;code&gt;organizationId&lt;/code&gt; field would be enough to support multi-tenancy, but while building the system I realized tenant isolation affects almost every part of the architecture.&lt;/p&gt;

&lt;p&gt;Things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;authorization&lt;/li&gt;
&lt;li&gt;database queries&lt;/li&gt;
&lt;li&gt;file uploads&lt;/li&gt;
&lt;li&gt;caching&lt;/li&gt;
&lt;li&gt;background jobs&lt;/li&gt;
&lt;li&gt;role management&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;all become organization-aware.&lt;/p&gt;

&lt;p&gt;The lesson I learned was that multi-tenancy is not just a database design pattern. It is a system-wide architectural decision.&lt;/p&gt;

&lt;p&gt;Building this system gave me a much deeper understanding of how SaaS applications structure teams, permissions, and tenant boundaries internally.&lt;/p&gt;

&lt;p&gt;If you've solved this differently or noticed any issues with this approach, I'd like to hear your thoughts. I'm still refining how I structure these systems.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>backend</category>
      <category>saas</category>
      <category>systemdesign</category>
    </item>
  </channel>
</rss>
