KatInterview

Design a Shared SQL Canvas with Immutable Releases and Async Runs

System DesignModel answer
MediumModel answerSystem Designsystem-designdata-warehousedistributed-systems

Last updated

Design a collaborative SQL editor featuring real-time document sync, frozen releases, and async execution tracking with strict input-output lineage.

You are tasked with designing a shared SQL Canvas for an enterprise analytics platform. The platform enables data engineers and analysts to collaborate on SQL queries, create immutable snapshots (releases), and execute queries against external cloud warehouses asynchronously while retaining total traceability of inputs and results.

Core Functional Requirements

  1. Real-time Collaboration: Workspace users can concurrently edit shared SQL drafts. Editors must see live presence indicators and cursor positions of active collaborators.
  2. Frozen Snapshots: Any editor can lock the current draft state into an immutable, versioned snapshot containing the exact SQL script, parameter definitions, default values, and target warehouse settings (such as target cluster, timeout, and row limits).
  3. Async Execution Engine: Authorized users can trigger execution of a specific snapshot by providing runtime parameter values. Queries run asynchronously (taking anywhere from a few seconds up to 45 minutes) and must support background execution without requiring an open browser session.
  4. Result Traceability and Side-by-Side Comparison: Each run records terminal state, execution duration, total row counts, error logs, a truncated result preview (up to 10,000 rows), and a link to a full export artifact. Users can view any two historical runs side-by-side with complete lineage of SQL code, parameters, settings, and output differences.
  5. Role-Based Access Control (RBAC): Workspace roles are categorized as Viewer (read-only access to drafts, snapshots, and results), Editor (can modify drafts and freeze snapshots), and Runner (Editor permissions plus the ability to submit query runs). Database credentials must be handled strictly server-side and never exposed to client applications.

Non-Functional & Scale Requirements

  • Usage: 25,000 active workspaces, 200,000 Monthly Active Users (MAU), and up to 5,000 concurrent active editors at peak.
  • Run Volume: 500,000 execution requests per day, peaking at 40 runs/second.
  • Latency & Reliability: Document edits must propagate to concurrent collaborators in under 250ms (p95). Zero lost edits after client acknowledgement. Guaranteed at-most-once execution side-effects on the warehouse under client retries or network failures.
  • Immutability Guarantee: Query parameters, environment settings, and code bound to a specific execution run must be completely unmodifiable once submitted.

Clarifying Questions to Address

  • How should concurrent edit conflicts be resolved? Is CRDT/OT required or structured pessimistic/optimistic locking?
  • How are unexpected worker failures or lost warehouse connections handled mid-run?
  • How are query cancellations managed, and what is the retention model for preview data vs full exports?

Deliverables

  • Data Model: Schema design supporting collaborative state, version snapshots, execution runs, and output artifacts.
  • Real-time Synchronization Architecture: Protocol and handling logic for editing, presence, and reconnection semantics.
  • Run Execution Lifecycle: End-to-end design covering API contracts, queueing, worker scheduling, idempotency, cancellation, and recovery from indeterminate states.
  • System Architecture & Key Trade-offs: Architecture diagram, scaling strategies, and detailed evaluation of system trade-offs.

Hints

Hint 1

Decouple the mutable collaboration domain (high write frequency, ephemeral) from the execution domain (immutable records, low write frequency).

Hint 2

Use client-side operation deduplication and a deterministic warehouse-side execution ID (e.g., query query_id/run_id labels) to prevent duplicate runs upon worker recovery.

Hint 3

Leverage consistent hashing or sticky sessions for routing collaboration web sockets to keep document state in-memory before persisting compaction frames.