Skip to main content

Architecture Overview

DgiDgi is a multi-tenant SaaS platform built on a modern cloud-native architecture. This document provides a high-level overview of the system architecture, key components, and design principles.

Design Principles

  • Multi-Tenant by Design: Complete tenant isolation at every layer
  • Cloud-Native: Built for Cloudflare, Supabase, and Fly.io
  • Edge-First: Global edge deployment for low latency
  • Security-First: Defense in depth with multiple security layers
  • Scalable: Horizontal scaling with stateless services

Infrastructure Stack

LayerProviderPurpose
Edge & CDNCloudflareEdge workers, DNS, static hosting, R2 storage
DatabaseSupabasePostgreSQL with Row-Level Security
ComputeFly.ioAPI servers, background workers
QueueUpstashRedis-based job queues
AuthClerkAuthentication and user management

High-Level Architecture

Component Overview

Edge Layer (Cloudflare)

The edge layer handles all incoming traffic with global distribution:

  • Edge Workers: Route requests to appropriate backends, handle CORS, rate limiting
  • Pages: Host static SPA applications (client, platform, docs, marketplace)
  • KV Storage: Distributed key-value store for rate limits and caching
  • R2 Storage: S3-compatible object storage for files and attachments
  • DNS: Managed DNS with automatic SSL certificates

Compute Layer (Fly.io)

The compute layer runs stateless services that can scale horizontally:

  • API Server: Handles REST API requests, WebSocket connections
  • Worker Pool: Processes background jobs (agent execution, LLM calls)
  • Health Monitoring: Automatic restarts and load balancing

Data Layer (Supabase)

The data layer provides persistent storage with built-in security:

  • PostgreSQL: Primary database with 100+ tables
  • Row-Level Security: Tenant isolation at the database level
  • Connection Pooling: Efficient connection management
  • Migrations: Version-controlled schema changes

Queue Layer (Upstash)

The queue layer manages asynchronous job processing:

  • Job Queues: Priority-based queues (high, normal, low)
  • Dead Letter Queue: Failed job tracking and retry
  • Pub/Sub: Real-time event distribution

Multi-Tenant Architecture

Every component is designed for multi-tenant isolation:

LayerIsolation Mechanism
APIJWT tokens with tenant ID claim
DatabaseRow-Level Security policies
StorageTenant-prefixed object keys (tenants/{id}/)
QueuesTenant ID in job metadata
LLM CallsTenant-specific API keys and quotas
RuntimeIsolated sandbox containers

Request Flow

A typical API request flows through the system as follows:

Scaling Characteristics

ComponentScaling ModelLimits
Edge WorkersAuto-scale globallyUnlimited
API ServersHorizontal (Fly machines)Configurable
WorkersHorizontal (Fly machines)2-10 instances
DatabaseVertical (Supabase plan)Connection pooling
RedisManaged (Upstash)Auto-scale