Last updated

Multi-tenant Architecture

Multi-tenant architecture is essential for applications that serve multiple customers or organizations. This guide explains how to implement secure multi-tenant memory management with the Papr Memory API.

Understanding Multi-tenancy

Multi-tenancy refers to a software architecture where a single instance of an application serves multiple customers (tenants), with each tenant's data isolated from others. In the Papr Memory API:

  • Each tenant has its own isolated memory space
  • Cross-tenant access is strictly controlled
  • Resources can be allocated and managed per tenant
  • Usage metrics are tracked by tenant

Tenant Setup

Creating and configuring tenants:

// Create a new tenant
const tenant = await papr.tenant.create({
  name: "Acme Corporation",
  tier: "enterprise",
  settings: {
    maxMemoryItems: 1000000,
    maxStorageGB: 500,
    enabledFeatures: ["knowledgeGraphs", "semanticSearch", "batchProcessing"]
  }
});

console.log(`Tenant created with ID: ${tenant.id}`);

Tenant Isolation

Ensuring data isolation between tenants:

// Set the current tenant context for operations
papr.setTenantContext(tenant.id);

// Add a memory to the current tenant context
const memory = await papr.memory.add({
  content: "Confidential company information",
  metadata: {
    classification: "internal",
    department: "finance"
  }
});

Sub-tenants

Managing hierarchical tenant structures:

// Create a sub-tenant
const subTenant = await papr.tenant.createSubTenant({
  parentId: tenant.id,
  name: "Acme Finance Department",
  settings: {
    maxMemoryItems: 100000,
    maxStorageGB: 50,
    inheritParentFeatures: true
  }
});

// Set context to sub-tenant
papr.setTenantContext(subTenant.id);

Resource Allocation

Managing resources across tenants:

// Update tenant resource allocation
await papr.tenant.updateResources({
  tenantId: tenant.id,
  resources: {
    maxMemoryItems: 2000000,
    maxStorageGB: 1000,
    maxQueriesPerMinute: 1000
  }
});

Data Migration

Moving data between tenants when needed:

// Migrate specific memories between tenants
await papr.tenant.migrateData({
  sourceTenantId: "tenant-old",
  targetTenantId: "tenant-new",
  dataSelector: {
    filter: {
      "metadata.department": "marketing",
      "metadata.date": {
        $gt: "2023-01-01"
      }
    }
  },
  options: {
    preserveIds: false,
    includeMetadata: true
  }
});

Tenant Management

Administrative operations for tenants:

// List all tenants
const tenants = await papr.tenant.list({
  limit: 100,
  offset: 0,
  filter: {
    tier: "enterprise"
  }
});

// Delete a tenant and all associated data
await papr.tenant.delete(tenant.id, {
  confirmation: "I understand this will permanently delete all tenant data"
});

Usage Monitoring

Tracking tenant resource usage:

// Get tenant usage metrics
const usage = await papr.tenant.getUsage({
  tenantId: tenant.id,
  timeframe: {
    start: "2023-01-01T00:00:00Z",
    end: "2023-01-31T23:59:59Z"
  },
  metrics: ["memoryCount", "storageUsed", "queryCount"]
});

Best Practices

  1. Plan Your Tenant Hierarchy: Design your tenant structure carefully
  2. Implement Robust Authentication: Use strong authentication for tenant access
  3. Set Resource Limits: Prevent any single tenant from consuming excessive resources
  4. Regular Audits: Perform security audits of tenant isolation
  5. Backup Strategy: Implement tenant-specific backup and restore procedures

Next Steps