Edge - Database - Surreal

There's an awesome new DB in town. It's called SurrealDB

Deploying A Database Is Not So Hard With Fly.io

SurrealDB is a new kid on the block. This database is ambitious! I got in touch with them to explore their ideas and the brother duo behind it is very passionate about their product. They are building a database that is not only fast but also does not cut any corners.

As always, with every new and ambitious project, there are some challenges. One of them is hosting.

SurrealDB is working on a cloud hosting solution but it is not ready yet. So I decided to explore the hosting landscape and see what the best option would be for hosting SurrealDB. I ended up using Fly.io, a hosting provider that offers a lot of flexibility, a great developer experience and lets you deploy app servers close to your users.

In this article, you will learn how to deploy SurrealDB on Fly.io.

Table Of Content

Requirements

Docker

Docker accelerates how you build, share, and run modern applications. Docker packs up your application and its source code into containers. These containers can then be deployed around the entire world quickly. Docker is flexible to deploy and can be used on any platform. You will use Docker to deploy SurrealDB on Fly.io.

Fly.io

Fly.io on the other hand is a deployment service designed for docker containers, databases and full-stack applications. It gives you the advantage of deploying your app servers close to your users. This way, you have the power to run your full-stack applications all over the world. Fly.io is a great option for deploying SurrealDB.

Running SurrealDB On Docker

  • First, let’s get a feel for how to run SurrealDB on Docker. We’ll use the official Docker image from SurrealDB. The image is available on Docker Hub. We’ll use the following command to run the image:
	bash
		
  
docker run --rm -p 8000:8000 surrealdb/surrealdb:latest start

This is a simple command that will run the image and expose port 8000. We can now access the database on http://localhost:8000 (which in a browser redirects us to the Surreal site). This first test command is a good way to see if the image is working as expected.

  • We can now move to the next step and add some proper authentication to the database.
	bash
		
  
docker run --rm --name surrealdb -p 8000:8000 surrealdb/surrealdb:latest start --log trace --user root --pass root memory

In here we set the most secure password I can think of… user: root, password: root. (do not use this in production, use a strong secure authentication method). We also set the log level to trace so we can see what is going on. We also set the database to memory so we don’t have to worry about persisting the data.

Deploying your Docker Container on Fly.io

  • Install the Fly.io CLI with the following command
	bash
		
  
curl -L https://fly.io/install.sh | sh
# or if you have Homebrew
brew install flyctl

Fly.io CLI is a command line tool that allows you to deploy your Docker containers to Fly.io. It is very easy to use and has a lot of features.

  • Next, Login to Fly.io:
	bash
		
  
flyctl auth login
  • Create a new project and create a Dockerfile.
	bash
		
  
mkdir database
cd database
touch Dockerfile
  • In the Dockerfile, add the following:
	dockerfile
		
  
FROM surrealdb/surrealdb:latest
EXPOSE 8080
CMD ["start", "--bind","0.0.0.0:8080", "file://data/srdb.db" ]

This Dockerfile is very simple. It uses the official SurrealDB image and exposes port 8080. It also sets the command to start the database. We also set the database to file so we can persist the data.

  • Now, you need to prepare your project for deployment. Use the following command:
	bash
		
  
fly launch     # path to the Dockerfile
    --dockerfile ./Dockerfile     # pass --name <name> to set a custom name
    --generate-name     # Don't deploy until we setup the rest
    --no-deploy     # pass --org <org> to seet a different org
    --org personal     # pass --region <region> to set a different region
    --region yul 

This command will create a project and a fly.toml file. The fly.toml file is the configuration file for Fly.io. It contains all the information about your project. It is a very powerful file and allows you to configure a lot of things. We’ll use it to configure the database.

  • Since you are persisting the data, you need to create a volume. Use the following command:
	bash
		
  
 # do change the region if you want
fly volumes create data --region yul
  • Specify at the end of your fly.toml where the application should use this volume:
	toml
		
  
# !This should not be indented! It is a top level key
[mounts]
source="data"
destination="/data"
  • Finally, setup some authentication in env variables:
	bash
		
  
fly secrets set USER=root
fly secrets set PASS=root #This is a BAD password, use a strong one
  • You can now deploy your application:
	bash
		
  
fly deploy

This should generally deploy within 1 minute as Fly.io uses a really cool technology called Firecracker MicroVMs.

  • Once you are done deploying, head over to Surreal React(web-based GUI to use with SurrealDB). You can now connect to your database.

  • Connect with the following credentials: - Host: https://<your-app-name>.fly.dev/rpc - User: root - Password: root

If you get a successful connection you have now deployed properly SurrealDB on Fly.io. You can now use the database in your application.

Conclusion

SurrealDB is a relational database, a document database, and it also does graphing. You can use all of them at the same time. On top of that, it uses SQL, except it stands for Surreal Query Language. So, it’s technically NoSQL. Hosting with Fly.io will reduce latency and give the advantage of hosting your application even closer to your users.

Connect with me on Twitter or on Youtube for chatting or asking questions!

N.B.

I owe a big thank you to Mark Lussier for contributing a life saver tutorial on Github. When I initially setout to write this tutorial, I faced a lot of issues. I was able to get it working thanks to Mark’s tutorial. I highly recommend you check it out if you are having issues deploying SurrealDB on Fly.io.