Alexander Nnakwue Software engineer. React, Node.js, Python, and other developer tools and libraries.

Improve database management with SurrealDB

9 min read 2779

Improve database management with SurrealDB

Databases are vital in web development for storing data in different forms and shapes (both structured and unstructured) in an organized format. Their end goal is for the stored data to be easily retrievable, updated, queried, and generally easily administered via a GUI application, dashboard, or even via the command line interface.

Due to these use cases, database management systems and platforms have evolved a lot over the years. Today, we have various database-as-a-service (DBaaS) platforms, also known as managed database services. They basically provide ways of administering databases and offload the work of configuring, maintaining, and managing these databases to various cloud database services and storage providers.

All of this has coincided with the evolution of the cloud era. Using a DBaaS software means that there is no need for a user to deploy a database management system (DBMS) on-premises, since they can now be offered as managed services. DBaaS leverages all the benefits of cloud computing to ensure that the user has a small or nonexistent initial spend on infrastructure.

In this post, we’ll discuss database management systems, and how SurrealDB fits into the larger ecosystem.

Jump ahead:

Classifying database management systems (DBMS)

One way of grouping or classifying DBMS is by their data model, i.e., the kinds of data structures supported and the manner in which the database schema is built.

As we may already know, relational databases store their data in either rows and columns or in a table-like format. Usually, these kinds of databases use SQL to interact with the data stored in them. Non-relational databases, on the other hand, are usually document-based and offer a different approach to querying the data.

Another way of classifying database systems is based on where and how the data is stored. Small databases can be stored on a file system, just like what the operating system uses, while large databases are usually hosted on computer clusters or in the cloud.

With DBaaS platforms, we can access a cloud database system without having to purchase hardware, install additional software, or even having to manage the database. All of these activities are done by the DBaaS provider, including regular upgrades, ensuring database availability at all times, and ensuring downtime is reduced to the barest levels. DBaaS, therefore, allows customers to focus on the core business operations and decisions.

Other important factors to consider when categorizing databases include:

  • The kinds of data to be stored
  • Security and privacy needs that are required for sensitive kinds of data

What is SurrealDB?

SurrealDB is a DBaaS platform that offers a relational model for storing data. In this post we are going to take a look at its features, importance, ideal use cases, how to set it up, and the CLI. Finally, we will get to explore how the features of SurrealDB compare to other DBaaS platforms in the same category, like PostgreSQL, ScyllaDB, and CockroachDB.

According to the documentation, SurrealDB claims to be he ultimate cloud database for all different kinds of applications. It is an end-to-end, cloud-native database that works for most traditional applications, including web and mobile. It can also be used for Serverless, Jamstack, and other backend-based applications.

One of the benefits of SurrealDB is its ability to act as both a database and real-time collaborative API backend layer. What this means is that it can run as a single, plug-and-play database server. It is highly available, highly scalable, and distributed, with support for:

  • SQL querying from client devices
  • GraphQL
  • ACID transactions
  • WebSocket connections
  • Both structured and unstructured data
  • Graph querying
  • Full-text indexing
  • Geospatial querying
  • Row permission-based access

SurrealDB comes with an out-of-the-box, SQL-style query language known as SurrealQL. SurrealQL is rich in terms of support for real-time queries, faster and performant query processing, advanced permissions, and access control for mulit-tenant applications. SurrealDB also claims to be the next-generation database for serverless applications.

SurrealDB is NoSQL in nature, which removes the need for most server-side components and the layers that are usually required when using other kinds of database systems. It is versatile and can fit in easily into any kind of tech stack. Also, you can easily spin it up with a simple command, or even via a direct connection to a client-side application.

It also works in cases where there are a number of queries or analytics to be made on a server. In the next section, we’ll explore some more distinct features of this awesome cloud database.

Features of SurrealDB

Your engineering team can get up to speed with SurrealDB in no time because it is very easy to set up and use. There’s no need to spend time learning a new query language or syntax, as the manner of querying your data in previous databases works right away. It is a fast and highly performant database that scales to your desired demands. Its features include:

  • Distributed ACID transactions: In terms of scalability, SurrealDB handles it under the hood, giving developers few operational and management tasks. This is because manually creating and allocating additional databases or server resources is handled by default, which allows SurrealDB to be highly available, distributed in nature, and ACID-compliant
  • No complex backend servers or APIs: With SurrealDB, we can specify the database and API schema in one place. Once a schema is defined, database access is automatically granted to the relevant parties, removing the need for custom code or integration
  • Support for multi-tenancy: With SurrealDB, we can directly connect to different end-user devices and run SurrealQL queries from web-browsers. This means that users can only see or edit the data that they are permitted to view
  • Inbuilt access and permissions: With its direct client connection, SurrealDB provides inbuilt permissions, speeding up the development process
  • Support for GraphQL and REST APIs: We can query the database with any kind of tool we want, though we have support for SurrealQL, GraphQL (although this is not yet implemented, but is on the roadmap), REST, and JSON-RPC over WebSockets
  • Online and offline data sync: SurrealDB also supports realtime queries. It ensures client devices are in-sync in real time to client applications, end-user devices, and server-side components
  • Highly scalable and distributed in nature: SurrealDB can be run in a standalone mode, in-memory, or as a part of a distributed cluster. SurrealDB is designed to be highly scalable and available in line with its distributed nature, and is implemented in such a way that it can handle multi-table transactions easily, as well as handle document and record IDs without table locking
  • Rewritten in Rust: SurrealDB is built entirely in Rust. As we know, Rust offers a low-memory footprint and also low CPU requirements. As a result, SurrealDB has an advanced querying power and can serve as a database server, which can then be hosted in a distributed cluster
  • Architecture: SurrealDB has a unique architecture with support or can be configured for single-node (in-memory), and on-disk, as a distributed key value store, and also as a distributed (multi-node) cluster
  • Support for multiple different clients and libraries: With SurrealDB, we can make use of most popular languages, including JavaScript, TypeScript, Rust, Go, and so on. The connection to client libraries are made via WebSockets using a binary standard, so as to ensure low-latency, blazing fast query performance
  • SurrealQL: SurrealDB also comes with SurrealQL, an inbuilt SQL-like query language that has slight differences and improvements for data manipulation. Data querying using SurrealQL is done using the SELECT, UPDATE, CREATE, and DELETE methods or statements, similar to regular SQL

Note: For more information regarding the different types of statements, syntax definitions, and example queries available in SurrealDB, take a look at the statements page in the documentation.

Other features include

  • SQL-style querying
  • JOIN-less deep fetching
  • Multi-row, multi-table transactions
  • Multi-column and full-text indexes
  • SurrealDB comes with a strongly typed set of data types that can be easily converted from one form to another
  • SurrealDB comes by default with inbuilt methods for working with or managing many types of data types and structures
  • Transactions for grouping statements together

Installation and getting going with SurrealDB

In terms of installation, there are two ways to run SurrealDB: either from Docker, or by installing the binary image, which is available for the popular operating systems.

Installing from Docker

To install and run with Docker, make sure Docker Hub is running on your machine and go ahead to pull the latest SurrealDB images from Docker Hub. To do so, run this command below:

docker run --rm -p 8000:8000 surrealdb/surrealdb:latest start

The SurrealDB image can be found here, and it contains all of the command-line tools needed to import and export data from a running server, or for running a SurrealDB server itself.

To install SurrealDB on macOS, since are making use of this operating system in this post, we can make use of the home brew package manager. See the command below:

brew install surrealdb/tap/surreal

For other operating systems, like Windows and Linux Mints, we can check out the installation page in the documentation. Once we are done with the installation, is it easy to backup, interact, and run SurrealDB instances.

Performing queries with SurrealDB

Integrating to SurrealDB to perform queries is available via different methods. Many users connect via one of the supported and actively maintained client libraries, which exist for many popular programming languages. Currently, on the server side, SurrealDB supports:

  • Node.js
  • Go
  • Rust

On the client side, it supports WASM, JavaScript, and Ember.js. For further details on using this library, check out the documentation.

To perform SurrealDB queries, REST or HTTP methods are supported. By default, all queries are performed over HTTP API calls with support for SQL. It is easy to modify single-table records or even all table records, if need be, with the use of its HTTP endpoint. Also, we can make use of the usual HTTP endpoints to query table records with the help of SurrealQL. More details on this can be found here in the docs.

Finally, as we highlighted earlier, the WebSockets API supports JSON-RPC querying with support for persistent connection, session auth, realtime queries, and notifications.

Using the SurrealDB CLI

The SurrealDB CLI can be used to import or export data from a local or remote SurrealDB instance. It can be used to manage either a single SurrealDB node or a distributed node cluster.



The start command starts a SurrealDB server in-memory, on a disk, or in a distributed environment. Make sure to install SurrealDB before proceeding.

The import command imports a SurrealQL script file into a local or remote SurrealDB database server. The version command, as usual, shows the current version of the installed CLI and the machine architecture. Other available commands can be found in the documentation.

Use cases and improving DBMS with SurrealDB

SurrealDB has support for both structured and unstructured data with an improved approach to data modeling, compared to other kinds of databases with support for only a single type of data model. It supports simple documents, documents with embedded fields, and graph connections between records. Also, it doesn’t force a specific kind of data model, and allows for both schemaful or schemaless tables, allowing you flexibility to store whatever you need.

When it comes to data querying, SurrealDB comes with a powerful set of querying capabilities. As previously mentioned, it offers a unique querying language, SurrealQL, which is a similar but modified version of traditional SQL. With SurrealQL, documents are traversed and queried efficiently, while still using a language that remains understandable by all. We can write simple SQL-like queries.

SurrealDB supports multi-column indexes that can be easily modified as your application evolves, allowing for consistent and performant read and write operations. Pre-computed table views allow for highly efficient data aggregation for known queries.

It also offers a faster app development time, with its inbuilt permissions and access control. For example, specifying permissions and making sure only those who have the appropriate rights or access are granted these permissions. Permissions for selecting, and performing all CRUD operations are supported, ensuring fine-grained control.

SurrealDB supports SSL/TLS, single-sign-on, external third party auth, JWT token-based authentication, and custom business authentication logic. It offers strong security, with the flexibility of custom authentication methods that can suit any application or business domain.

With SurrealDB, all connections are made via WebSockets, aiding bi-directional data exchange and transfer. Dependent on data access permissions, client-based document subscriptions are triggered in realtime whenever changes are made to the data.

SurrealDB vs. PostgreSQL

PostgreSQL and SurrealDB are quite similar in nature, yet they provide unique feature sets that are worth looking into. While both PostgreSQL and SurrealDB are free and open source relational database management systems, PostgreSQL can be used for commercial and non-commercial projects alike.


More great articles from LogRocket:


PostgreSQL is also available for all major operating systems, including macOS, Linux, Windows, BSD, and Solaris, and has been in active development for over 30 years. It powers many of the applications we use on a daily basis. It is also more stable and has been battle tested over the years, so it is resilient and highly available with support for all kinds of mission-critical workloads.

Although PostgreSQL has issues relating to table and index bloating, it integrates with many other tools and platforms. It also supports distributed ACID transactions — i.e., ones that modify multiple rows in more than one shard — structured and unstructured data, and online/offline data synchronization.

Benefits and advantages of SurrealDB

Unlike other database platforms, SurrealDB combines the database layer, querying layer, API, and authentication layer into one platform. With SurrealDB, one very niche advantage is that there’s no need for complex database and backend setup.

SurrealDB also takes care of most security rules on your behalf. It offers an advanced table- and row-based, customizable access permissions allowing granular data access patterns for different types of users.

Other benefits include:

  • Structured and unstructured data: As mentioned earlier, it is a multi-model kind of database, enabling developers to use multiple techniques to store and model data without having to choose a method in advance. Although it uses tables like other relational databases, it has the added functionality and flexibility of advanced nested fields and arrays
  • Avoids the N+1 query problem: With SurrealDB, we can have links between documents without the use of JOINS, as found in other SQL-related query clients, thus eliminating the common and dreaded N+1 query performance problem
  • Document and graph database: SurrealDB comes with a full graph database function, enabling more advanced querying and analysis.
  • Records, also known as vertices, can be interconnected via edges: With simple extensions to traditional SQL-like queries, we can run queries to target multiple tables or documents in an efficient manner, without the need for table JOINS or other approaches

With SurrealDB, we can also easily extend our database with JavaScript and Wasm functions. This allows for an advanced, custom functionality with computation logic being moved to the data layer. This removes the need to move data to client devices, so as to carry out regular computational workloads. Therefore, only the necessary data is transferred upstream.

Conclusion

With database management systems, we can perform all actions required of databases via a GUI. With these GUIs, we can have a bird’s-eye view of all the data in our database for easy analysis of either the schema or data types, and generally an easy way of administering these databases.

SurrealDB reduces the development time for modern applications because it helps to simplify the entire database and API stack, removing the need for most server-side components. This way, you can easily build secure and performant apps quickly and cheaply. With its design, it can also run in all types of environments, designed to be embedded or run in a distributed manner in the cloud.

With SurrealDB, common traditional database issues like provisioning, scaling, sharding, replication, and correctness are all taken care of. Additionally, because it is a cloud-hosted database, when it comes to hosting or deploying, it becomes an easy job because SurrealDB can be deployed anywhere, even on the SurrealDB Cloud itself. In general, it can be hosted in the cloud, on-premises, embedded, and in edge computing environments.

When it comes to database administration, including general admin and user management, enforcing data security and control, performance monitoring, maintaining data integrity, dealing with concurrency transactions, and recovering information in the case of events such as an unexpected system failure, SurrealDB is the solution.

: Full visibility into your web and mobile apps

LogRocket is a frontend application monitoring solution that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page and mobile apps.

.
Alexander Nnakwue Software engineer. React, Node.js, Python, and other developer tools and libraries.

Leave a Reply