SurrealDB Tutorial: A Database to Replace PostgreSQL, Redis, and Neo4j?

Database tutorial - IT technology blog
Database tutorial - IT technology blog

The Nightmare of “Frankenstein Architecture”

Fullstack developers are no strangers to projects burdened by 4-5 different types of databases. You use PostgreSQL for relational data, Redis for caching, Elasticsearch for search, and then add Neo4j if you need to handle network relationships. Maintaining this bulky infrastructure is incredibly time-consuming. It consumes massive resources, especially for MVP projects that need rapid deployment.

In reality, traditional databases usually excel at only one thing. MySQL or Postgres are very rigid but difficult to scale schemas flexibly. Conversely, MongoDB allows for flexible storage, but when you need to JOIN (lookup) multiple tables, performance drops off a cliff. At that point, your code becomes incredibly messy just to handle data logic.

This complexity stems from trying to piece together fragmented parts. When modern applications demand real-time capabilities, flexible scaling, and diverse structures, you need a unified solution. That is where SurrealDB comes in to solve this problem.

What is SurrealDB? Why is the Dev Community Raving?

SurrealDB isn’t just a typical database; it’s a Multi-model Database. It is written in Rust and packaged into a single lightweight binary file of only about 20-30MB. Its power lies in converging the best of all worlds:

  • Relational (SQL): You still use SELECT, JOIN, and GROUP BY just like you’re working with Postgres.
  • Document (NoSQL): Store JSON flexibly; whether you want a schema or not is up to you.
  • Graph: Connect records using graph relationships. You can query millions of connections without the delay of traditional JOINs.
  • Real-time: Built-in Live Queries. Data is automatically pushed to the client as soon as changes occur without needing complex Socket.io setups.

Instead of having to write a Backend API (Node.js, Go) as an intermediary, SurrealDB allows direct connections from the Frontend. A granular permissions system (Permissions) down to the individual record ensures data is always secure.

Setting Up SurrealDB in Record Time

Because it’s optimized with Rust, SurrealDB is extremely lightweight and starts almost instantaneously.

1. Direct Installation on macOS/Linux

Use the classic curl command to install the latest version:

curl --proto '=https' --tlsv1.2 -sSf https://install.surrealdb.com | sh

2. Running with Docker (Recommended)

If you want a clean environment, Docker is the way to go. You only need one command to have a database server ready:

docker run --rm -p 8000:8000 surrealdb/surrealdb:latest start --user root --pass root

The server will listen on port 8000. You can access it immediately with the credentials root/root.

Working with SurrealQL

SurrealQL is a combination of familiar SQL and modern thinking. It helps you manipulate complex data in an incredibly intuitive way.

Lightning-Fast Data Creation

Access the CLI to start tinkering:

surreal sql --endpoint http://localhost:8000 --user root --pass root --ns test --db test

Try creating a user record with flexible fields:

-- Create a new user
CREATE user:admin SET 
    name = 'IT Blog From Zero',
    tags = ['rust', 'database', 'surrealdb'];

-- Get data
SELECT * FROM user;

Graph Queries: No More Slow JOIN Worries

Imagine you’re building a “Like” feature. In traditional SQL, you’d need an intermediary table. With SurrealDB, you just need to create an “edge” connecting two records:

-- Create an article
CREATE article:post1 SET title = 'SurrealDB 101';

-- Create a 'like' relationship from user to article
RELATE user:admin->like->article:post1 SET time = time::now();

-- Get the list of users who liked the article
SELECT ->like->user.name FROM article:post1;

The above command runs with O(1) complexity instead of O(log n) like a typical JOIN. This is extremely important when data reaches millions of rows.

Live Queries: Real-time Without the Backend

Previously, to build a notification feature, I had to use LISTEN/NOTIFY in Postgres and then pass it through Socket.io. With SurrealDB, everything is encapsulated in a single command:

LIVE SELECT * FROM article WHERE tags CONTAINS 'rust';

Whenever someone adds a new article about Rust, your client will receive a notification immediately. This feature helps reduce backend boilerplate code by up to 60%.

A Realistic Outlook: Should You Use SurrealDB Right Now?

While powerful, you should consider your choice carefully based on actual needs:

  1. Startup/MVP Projects: This is the #1 choice. You’ll save weeks of infrastructure setup and API definition.
  2. Social/IoT Applications: Apps that need to handle complex relationships or continuous data streams will best leverage the strengths of Graph and Live Queries.
  3. Production Considerations: SurrealDB is still quite young. The community isn’t as large as Postgres or MySQL yet. GUI tools and ORMs are still being refined.

One formidable point is that SurrealDB has infinite scaling capabilities. You can run it on a single file or deploy a distributed cluster using TiKV as the storage engine without changing your code.

Final Thoughts

SurrealDB is truly a breath of fresh air in a database world long dominated by aging names. Combining SQL, NoSQL, and Graph into one place allows us to focus on product logic instead of wrestling with infrastructure.

If you’re tired of configuring dozens of services just to run a web app, try spending a weekend with SurrealDB. Who knows, it might just be the “secret weapon” for your next project.

Share: