Skip to main content

Part-1: Key-value store

Introductionโ€‹

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 seven parts tutorial, you'll learn how to develop with the key-value store in codehooks.io, including practical code examples and common use cases.

Tutorial overview

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โ€‹

Codehooks.io 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.

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

// REST route for serverless function
app.post('/foo', async (req, res) => {
// connect to the key-value database
const key_val_store = await Datastore.open();
// 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();

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

Deploy and test the serverless app from the project directory
$ coho deploy 
Project: keyval-xxxx Space: dev
Deployed Codehook successfully! ๐Ÿ™Œ


$ curl -X POST \
'https://keyval-xxxx.api.codehooks.io/dev/foo' \
--header 'Accept: */*' \
--header 'x-api-key: 07ae0bba-aaaa-437c-bbbbb-dfd0b991fa0b'

Thank's for setting and getting bar

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 next Key' => 'Some value'
'My second Key' => 'Some value'

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'  => 'https://codehooks.io/docs/'
'my_json_key' => '{"foo": "bar"}'
'my_num_key' => '42'
'my_buf_key' => '7b 22 66 6f 6f 22 3a 20 22 62 61 72 22 7d'
'my_user_key' => '4f90d13a42'

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 codehooks.io.

Continue to Part-2 to learn how to master the basic operations of the key-value store.