The Polyglot Database Trap in Modern Architectures
Most engineering teams start simple. A single relational database handles user accounts, billing, and core products. But growth brings complexity. Soon you need deeply nested JSON documents for dynamic schemas, a graph engine for permissions or social graphs, and an in-memory cache to push live notifications to connected browser clients.
Before long, your stack morphs into a fragile patchwork: PostgreSQL for relational tables, MongoDB for document payloads, Neo4j for graph traversals, and Redis with WebSockets for real-time broadcasts. Keeping four separate database engines in sync is an operational nightmare. You spend hours writing reconciliation workers, managing distributed dual-write failures, and debugging cache invalidation bugs when replica lag spikes past 500ms.
Root Cause: Impedance Mismatch and Architecture Sprawl
Specialized single-model engines solve narrow problems well. However, stringing them across a distributed network introduces compounding friction:
- Data Duplication: A single entity often gets copied into three systems—a document store, a graph index, and a cache. A 200-byte profile record easily explodes into multiple redundant storage footprints and out-of-sync states.
- Operational Overhead: Every database demands its own provisioning, access control rules, backup strategy, monitoring agents, and client libraries.
- Heavy Backend Glue: Up to 30% of backend application code ends up dealing with data orchestration and distributed consistency instead of business logic.
SurrealDB takes a different approach. Written from scratch in Rust, it merges schemaless documents, direct pointer-based graph edges, fine-grained access policies, and reactive live queries into a single server process that idles at under 50 MB of RAM.
Core Concepts: How SurrealDB Redefines Storage
Working with SurrealDB requires setting aside traditional relational constraints. The engine merges distinct storage paradigms cleanly under one roof:
1. Documents and Schemas
Records are rich JSON documents. You can run completely schemaless during early prototyping, lock tables down with strict typing in production, or mix both modes in the same collection. Record IDs are first-class strings like user:alice or company:acme. You no longer need synthetic auto-incrementing integer IDs.
2. First-Class Graph Edges
SurrealDB eliminates multi-table relational JOIN operations. Instead, it embeds directional graph traversals directly into SurrealQL using operators like -> and <-. Records point straight to records across edge tables, resolving relationship queries in sub-millisecond lookups.
3. Native Live Queries
SurrealDB builds push subscriptions directly into the storage engine. Clients open a WebSocket connection and issue a standard LIVE SELECT query. The moment a record matching the query filter changes, the server pushes the diff directly to the client. No Redis pub/sub layer or polling daemon required.
Hands-On Practice: Running SurrealDB on Docker
Spinning up a local test instance takes just a few minutes with Docker. We will walk through documents, graph edges, and reactive live queries step by step.
Step 1: Deploy with Docker Compose
Create a clean working directory and add the following docker-compose.yml file. We use RocksDB as the backing file store so your data persists across container restarts.
services:
surrealdb:
image: surrealdb/surrealdb:v2.0.4
container_name: surrealdb-dev
entrypoint:
- /surreal
- start
- --log=info
- --user=root
- --pass=rootpass
- --bind=0.0.0.0:8000
- rocksdb:/data/surreal.db
ports:
- "8000:8000"
volumes:
- surreal-data:/data
restart: unless-stopped
volumes:
surreal-data:
Launch the container in detached mode:
docker compose up -d
docker logs surrealdb-dev
The logs will confirm that the HTTP and WebSocket endpoints are listening on port 8000.
Step 2: Connecting via Surreal CLI
Open an interactive SQL shell inside the running container:
docker exec -it surrealdb-dev /surreal sql \
--endpoint http://localhost:8000 \
--user root \
--pass rootpass \
--namespace production \
--database analytics
You will land on a production/analytics prompt ready for SurrealQL queries.
Step 3: Creating Document Records
Let us populate the database with engineering staff and active projects:
-- Create document records with explicit IDs
CREATE user:john CONTENT {
name: "John Doe",
role: "Backend Engineer",
skills: ["Python", "Rust", "Docker"],
joined_at: time::now()
};
CREATE user:sarah CONTENT {
name: "Sarah Connor",
role: "Lead Architect",
skills: ["Distributed Systems", "Kubernetes"],
joined_at: time::now()
};
CREATE project:portal CONTENT {
name: "Cloud Migration Portal",
status: "Active",
tags: ["cloud", "infrastructure"]
};
If you have legacy data to migrate, converting raw CSV dumps to structured JSON is often the first step. You can use toolcraft.app/en/tools/data/csv-to-json for client-side conversions without sending sensitive company rows over the wire. From there, posting batches of 1,000 JSON records to the SurrealDB HTTP API (POST /key/table) takes just a single curl command.
Step 4: Connecting Entities with Graph Relations
In Postgres or MySQL, linking Sarah and John to the portal requires a join table like project_members with composite foreign keys. In SurrealDB, relationships are first-class edges defined via the RELATE command:
-- Link users to the project and attach edge metadata
RELATE user:john->assigned_to->project:portal CONTENT {
assigned_date: time::now(),
permission: "read_write"
};
RELATE user:sarah->assigned_to->project:portal CONTENT {
assigned_date: time::now(),
permission: "admin"
};
To inspect team members assigned to the portal project, traverse backward along the graph arrow:
-- Traverse from project backwards to find assigned users
SELECT <-assigned_to<-user.name AS members FROM project:portal;
-- Fetch user assignments and edge permissions simultaneously
SELECT
->assigned_to->project.name AS projects,
->assigned_to.permission AS access_level
FROM user:sarah;
SurrealDB returns structured nested JSON immediately. No query planner join costs. No indexing workarounds.
Step 5: Subscribing with Live Queries
To see real-time updates in action, open a second terminal tab and start an active listener:
-- Listen for admin assignments as they happen
LIVE SELECT * FROM assigned_to WHERE permission = "admin";
The console prints a subscription token like "d4f40f37-6797-4221-a486-538a08151475". Now jump back to your primary prompt and assign a new security auditor:
CREATE user:david CONTENT { name: "David Kim", role: "Security Auditor" };
RELATE user:david->assigned_to->project:portal CONTENT {
assigned_date: time::now(),
permission: "admin"
};
Within milliseconds, the listening terminal prints the exact record delta. The engine handles the pub/sub loop internally, saving you from setting up separate Redis clusters and WebSocket push infrastructure.
Operational Trade-offs and Best Fits
Collapsing document storage, graph edges, and live events into one binary cuts infrastructure cost and complexity. You manage one backup file, one security surface, and zero synchronization pipelines.
Workloads running across hundreds of terabytes still benefit from specialized battle-tested engines like PostgreSQL or distributed Neo4j clusters. But for greenfield SaaS platforms, internal developer tools, and event-driven backends, SurrealDB is an exceptional fit. Running it locally on Docker gives you a full multi-model stack without the cloud bill or operational bloat.
