Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Capstone: Isolated Multi-Tenant SaaS with Spiky Load

Track

Mixed

Brief

An eight-person platform team is building a SaaS product where tenants submit usage records, configure alerts, and receive monthly invoices. Traffic can jump from a quiet baseline to a large import burst when one tenant closes its books, but that burst must not consume another tenant’s capacity or expose its data. Regulated customers also require regional storage and tenant-specific encryption keys. Design the feature organization, service boundaries, execution model, and asynchronous flows so the team can ship quickly without making isolation an afterthought.

Constraints

  • Load varies from 10 to 2,500 requests per second within two minutes, and interactive APIs must remain below 500 ms at p95 during imports.
  • Enterprise tenants require dedicated encryption keys; designated tenants also require a dedicated regional database rather than pooled storage.
  • The platform must enforce tenant-aware quotas so one import cannot starve unrelated tenants.
  • Eight engineers use managed cloud functions, managed PostgreSQL, and an existing event broker, with one small on-call rotation.
  • Tenant ID must come from verified identity claims, never from a client-supplied body field or unchecked header.
  • Events and stored artifacts must remain in the tenant’s approved region and support duplicate-safe consumption.

Required Patterns

  • Vertical Slice Architecture — WHY: submit usage, configure alerts, and generate invoices can each own their request, validation, handler, and persistence path without shared feature-wide controller layers.
  • Hexagonal Architecture — WHY: ports for tenant storage, key access, identity, and event publication let each slice stay independent of regional and tenant-specific adapters.
  • Microservices — WHY: usage ingestion, alerting, and billing have distinct scaling and deployment profiles and can own separate data instead of sharing tenant tables.
  • Serverless — WHY: managed functions absorb sudden ingestion and alert workloads with concurrency limits and queue-based backpressure instead of permanent peak capacity.
  • Event-Driven Architecture — WHY: accepted usage events can feed alerting and billing asynchronously while tenant partitioning, retry, and deduplication keep bursts controlled.

Solution Sketch

Illustrative pseudocode and configuration — not runnable.

services:
  usage:   owns usage data; functions for ingest/query; regional deployments
  alerts:  owns rules and notification state; scales on regional queue depth
  billing: owns invoices and rating state; independent deployment pipeline

Usage vertical slice: SubmitUsage
  request -> SubmitUsageEndpoint -> SubmitUsageHandler -> UsageStorePort
                                                    -> UsageEventPublisherPort
  no imports from ConfigureAlert or GenerateInvoice internals

SubmitUsageHandler.handle(verifiedIdentity, request):
  tenant = tenantDirectory.require(verifiedIdentity.tenantClaim)
  require request.region == tenant.approvedRegion
  permit = quotaPort.acquire(tenant.id, request.recordCount)
  reject with tenant-safe 429 when permit is denied
  storage = storageFactory.forTenant(tenant)  # pooled regional or dedicated DB
  crypto = keyPort.forTenant(tenant.id)       # tenant-specific enterprise key
  result = storage.appendIdempotently(request.batchId, encrypt(request, crypto))
  publisher.publishAfterCommit(UsageAccepted, tenant.id, region, result)

regional broker:
  partition key = tenant.id + usageStreamId
  event metadata = eventId, tenant.id, region, schemaVersion

alert/billing consumers:
  enforce regional subscription and tenant authorization
  transactionally record consumer + eventId before/with owned state changes
  scale on queue depth with per-tenant concurrency and service maxConcurrency
  interactive reads never wait for import or invoice completion

isolation policy:
  standard = pooled regional DB + tenant predicate enforced by repository + RLS
  regulated = dedicated regional DB + tenant key + separate connection policy

Rubric

  • Tenant identity: Passes if every slice derives tenant context from verified claims and rejects mismatches in requested region; fails if a body or unchecked header selects storage.
  • Isolation enforcement: Passes if the design shows both pooled row-level enforcement and dedicated-database routing, with tenant-specific key selection in the adapter boundary.
  • Slice independence: Passes if each feature owns its endpoint, handler, validation, and ports and does not import another feature’s internals.
  • Service and data boundaries: Passes if usage, alerts, and billing own their state and use events or APIs rather than cross-service database queries.
  • Burst protection: Passes if tenant quotas, function concurrency caps, queue backpressure, and asynchronous processing together protect the 500 ms interactive target.
  • Event safety and residency: Passes if consumers deduplicate transactionally, partition by tenant, and keep publication and subscription within the approved region.
  • Operational fit: Passes if regional dashboards and alerts expose queue age and per-tenant saturation while retaining managed infrastructure and one small on-call rotation.