Redis Data Types – Strings, Hashes, Lists, Sets

Before we talk about Redis Data Types, let us briefly explain what is Redis? Redis is a data structure store.  It is a BSD licensed, open-source, in-memory store which has various uses including a database, a cache, and a message broker.

Strings, hashes, lists, sets, sets sorted with range queries, bitmaps, hyperloglogs and geospatial indexes which have radius queries are some examples of the data structure that is supported.

Redis Data Types 

Following are some of the Redis data types:

String Data Type

These are a Redis value of the most basic type.  Because they are binary safe, a Redis string can hold any type of data, including a JPEG image or a Ruby object that is serialised.  The maximum length of a string is 512 Megabytes.  It has a number of uses including:

  • Strings can be used as atomic counters, using commands including INCR, DECR, INCRBY – all from the INCR family.
  • You can use the APPEND command to append strings.
  • Strings can be used with GETRANGE and SETRANGE as random access vectors.
  • A lot of data can be encoded in a small space, or use GETBIT and SETBIT to create a Redised based Bloom Filter.

Lists Data Type

Lists in Redis data types are lists of strings by order of insertion.  You can add elements to a Redis List by pushing new ones on the head (left) of on the tail (right) of the list.  LPUSH and RPUSH are commands using to insert to the head and the tail.  To create a new list perform one of these two operations against an empty key.

The key will be removed from the keyspace if a list operation empties the list.  The commands behave as they are named.

An example:

LPUSH   my list x # “x”

LPUSH   my list y # “y”, “x”

RPUSH  my list z # “y”, “x”, “z”

The maximum length is 2³² -1 elements

The most important features of Redis Lists are support for constant time insertion and the deletion of elements that are near the head and tail.  When close to the extremes it is very fast to access elements, but can be slow if you are trying to access elements in the middle of a large list.  You can also:

  • Model a timeline in a social network, by using LPUSH, to add, and LRANGE to access recent
  • Use LPUSH and LTRIM and create a list that has a constant number of elements.
  • Use as a message-passing primitive.
  • Support a number of commands, one of which is BLPOP

Sets Data Type

In Redis data types, sets are a collection of strings that are not ordered.  You are able to add, remove, and test the existence of members in 0(1).  This can be completed in constant time, no matter how many elements are contained in the set.

One important property of Redis Sets is they don’t allow repeated members.  This means there is no need to check.

A number of server-side commands allow you to do unions, intersections, and differences of sets in a short time.

The maximum number of members allowed is 2³² – 1.  Other uses include:

  • To knows all the unique IP addresses that are visiting a particular blog. Use the command SAAD each time you process a page view.  You can check there are no repeated IP addresses.
  • Representing relations with Redis you can use a set to represent every tag and create a tagging system. This uses the SAAD command.  Use SINTER if you want to find the IDs of all the objects which have three different tags simultaneously.
  • By using SPOP or SRANDMEMBER command you can use Sets to extract elements randomly.

Hashes Data Types

Redis Hashes are a very useful data type for representing objects by creating maps between string fields and string values.

You can store many objects in a small Redis instance because a hash with only a few fields can be stored in this manner.

One hash can hold up to 2³² – 1 field values pairs.

Sorted Sets 

Redis Sorted Sets are very like Redis Sets.  The difference is that a score is associated with every member, which orders them from the smallest to the greatest score.  Scores may be repeated, but members remain unique.

Sorted Sets allow you to add, remove or update elements rapidly.  Because elements are taken in order, and not ordered later, you are able to obtain ranges by score or rank them quickly.  It is also fast to access the middle of a Sorted Set.  Sorted Sets allow you to do many tasks efficiently, which is difficult in many other types of databases.  You can also:

  • Use a leader board with online games, and each time there is a new score it can be updated using the command ZADD. ZRANGE will give the top users, and ZRANK and ZRANGE together will show users with similar scores.
  • Index data that is stored in Redis can be sorted in order. ZRANGEBYSCORES command will quickly give you all users with a given interval of ages.

Sorted Sets are one of the advanced data types in Redis.

Bitmaps and Hyperloglogs

Both are data types with their own semantics but are actually data types based on the String base type.

There are additional features including replication (built-in), Lua scripting, LRU eviction, transaction with various levels of on-disk persistence.  This provides high availability via Redis Sentinel and partitioning is automatic with Redis Cluster.

Redis uses ANSI C and will work in most systems that use POSIX, such as Linux.

Redis was developed and tested using Linux and OS X operating systems.

It is highly recommended to use Linux for deployment.  The best support is available through Linux.

Leave a Comment