Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


What are data types in Redis?

Wale Moshood

Remote Directory Server, otherwise referred to as Redis, is an open-source, fast, key-value, and in-memory data store used as a database. Redis provides a large variety of data structures for the user’s needs.

The most important feature of Redis is its low latency and high rate of data access, which is achieved because all Redis data resides in memory.

Data types in Redis

Redis data types
Redis data types

There are five data types in Redis.

1. Strings

Strings are the most basic kind of Redis data type that is considered to be binary-safe. This means they can contain any kind of data, e.g., an image in JPEG format. They can hold values up to a maximum of 512MB in size.

You can use strings in Redis for the following:

  • To append strings with the APPEND command.
  • Use of the SETRANGE and GETRANGE commands to randomly access vectors.
  • Encode a lot of data in little space.
  • Use the INCR family commands as an atomic counter.

2. Lists

These are simply lists of strings that are sorted by insertion order. New elements can be added to a Redis list by pushing the new elements on the left or right of the list.

You can use the LPUSH and RPUSH commands, respectively. A new list is created if any of these operations are carried out against an empty key.

Lists in Redis can be used for the following:

  • To create a list that can never exceed a given number of elements by using LPUSH together with LTRIM.
  • Lists support several commands, including blocking commands such as BLPOP.

3. Sets

These are unordered collections of Strings. Sets can be used to add, remove, and test for the existence of members, regardless of the number of elements contained inside the Set.

Adding the same element multiple times in sets will result in a set having a single copy of the element. Redis sets have the desirable property of not allowing members to be repeated.

You can use Redis sets for the following:

  • Support several server-side commands to compute sets and carry out operations such as intersections, unions, and differences of sets.
  • Extract elements at random with the SPOP command.

4. Hashes

Hashes are the most important data type to represent objects. They are maps between string values and fields.

A hash with a few fields is stored in a way that takes very little space, so millions of objects can be stored in a small Redis instance.

5. Sorted sets

Sorted sets are similar to sets in Redis. They are a non-repeating collection of strings. Individual members of a sorted set are given a score that allows the sorted set to be ordered from the smallest to the greatest score. Even though sorted sets are unique, scores may be repeated.

Sorted sets are the most advanced Redis data types. You can add, remove, and update elements in a time proportional to the logarithm of the number of elements.



View all Courses

Keep Exploring