DEV Community

Cover image for Quick tip: Build a Redis API on top of SingleStoreDB
Akmal Chaudhri for SingleStore

Posted on • Edited on

Quick tip: Build a Redis API on top of SingleStoreDB

Abstract

In this short article, we'll see how to install and run a SingleStore GitHub project for implementing a Key-Value API on top of SingleStoreDB Cloud.

Introduction

In this short article, we'll take an existing SingleStore Labs project and demonstrate the ease with which it can be deployed and run on SingleStoreDB Cloud.

Please note that the example is for demonstration purposes only and should not be used for production applications.

Create a SingleStoreDB Cloud account

A previous article showed the steps required to create a free SingleStoreDB Cloud account. We'll use Key-Value Demo Group as our Workspace Group Name and key-value-demo as our Workspace Name. We'll make a note of our password and host name.

Setup local development environment

Install Go

First, we'll download and install Go, as follows:

rm -rf /usr/local/go && tar -C /usr/local -xzf go1.20.1.linux-amd64.tar.gz
Enter fullscreen mode Exit fullscreen mode

We may need to use sudo, as follows:

sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.20.1.linux-amd64.tar.gz
Enter fullscreen mode Exit fullscreen mode

We'll add the following to the PATH variable:

export PATH=$PATH:/usr/local/go/bin
Enter fullscreen mode Exit fullscreen mode

and then check the version:

go version
Enter fullscreen mode Exit fullscreen mode

This will print the installed version of Go. For example:

go version go1.20.1 linux/amd64
Enter fullscreen mode Exit fullscreen mode

Install mockgen

Next, we'll install mockgen, as follows:

go install github.com/golang/mock/mockgen@v1.6.0
Enter fullscreen mode Exit fullscreen mode

Install redis-tools

Next, we'll install redis-tools so that we can use redis-cli:

sudo apt install redis-tools
Enter fullscreen mode Exit fullscreen mode

Clone GitHub repo

We'll now clone the following GitHub repo:

git clone https://github.com/singlestore-labs/s2kv
Enter fullscreen mode Exit fullscreen mode

and change to the directory:

cd s2kv
Enter fullscreen mode Exit fullscreen mode

We'll make a backup copy of the following file:

cp config.example.toml config.example.toml.bak
Enter fullscreen mode Exit fullscreen mode

In the original config.example.toml, we'll modify the settings, as follows:

[database]
host = "<host>"
port = "3306"
username = "admin"
password = "<password>"
database = "kv"
Enter fullscreen mode Exit fullscreen mode

We'll replace the <host> and <password> with the values from our SingleStoreDB Cloud account.

Create a Database and Tables

We are now ready to create the database and tables. This can be done using a MySQL CLI client:

mysql -u admin -h <host> -P 3306 -p<password> < schema.sql
Enter fullscreen mode Exit fullscreen mode

and we'll also create some procedures and functions to support the Key-Value operations in SingleStoreDB:

mysql -u admin -h <host> -P 3306 -p<password> < procedures.sql
Enter fullscreen mode Exit fullscreen mode

We'll replace the <host> and <password> with the values from our SingleStoreDB Cloud account.

Check Configuration

We can check that everything is correctly configured, as follows:

go test -config config.example.toml
Enter fullscreen mode Exit fullscreen mode

The results should be similar to the following:

Connecting to SingleStore database... <host>:3306
PASS
ok   s2kv 1.915s
Enter fullscreen mode Exit fullscreen mode

where <host> is your host.

Build and Run

If the tests passed successfully, we can build s2kv, as follows:

go build s2kv/cmd/s2kv
Enter fullscreen mode Exit fullscreen mode

and then start the s2kv application as follows:

./s2kv -config config.example.toml
Enter fullscreen mode Exit fullscreen mode

Example Queries

From another terminal window, we'll launch redis-cli and try the example queries listed in the GitHub Repo:

$ redis-cli
127.0.0.1:6379> exists foo
(integer) 1
127.0.0.1:6379> del foo
(integer) 1
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> set i 1
OK
127.0.0.1:6379> incrby i 2
(integer) 3
127.0.0.1:6379> get i
"3"
127.0.0.1:6379> sadd set 1
OK
127.0.0.1:6379> sadd set 2
OK
127.0.0.1:6379> sadd set 3
OK
127.0.0.1:6379> scard set
(integer) 3
127.0.0.1:6379> sadd bar 2
OK
127.0.0.1:6379> sadd bar 3
OK
127.0.0.1:6379> sadd bar 4
OK
127.0.0.1:6379> sinter set bar
1) "3"
2) "2"
127.0.0.1:6379> quit
Enter fullscreen mode Exit fullscreen mode

Summary

In this article, we have seen a Key-Value implementation on top of SingleStoreDB. The GitHub repo provides the basis to extend and improve the work. SingleStoreDB offers many features and capabilities beyond Key-Value, so check out the website.

Acknowledgements

I thank Carl Sverre for developing the s2kv example and documentation in the GitHub repo.

Top comments (0)