Quick tip: Build a Redis API on top of SingleStoreDB
Akmal Chaudhri
Posted on March 3, 2023
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
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
We'll add the following to the PATH
variable:
export PATH=$PATH:/usr/local/go/bin
and then check the version:
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:
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
:
sudo apt install redis-tools
Clone GitHub repo
We'll now clone the following GitHub repo:
git clone https://github.com/singlestore-labs/s2kv
and change to the directory:
cd s2kv
We'll make a backup copy of the following file:
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:
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:
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:
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:
go build s2kv/cmd/s2kv
and then start the s2kv
application as follows:
./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> 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
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.
Posted on March 3, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.