← Back to Writings
SYS.DOC
· Updated:

Polyglot Persistence & Domain Event Buses: Scaling Multi-Tenant SaaS

How we combine Hexagonal Architecture, Redis Streams/PubSub, MongoDB Beanie ODM, and PostgreSQL concurrency locking for real-time enterprise workloads.

Polyglot Persistence & Domain Event Buses: Scaling Multi-Tenant SaaS

No single database engine is optimal for every access pattern in a modern enterprise SaaS. Across Suite Aerya (conversational AI) and BrickTicket (real-time construction ERP & job costing), forcing vector search, ephemeral pub/sub state, and ACID financial ledgers into a single datastore creates immediate throughput bottlenecks.

1. The 3-Tier Polyglot Storage Topology

We separate storage strictly by latency and consistency guarantees:

  1. L1 Edge & Hot Cache (Cloudflare KV / D1 & Redis Cache — 1.8ms hit): Tenant configuration, session tokens, and idempotency keys with strict TTL eviction.
  2. L2 Event Backbone (Redis Pub/Sub & Cloud Tasks — 4,820 msg/sec): Non-blocking domain events (OrderCostUpdated, AgentTurnCompleted, GanttScheduleShifted) fan out to asynchronous workers.
  3. L3 System of Record (PostgreSQL + MongoDB Atlas 3072d): Relational ACID transactions with optimistic concurrency control for BrickTicket financial job-costing ledgers, paired with document + vector collections via Beanie ODM for high-dimensional semantic retrieval.

2. Hexagonal Boundaries in Python 3.13 & TypeScript

By enforcing Hexagonal (Ports & Adapters) boundaries, domain services remain 100% agnostic of whether an event is dispatched over in-memory queues during unit tests, Redis Pub/Sub in staging, or Google Cloud Pub/Sub in production.

INTERACTIVE TELEMETRY SIMULATOR // SYS.BENCH

Edge Caching & Multi-Agent p95 Latency Simulator

613.4M evt/mo
14,200 evt/m
68%
Effective p95 Latency
110ms
vs 612ms unoptimized
LLM + Vector Compute Saved
$75,085/mo
3,072d embedding bypass
DO SQLite State Read
0.42ms
Zero network hop (Co-located)