DEV Community

Cover image for Tutorial part-1: Key-value store
Jbee -
Jbee -

Posted on • Updated on

Tutorial part-1: Key-value store


A key-value store is a type of non-relational database that uses a simple key-value pair to store data. In this database, data is stored as a collection of unique keys, each of which is associated with a specific value. Key-value stores are both simple to use and offer fast access to data, making them well-suited for use in distributed systems and other high-performance environments. One of the most popular and well known key-value databases is Redis.

In this 7 parts tutorial, you'll learn how to develop with the key-value store in, including practical code examples and common use cases.

This tutorial is also available at

Tutorial overview

Upcoming tutorial parts

  • Part 3 - Increment end decrement
  • Part 4 - Working with multiple values and streams
  • Part 5 - Caching data with TTL options
  • Part 6 - Multiple key spaces
  • Part 7 - Interaction with the key-val store from the CLI

Lets dive into the simple but powerful world of key-value stores and how you can learn to use them.

The basic concepts of a key-value store has a built-in key-value store (in addition to a NoSQL document database) that can be accessed from serverless JavaScript functions via the import statement. The simple code example below shows how the fundamental get and set functions operates on the key-value Datastore API.

import {app, Datastore} from 'codehooks-js'

// REST route for serverless function'/foo', async (req, res) => {
    // connect to the key-value datastore
    const key_val_store = await;
    // set a value
    const setval = await key_val_store.set('foo', 'bar'); // returns 'bar'
    console.debug('setval', setval);
    // get a value
    const getval = await key_val_store.get('foo'); // returns 'bar'
    res.status(201).end(`Thank's for setting and getting ${getval}`);

// Bind functions to the serverless cloud
export default app.init(); 
Enter fullscreen mode Exit fullscreen mode

The code example above is a complete serverless backend application that can be be deployed to the cloud service. Deployment and test with curl is shown in the example commands below.

Deploy and test the serverless app from the project directory with the following command lines.

$ coho deploy 
Project: keyval-xxxx  Space: dev
Deployed Codehook successfully! 🙌 

$ curl -X POST \
  '' \
  --header 'Accept: */*' \
  --header 'x-api-key: 07ae0bba-aaaa-437c-bbbbb-dfd0b991fa0b'

Thank's for setting and getting bar
Enter fullscreen mode Exit fullscreen mode

Keys are unique and ordered

In the key-value store, each key is a unique string.

A key can only point to one value, and will always overwrite any existing value.

The example below shows three unique keys with their associated values.

'My first Key'  => 'My first value'
'My second Key' => 'Some value'
'My first Key'  => 'Some value'
Enter fullscreen mode Exit fullscreen mode

Another important feature of the key-value store is how the keys are inserted into the key-value store. Each key string is inserted in a ascending sorted order. When retrieving a single key-value pair the sort order is not relevant. However, as we will cover in another part of this tutorial, when streaming multiple key-value pairs, the sorted stream order can be used to solve many important problems.

Values are strings

Just as with keys, values are also plain strings.
The content of the value-string can be anything, but is must be encoded to a string before inserted to the key-value store, and (if needed) decoded when retrieving. The example below shows a variation of string values and encoded string values.

'my_url_key'  => ''
'my_json_key' => '{"foo": "bar"}'
'my_buf_key'  => '7b 22 66 6f 6f 22 3a 20 22 62 61 72 22 7d'
'my_user_key' => '4f90d13a42'
Enter fullscreen mode Exit fullscreen mode

Use cases for a key-value store

Key-value stores are often used in a variety of applications where fast access to data and high scalability are important. Some common use cases for key-value stores include:

  • Caching: Key-value stores are often used as a caching layer in web applications to speed up data access. For example, a key-value store might be used to cache frequently accessed data from a slow database query to improve the performance of a web application.

  • Real-time analytics: Key-value stores can be used to store and process large amounts of data in real-time, making them well-suited for applications that require fast analysis of streaming data.

  • E-commerce: Key-value stores can be used to store product information, customer data, and other information in an online store. This allows for fast access to data, which is critical in e-commerce applications where every second counts.

  • Gaming: Key-value stores are often used in gaming applications to store game state information and other data that needs to be accessed quickly. This allows for fast and seamless gameplay.

  • IoT: Key-value stores can be used to store and manage large amounts of data generated by IoT devices. This data can be used to gain insights into the behavior of these devices and improve their performance.

This concludes our brief introduction to key-value stores in

Part-2 on learning the basic operations of your key-value store.

Top comments (0)