In this topic, we'll take a look at the key-value storages. They are similar to a dictionary in Python or a map in Java but are managed with the database management system. Key-value storages are used for storing unstructured data with a short life span such as cache.
Working with key-value storage
Key-value storage is an associative array, meaning it creates a relation between the two parts of data: key and value.
A key should be a unique identifier, and a value could be any data structure. For example, if we decide to store files, we can choose a file path or URI as the key, and the value could be a blob of this file.
However, unlike the relational database, we're not restricted by the format of data neither for a key nor for a value. It means that we can store something completely different in the same place.
Usually, key-value storages support the following operations:
-
getting value for a certain key
-
putting (adding or updating) value for a certain key
-
deleting the value for a certain key.
It's important to remember that it's hard (and for some storages even impossible) to filter objects based on value.
Benefits of key-value storage
For many key-value storages, these operations are happening in RAM, so they are almost instantaneous. Since there is no strict structure in the database, incoming data is not restricted to anything. It might be very helpful when the data is not structured or changes a lot.
Usage of key-value storage
Key-value storage can be used as a solution for the in-memory cache. It reduces the number of requests and the load on the server while also improving the user experience.
Web applications often store user session data in key-value storages. For example, a webshop could contain a shopping basket or other parameters for customer personalization.
The other important use of key-value storages includes a message queue. It allows different parts of the system to communicate in an asynchronous way.
Key-value storages work well with relational databases. If an application often uses the results of some query, but the data itself doesn't change a lot, it makes sense to store the result in a key-value storage and read it from there. So key-value storages may work not only as traditional cache but also as cache for databases. It is especially important when the query itself takes a long time.
Example of usage
Let's have a look at how can we operate with Redis. Different key-value storages may have slightly different syntax but the generic approach will be the same.
Redis itself runs on a Redis-server, and we can access it, for example, with a redis-cli tool. By default, it starts on 6379 port, but the port can be specified in a start string. Let's store a value "somevalue" with a key "mykey":
To use a Redis queue we can use commands rpush/lpush and rpop/lpop that are pushing or getting the element from the end or the beginning of the queue accordingly:
We can also store a key with a value that will expire with an ex parameter. In this example the time is provided in seconds:
As you can see, the Redis interface is quite friendly and intuitive.
If you want to experiment with Redis you can use a web interface.
Conclusion
Key-value storage is a great solution for the following cases:
-
application cache;
-
cache from a relational database;
-
message queues;
-
all other cases when data has to be accessible with minimal lag.
In general, key-value storages might be a big help for an application that needs to handle the load.