Table of contents
1.
Introduction 
2.
Redis data types
2.1.
Strings
2.2.
Lists
2.3.
Sets
2.4.
Hashes
2.5.
Sorted Sets
2.6.
Other data types
3.
Frequently Asked Questions
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Introduction to Data Types in Redis

Author GAZAL ARORA
0 upvote

Introduction 

Redis is an in-memory data structure used as a database, cache, message broker, and streaming engine. It offers built-in replication, Lua scripting, LRU eviction, transactions, several levels of on-disk persistence, Redis Sentinel for high availability, and Redis Cluster for automatic partitioning. You can use Redis from most programming languages.

Redis is open-source (BSD licence). Redis allows data structures like strings, hashes, lists, sets, sorted sets with range searches, bitmaps, hyperloglogs, geographic indexes, and streams. 

Redis data types

Let's take an overview of the data types supported by Redis.

Strings

The most basic type of Redis data is a string. A Redis string can hold any data, such as a JPEG image or a serial Ruby object because Redis Strings are binary-safe.

The max length of a String is 512 Megabytes.

In Redis, you can do a variety of exciting things with strings, such as:

  • Use the INCR family of commands to use Strings as atomic counters: INCRDECRINCRBY.
  • With the APPEND command, you can add to strings.
  • With GETRANGE and SETRANGEyou can use Strings as random access vectors.
  • Create a Redis-backed Bloom Filter using GETBIT and SETBIT to encode a large amount of data in a small amount of space.

 

Learn more about some available string commands.

Lists

Redis Lists are simply strings sorted in order of insertion. It is possible to add new entries to a Redis List by pushing them to the list's head (on the left) or tail (on the right).

The max length of a list can be 2^32 - 1 elements. 

The ability for constant time insertion and deletion of elements near the head and tail, even with several millions of inserted items, is one of Redis Lists' primary features in terms of time complexity. 

Accessing items is fast near the list's extremes, but it takes longer to access the middle of a large list because it is an O(N) operation.

In Redis, you can do a variety of exciting things with lists, such as:

  • Create a timeline in a social network using LPUSH to add new components to the user's timeline and LRANGE to retrieve a few of the most recently entered items.
  • You can use LPUSH and LTRIM together to make a list that never goes beyond a certain number of elements and only remembers the most recent N.
  • For example, the well-known Resque Ruby framework for creating background jobs uses lists as a message-passing primitive.
  • Lists support a variety of instructions, including blocking commands like BLPOP.


Linked Lists are used to implement Redis lists. This implies that even if a list contains millions of members, the action of adding a new element to the head or tail of the list takes the same amount of time. 

Learn more about some available list commands.

Sets

Sets in Redis are the unordered collection of Strings. You can add, remove, and test for the presence of an element in the set in O(1).

Redis Sets don't allow duplicate entries. Adding the same element to a set several times will result in a single copy of that element in the set. 

Redis Sets provides a range of server-side commands for computing sets from existing sets, allowing you to quickly perform unions, intersections, and differences of sets.

A set can have a maximum of 232 - 1 members.

In Redis, you can do a variety of exciting things with sets, such as:

  • Redis Sets keep track of unique items. Do you want to know how many different IP addresses have visited a particular blog post? Simply use SADD every time a page view is processed. You're confident that no duplicate IPs will be entered.
  • You can use the SPOP or SRANDMEMBER commands to extract elements at random from the sets.

 

Learn more about some available set commands.

Hashes

Redis Hashes are maps between the string fields and the string values, making them an ideal data type for representing objects (for example, a User with multiple fields such as name, surname, age, etc).

  • A hash with a few fields is stored in a space-efficient manner, allowing millions of items to be saved in a small Redis instance.
  • HGET retrieves a single field, whereas HMSET sets multiple fields of the hash. HMGET is similar to HGET, but it returns an array of values instead of a single value.
  • Commands like HINCRBY can perform operations on individual fields.

 

Learn more about some available hash commands.

Sorted Sets

Redis Sorted Sets are non-repeating collections of Strings, similar to Redis Sets. The difference is that each member of a Sorted Set is assigned a score, which is used to keep the Sorted Set in order, from lowest to highest score. Scores can be duplicated, but the members are unique.

Sorted Sets allow you to quickly add, remove, or update pieces (in a time proportional to the logarithm of elements). Because elements are stored in sequence and are not reordered, you can quickly obtain ranges by score or rank (position). Because accessing the middle of a Sorted Set is quick, you can use Sorted Sets as a clever storage solution.

Learn more about some available sorted set commands.

Other data types

  • Bitmaps and HyperLogLogs, which are data types built on the String, are also supported by Redis.
  • A Redis stream is a data structure that works similarly to an append-only log. Streams are excellent for capturing events in their original order. For more information and examples, see the Redis streams documentation.

 

  • Geospatial indexes are provided by Redis and are useful for discovering locations within a certain geographic radius. You can use the GEOADD command to add locations to a geospatial index. 
  •  The GEORADIUS command is used to find places within a specified radius.

Frequently Asked Questions

1. What is Redis?
Redis is defined as an in-memory data structure used as a database, cache, message broker, and streaming engine. It is open-source (BSD licence). Data types like Strings, hashes, lists, sets, sorted sets with range searches, bitmaps, hyperloglogs, geographic indexes, and streams are all available in Redis.

2. How is data stored in Redis?
Redis is a non-relational in-memory key-value store. It stores data using keys and values – think of it as a huge dictionary that stores information using words and their definitions.

Conclusion

In this article, we learned about the data structures available in Redis. We learned that Redis is open-source (BSD licence). It allows data structures like strings, hashes, lists, sets, sorted sets with range searches, bitmaps, hyperloglogs, geographic indexes, and streams. 

In this article, we looked at:

  • Strings
  • Lists
  • Sets
  • Hashes
  • Sorted sets
  • Other data types

To learn more about Redix, click here.

To study more about data types, refer to Abstract Data Types in C++.

Happy Coding!

You can use Coding Ninjas Studio to practice various DSA questions asked in the interviews. It will help you in mastering effective coding techniques, and you will also get interview experiences with people working in big companies.

Live masterclass