Back to Documentation
Documentation / Popular / Multi-tenancy Setup
Enterprise30 min read

Multi-tenancy Setup

Configure secure multi-tenant architecture for enterprise organizations

Multi-Tenancy Overview

Manage multiple organizations with complete data isolation and custom branding

Architecture Types

Shared Database

Single database with tenant_id columns

  • ✓ Cost-effective
  • ✓ Easy maintenance
  • ✓ Efficient resource usage

Isolated Database

Separate database per tenant

  • ✓ Maximum isolation
  • ✓ Custom schema per tenant
  • ✓ Enterprise compliance

Setup Steps

Step 1: Enable Multi-Tenancy

// Configuration
{
  "multiTenancy": {
    "enabled": true,
    "mode": "shared_database", // or "isolated_database"
    "tenantIdentifier": "subdomain" // or "header" or "path"
  }
}

Step 2: Create Tenant

POST https://api.cognexiaai.com/v1/tenants

{
  "name": "Acme Corporation",
  "subdomain": "acme",
  "settings": {
    "timezone": "America/New_York",
    "currency": "USD",
    "dateFormat": "MM/DD/YYYY"
  },
  "branding": {
    "logo": "https://cdn.acme.com/logo.png",
    "primaryColor": "#0066CC",
    "customDomain": "erp.acme.com"
  }
}

Step 3: Configure Access

Set up tenant-specific authentication and authorization.

// Middleware for tenant resolution
function resolveTenant(req, res, next) {
  const subdomain = req.hostname.split('.')[0];
  
  // Load tenant from database
  const tenant = await getTenantBySubdomain(subdomain);
  
  if (!tenant) {
    return res.status(404).send('Tenant not found');
  }
  
  req.tenant = tenant;
  next();
}

Data Isolation

Row-Level Security

All queries automatically filtered by tenant_id

User Segregation

Users can only access their tenant's data

Cross-Tenant Prevention

API validates tenant context on every request

Custom Branding

// Tenant branding configuration
{
  "tenantId": "acme",
  "branding": {
    "logo": {
      "light": "https://cdn.acme.com/logo-light.png",
      "dark": "https://cdn.acme.com/logo-dark.png"
    },
    "colors": {
      "primary": "#0066CC",
      "secondary": "#FF6B35",
      "accent": "#4ECDC4"
    },
    "fonts": {
      "heading": "Inter",
      "body": "Inter"
    },
    "customDomain": "erp.acme.com",
    "customCSS": ".custom-header { ... }"
  }
}

Tenant Management

Provisioning

Automated tenant creation with default settings and sample data

Billing

Per-tenant usage tracking and subscription management

Backup & Recovery

Tenant-specific backup policies and point-in-time recovery

Migration

Tools to move tenants between regions or architectures

Performance Optimization

  1. Index tenant_id columns on all tables
  2. Implement tenant-aware caching strategies
  3. Use connection pooling per tenant
  4. Monitor and alert on per-tenant resource usage
  5. Implement rate limiting at tenant level

Next Steps