Quick tip: Build a Redis API on top of SingleStoreDB
Tags: singlestoredb, redis, keyvalue
Published: 2023-03-03 21:22:16

## 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](https://github.com/singlestore-labs/s2kv) 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](https://dev.to/singlestore/quick-tip-using-dbt-with-singlestoredb-161g) 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](https://go.dev/dl/go1.20.1.linux-amd64.tar.gz) and [install](https://go.dev/doc/install) Go, as follows:

```shell
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.20.1.linux-amd64.tar.gz
```

We may need to use `sudo`, as follows:

```shell
sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.20.1.linux-amd64.tar.gz
```

We'll add the following to the `PATH` variable:

```shell
export PATH=$PATH:/usr/local/go/bin
```

and then check the version:

```shell
go version
```

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

```
go version go1.20.1 linux/amd64
```

### Install mockgen
Next, we'll install `mockgen`, as follows:

```shell
go install github.com/golang/mock/mockgen@v1.6.0
```

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

```shell
sudo apt install redis-tools
```

### Clone GitHub repo
We'll now clone the following GitHub repo:

```shell
git clone https://github.com/singlestore-labs/s2kv
```

and change to the directory:

```shell
cd s2kv
```

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

```shell
cp config.example.toml config.example.toml.bak
```

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

```
[database]
host = "<host>"
port = "3306"
username = "admin"
password = "<password>"
database = "kv"
```

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:

```shell
mysql -u admin -h <host> -P 3306 -p<password> < schema.sql
```

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

```shell
mysql -u admin -h <host> -P 3306 -p<password> < procedures.sql
```

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:

```shell
go test -config config.example.toml
```

The results should be similar to the following:

```
Connecting to SingleStore database... <host>:3306
PASS
ok   s2kv 1.915s
```

where `<host>` is your host.

## Build and Run
If the tests passed successfully, we can build `s2kv`, as follows:

```shell
go build s2kv/cmd/s2kv
```

and then start the `s2kv` application as follows:

```shell
./s2kv -config config.example.toml
```

## 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> 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
```

## 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](https://www.singlestore.com/).

## Acknowledgements
I thank [Carl Sverre](https://www.linkedin.com/in/carlsverre/) for developing the s2kv example and documentation in the [GitHub repo](https://github.com/singlestore-labs/s2kv).