Skip to main content

Redis

Keyspace

Redis is an in-memory database that stores data in a key-value data model. thath supports different types of data structures as values.

The key is a binary-safe String, meaning that any binary sequence can be used as a key, including string, empty string, JPEG image, etc, but with a max size of 512 MB and one should always consider creating shorter keys.

Data Types

String

Redis String data type stores text, serialized objects, and binary arrays as a sequence of bytes with a limit of 512 MB.

As Redis key is a string as well, using String type for value is essentially creating a mapping between two strings.

Even though the value is of String type, it can actually be used a counter by using commands such as INCR, INCRBY, DECR and DECRBY, which coverts the value to an integer and sets the obtained value as the new value after performing corresponding actions. Note that above commands are atomic.

Most commands related to String type have a performance of O(1), while commands such as GETRANGE, SETRANGE are O(n).

Hash

Hash data type stores record as as collections of field-value pairs. It is often used to represend objects, with fields representing properites of an object.

Every Hash can store up to 4,294,967,295 (2^32 - 1) field-value pairs. In practce, your hashes are limited only by the overall memory.

The ability to specify an expiration time or a time-to-live (TTL) value for individual hash fields is added since 7.4. Common use cases are event tracking, active session tracking, fraud detection etc.

Most commands related to Hash type have a performance of O(1), while commands such as HKEYS, HVALS, HGETALL, and most of the expiration-related commands, are O(n), are O(n).

List

List datatype is a linked lists of string values. It is frequently used to implement stacks/queues and build queue management for background worker systems. The max length of a list datatype is 2^32 - 1 (4,294,967,295) elements.

Because this datatype is implemented with linked list, adding and removing elements on the head or tail has a performance of constant time, but not that performant when accessing elements in the middle of the collections.

For below usecases, other datatypes might be a better option:

  • When fast access to the middle of a large collection of elements is important, sorted set is a better option.
  • When storing and processing an indeterminate series of events is needed, stream is a better alternative.

Set

Set is an unordered collection of unique strings. Common usescase are:

  • Tracking unique items (e.g., track all unique IP addresses accessing a given blog post).
  • Represent relations (e.g., the set of all users with a given role).
  • Perform common set operations such as intersection, unions, and differences.

The max size of a set is 2^32 - 1 (4,294,967,295) members, with most operations have a performance of O(1). However, for large sets with hundreds of thousands of members or more, running the SMEMBERS command is O(n) and returns the entire set in a single response. As an alternative, consider the SSCAN, which lets you retrieve all members of a set iteratively.

While we can use SADD command to add elements to a set, the returned element order is not guaranteed when retrieving elements with SMEMBERS command.

Set membership checks on large datasets (or on streaming data) can use a lot of memory, consider a Bloom filter or Cuckoo filter as an alternative if memory usage is a concern and don't need perfect precision,

Sorted set

A Redis sorted set is a collection of unique strings (members) ordered by an associated score of floating point value. When more than one string has the same score, the strings are ordered lexicographically. Some use cases for sorted sets include leaderboards and rate limiter.

Given that Sorted set is a combination of Hash and Set with a sorted nature, we can perform actions similart to those with additional features such as extracting values with a given index range(e.g ZRANGE), score range (e.g ZRANGEBYSCORE) and lexicographical range(e.g ZRANGEBYLEX, ZREVRANGEBYLEX, and ZLEXCOUNT).

Most sorted set operations are O(log(n)), where n is the number of members. ZRANGE command, for instance, has a time complexity of O(log(n) + m) with large returns values (e.g., in the tens of thousands or more), where m is the number of results returned.

Other datatypes

Aside from most common useddatatypes mentioned above, Redis also provide datatypes such as:

  • Stream
  • Bitmap
  • Bitfield
  • Geospatial
  • JSON (Enterprise only)
  • Probabilistic data types (Enterprise only)
  • Time series (Enterprise only)

Transactions

Even though Redis support the concept of transactions, its concept is quite different to transactions in RDBMS.

Redis doesn't support rollbacks since supporting rollbacks would have a significant impact on the simplicity and performance, nor does it prevent race conditions. The current available workaround is using an [optimistic locking mechnaism using `WATCH` command](https://redis.io/docs/latest/develop/interact/transactions/#optimistic-locking-using-check-and-set).

References