LeafhavenDocs
System Architecture

System & Database Architecture

Enterprise Topography, PostgreSQL Row-Level Security, and Asynchronous Event Pipelines

Technical Architecture Specification

High-Level Topology

[ International Buyer Browser ] (Hamburg, Germany)
               │
               ▼ (Anycast TLS 1.3 Handshake < 15ms)
┌────────────────────────────────────────────────────────┐
│             CLOUDFLARE EDGE V8 ISOLATES                │
│  - SSL Termination & GeoIP enrichment                  │
│  - Middleware rewrite: /[tenant]/[locale]-[country]/   │
│  - Edge Micro-Cache (< 15ms TTFB)                      │
└──────────────────────────┬─────────────────────────────┘
                           │
                           ▼
┌────────────────────────────────────────────────────────┐
│           NEXT.JS 15 APP ROUTER (Storefront)           │
│  - React Server Components & Puck AST Layout Engine    │
│  - Server Action: submitRfqAction(payload)             │
└──────────────┬──────────────────────────┬──────────────┘
               │                          │
               ▼                          ▼
┌─────────────────────────────┐ ┌────────────────────────┐
│ COMMERCE CORE (Medusa v2)   │ │ REDIS BULLMQ QUEUE     │
│ - Transactional Outbox      │ │ - Queue: rfq-sync      │
│ - Locks SKU & Packaging     │ │ - Idempotency dedupe   │
└──────────────┬──────────────┘ └──────────┬─────────────┘
               │                           │
               ▼                           ▼
┌────────────────────────────────────────────────────────┐
│            PERSISTENCE & NEGOTIATION LAYER             │
│  - PostgreSQL 16 dengan Row-Level Security (RLS)       │
│  - Twenty CRM Engine (Deals Kanban & Logistics Spec)   │
└────────────────────────────────────────────────────────┘

PostgreSQL 16 Kernel Row-Level Security (RLS)

Tenants are strictly isolated at the database kernel level using a PostgreSQL session context:

CREATE OR REPLACE FUNCTION current_tenant_id() RETURNS UUID AS $$
BEGIN
    RETURN NULLIF(current_setting('app.current_tenant_id', true), '')::UUID;
END;
$$ LANGUAGE plpgsql STABLE;

-- Example: Strict RLS Isolation on B2B Products
ALTER TABLE b2b_products ENABLE ROW LEVEL SECURITY;
CREATE POLICY b2b_products_isolation_policy ON b2b_products
    USING (tenant_id = current_tenant_id());