How to Use SingleStore as a Key-Value Store
Blog How to use SingleStore as a key-value store with plain SQL, so you get fast key-value lookups without running a separate database just for that.
- : The code had a
catchwith notry, calledconsole.err, which doesn’t exist, left outintToStringandreadOne, and never awaited or called its functions.
TL;DR You can skip spinning up a separate key-value database by using SingleStore as one instead. I show you how to create a rowstore table with a hash index and wire it up with Node.js for fast key-value reads and writes using plain SQL.
On this page
Key-value stores are perhaps one of the most popular databases on the market today - and with good reason! They are fast, scalable and super easy to use. But did you know you can get all the advantages of a key-value store, without needing to spin up a separate database dedicated to just that singular purpose? In this post, I will show you how you can create a key-value store in SingleStore, using just SQL.
If you want to check out any of the code used in this demo, feel free to take a look at it on our GitHub repo
A key-value store (also known as a key-value database) can be defined as a non-relational database and simple database that utilizes a hashtable or dictionary as its underlying data model. These can comprise anything from a number or string to more complicated objects that use a key to keep track of the object. In its simplest form, a key-value store uses a simple key-value method to store data. Take a look at the example below:
| Key | Value |
|---|---|
| Joe_Karlsson | @JoeKarlsson1 |
| SingleStore | @SingleStoreDB |
| SingleStore_Devs | @SingleStoreDevs |
There are so many databases out there, and it’s often hard to know when it’s appropriate to use a key-value store over other data storage paradigms. Due to key-value stores being fast and scalable, the following use cases are well suited for this datastore:
-
Web applications to store user session details and preferences
-
Real-time product recommendations and advertisement
-
Data caching to increase application performance
-
Data caching for the data that is not regularly updated
-
Material for big data research
So the big question remains, why not just use a database that was designed to be a key-value store, instead of using a SQL database? Here are a couple of reasons why you should consider using SingleStore as a key-value store on your application:
-
Save money: SingleStore is one-third the cost of legacy databases and offers better cost predictability. Consolidating databases allows you to only pay for one database, instead of two.
- 1 SingleStore is MySQL wire-compatible: It speaks MySQL’s network protocol, so MySQL clients and drivers connect to it without changes.
Use existing skills: Since SingleStore is MySQL wire-compatible1, you don’t need to learn a new language to query your database.
-
Simplify your architecture: Eliminates the need for multiple purpose-built data engines.
-
Increase your speed: Take advantage of ultra-fast data ingestion and sub-second latencies.
I know it can be weird using a SQL database as a key-value store at first, but I promise it’s pretty straightforward once we dig in. It helps that the structure of a key-value store is simple. The first thing you are going to need to do is to create a free SingleStore database. SingleStoreDB Cloud is the easiest way to try SingleStore2, giving you a fully-managed database online in minutes. You can learn more about setting up SingleStore here.
data in a rowstore is also written to the transaction log on disk so that it can be recovered when SingleStore Helios is restarted.SingleStore's docs on choosing a table type
SingleStore supports several different table types depending on the type of work you need to perform with the data stored in it. SingleStore by default uses a column store table - but for a key-value store, we need to make sure we are selecting the in-memory row store3. The following table enumerates the strengths and intended uses of the rowstore and columnstore:
data in a rowstore is also written to the transaction log on disk so that it can be recovered when SingleStore Helios is restarted.SingleStore's docs on choosing a table type
| In-Memory Rowstore | Flash, SSD, or Disk-based Columnstore |
|---|---|
| Operational/transactional Workloads | Analytical Workloads |
| Fast inserts and updates over a small or large number of rows | Fast inserts over a small or large number of rows |
| Random seek performance | Fast aggregations and table scans |
| Updates/deletes are frequent | Updates/deletes are rare |
| Compression |
The in-memory rowstore is better suited for the key-value store since it offers super-fast inserts and random read performance, which is a common use case for this kind of data storage.
The schema for a key-value store is pretty simple since we only need to store a key, and an associated value for that key. We will also use a hash index on the primary key. Hash indexes provide fast, exact-match access to unique values, which is exactly what we want! Let’s look at how we can initialize our key-value store table:
DROP DATABASE IF EXISTS app;
CREATE DATABASE app;
USE app;
CREATE ROWSTORE TABLE IF NOT EXISTS data (
id VARCHAR(255) NOT NULL,
value VARCHAR(255) NOT NULL,
PRIMARY KEY (id) USING HASH
) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Now that we have created the table, let’s go ahead and start adding data, and querying it. The first thing we need to do is connect to our database.
I am going to be connecting to our cluster with Node.js, but feel free to use any programming language or tool you want to connect to SingleStore. The awesome thing about SingleStore is that it’s MySQL wire compatible, so you can use any MySQL driver to connect to your cluster.
For this project, we will be using node-mysql2. The MySQL2 project is a continuation of MySQL-Native. We’re using it because it makes working with MySQL-compatible databases and queries easy. If you want a complete tutorial on how to connect4 to your application using Node.js, be sure to check out my post on how to use SingleStore and Node.js.
To install mysql2 and let app.js use import, run the following in your terminal:
npm install --save mysql2
npm pkg set type=module
Now, let’s edit our main file.
//app.js
import mysql from 'mysql2/promise';
// TODO: adjust these connection details to match your SingleStore deployment:
const HOST = 'PASTE YOUR SINGLESTORE ADMIN ENDPOINT HERE';
const USER = 'admin';
const PASSWORD = 'PASTE YOUR PASSWORD HERE';
const DATABASE = 'app';
async function singleStoreKeyValueStore(numberOfRequests) {
let conn;
try {
// Use a pool to improve performance and concurrency
conn = await mysql.createPool({
host: HOST,
user: USER,
password: PASSWORD,
database: DATABASE,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
});
// code will be added here.
await singleStoreInsertData({ conn, numberOfRequests });
await singleStoreReadData({ conn, numberOfRequests });
} catch (err) {
console.error(err);
} finally {
if (conn) {
await conn.end();
}
}
}
singleStoreKeyValueStore(1000);
The following code snippets allow us to run a simple performance test on your SingleStore key-value store. It shows you how to insert any number of keys into your database:
function intToString(num) {
return num.toString();
}
async function create({ conn, data }) {
try {
const [results] = await conn.query('INSERT INTO data VALUES ( ?, ? )', [data.id, data.value]);
return results.insertId;
} catch (err) {}
}
async function singleStoreInsertData({ conn, numberOfRequests }) {
try {
const start = Date.now();
for (let i = 0; i < numberOfRequests; i++) {
const key = intToString(i);
await create({
conn,
data: { id: key, value: `Value: ${key}` },
});
}
const end = Date.now();
console.log(`SingleStore took ${end - start}ms to insert ${numberOfRequests} rows.`);
return end - start;
} catch (err) {
console.error(err);
}
}
Now that we’ve inserted data into our key-value store, let’s go and read the data out of our database. This snippet can also be used for testing how long it takes to read any number of random data from our key-value store. This read strategy is a common access pattern for apps that typically use a key-value store, so it’s useful for running a performance test on your data.
function randomNumberBetween(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
async function readOne({ conn, id }) {
const [rows, fields] = await conn.execute('SELECT * FROM data WHERE id = ?', [id]);
return rows[0];
}
async function singleStoreReadData({ conn, numberOfRequests }) {
try {
const start = Date.now();
for (let i = 0; i < numberOfRequests; i++) {
const randKey = intToString(randomNumberBetween(0, numberOfRequests));
const result = await readOne({
conn,
id: randKey,
});
}
const end = Date.now();
console.log(`SingleStore took ${end - start}ms to read ${numberOfRequests} rows.`);
return end - start;
} catch (err) {
console.error(err);
}
}
Let’s recap what we’ve learned today. In its simplest form, a key-value store uses a simple key-value method to store data, like a hashtable or dictionary. They are often used for their simplicity, scalability, and performance. By using SingleStore as a key-value store on your application, you can save money by only using a single database, without learning new skills since SingleStore speaks SQL. You can simplify your application’s architecture and you can use SingleStore as a key-value store with incredible performance that is in the same league as competitors - including super-fast data ingest and sub-second latencies.
Now that we’ve discussed using SingleStore as a key-value store, what’s next? If you want to play around with SingleStore in the cloud, the best way is to spin up a database cluster on SingleStore and try it out for yourself! You can sign up for free SingleStoreDB Clouds here.
Personally, I would also recommend that you check out the SingleStore Developers site. There are a ton of great developer projects and demos for many languages, frameworks, and integrations.
Works on My Machine
Get the next post by email. My newsletter covers homelab builds, AI tools and open source, and it goes out when I have something to say.
It’s free, and you can unsubscribe anytime. You’ll get an email to confirm first.
If you liked this post, consider sharing it on Hacker News, Bluesky, LinkedIn, or X.

Joe Karlsson
Technology is better when it's weird and it's yours. Everything I write about runs in my house.
More about me →