How is Redis implemented

NoSQL Part 2: Key-Value Stores using Redis as an example

Easy and fast

In the first part of our NoSQL blog article series, we showed that the WWW has created new problem areas in the area of ​​data storage and data retrieval, which cannot be optimally solved by relational database management systems (DBMS). The term NoSQL covers all those DBMS which solve exactly one type of problem with a better cost / benefit ratio than the classic relational DBMS. The area of ​​the NoSQL DBMS can be divided into various categories. In the following we want to introduce one of the simplest (from an API point of view), but also at the same time most flexible NoSQL database types: The key-value store.

Key-value stores belong to the category of aggregate-oriented databases. According to Martin Fowler, aggregates are “[...] combinations of entities and objects of value and their associations with one another to form a common transactional unit. Aggregates define exactly one entity as the only access to the entire aggregate. " An example aggregate would be an order (as "aggregation root") with all of its order items, for which access to the items would only be permitted via the order. In key-value stores, a unique key - usually a simple string - is assigned a value. This value can be a primitive data type (such as a string, integer, etc.) or, for example, a serialized object that can represent an aggregate. Queries and delete operations can only be carried out using the unique key of the value. Editing is not possible. In the simplest case, this results in the following three operations which describe the interface of a key-value store:

  • put (key, value)
  • get (key)
  • remove (key)

use cases

Key-value stores are used wherever the data model only allows access and storage according to the key, but especially where performance plays a critical role with a large number of objects. High read / write rates are achieved through simple indexing (only according to the primary key) and simple, mostly linear, scaling. Application examples where speed is critical are session data, shopping baskets in e-commerce applications or even simple caching scenarios. Key-value stores, on the other hand, are not suitable for application scenarios in which complex queries should be possible, e.g. across aggregates or with complex data models. In these cases it is worth taking a look at the rest of the NoSQL types that we will describe in upcoming blog articles.

Example implementations for key value stores are Redis, Riak, Memcached or the software-as-a-service Amazon Dynamo DB. They differ mainly in the following questions:

  • Are other data structures supported beyond simple key values ​​(e.g. lists or sets)?
  • Is the data kept volatile, e.g. in RAM, or persistently on the hard drive?
  • Is scaling done via sharding, master-slave or master-master?
  • Does the implementation offer strict consistency or only optional?

Riak, for example, is one of the more complex persistent key-value stores in which the desired consistency can be configured per query. In addition to keys and values, buckets are also available in the data model, as well as scaling across multiple nodes without a dedicated master. In comparison, Memcached is quite simple: it holds the key-value pairs in RAM and scales using sharding.

Redis as a key-value store

In the following, we want to use Redis to take a closer look at the functionality of a very widespread key-value store that has also been used in a large number of projects since two days.

Redis is an in-memory key-value store implemented in C. In addition to key-value pairs, Redis offers extended data structures such as lists, hashes, sets and bitmaps. By means of configuration, it is possible to regularly save the volatile data stored in the RAM on the hard drive in order to prevent data loss. Redis can be used for caching as well as in cases where permanent persistence of data is required. Like most key value stores, Redis has a very high read / write rate. In addition, Redis comes with its own publish / subscribe implementation, which can be used to implement messaging systems in distributed architectures.

In the following example we use the Ruby gem redis-rb to introduce some functionality of Redis:

These are just some of the capabilities of Redis. Detailed documentation can be found at http://redis.io.

At Zweiag, key value stores are regularly used in projects. Many of the above implementations have a high level of maturity and are continuously being developed by the open source community. We appreciate the ease with which developers can master challenges that fit the key-value model, as well as the flexibility that is available to be able to react to rapidly changing external requirements. For our customers, key value stores offer the optimal cost-benefit ratio when it comes to the performance and scaling of certain data in IT projects.