Redis
data structures
memory management
NoSQL
in-memory database

What are the underlying data structures used for Redis?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Redis is a highly performant, open-source, in-memory data structure store widely used for caching, real-time analytics, message brokering, and more. The secret behind its robustness and flexibility lies in its underlying data structures. This article explores the core data structures Redis employs and how they contribute to its efficiency.

Data Structures in Redis

Redis supports complex data types as its primary data structures:

  1. Strings
  2. Lists
  3. Sets
  4. Sorted Sets
  5. Hashes
  6. Bitmaps
  7. HyperLogLogs
  8. Streams

1. Strings

Strings in Redis are the simplest kind of values, but they're more than just character sequences. They can store any kind of data, such as text or serialized objects, up to 512 MB in size. Technically, Redis uses a dynamic string representation known as SDS (Simple Dynamic Strings).

Example:

redis
SET key "Hello, Redis!"
GET key

2. Lists

Redis lists are collections of strings sorted by the order of insertion, analogous to linked lists. Internally, Redis uses a doubly linked list or a quicklist, combining arrays and linked lists depending on the length and settings.

Example:

redis
LPUSH mylist "World!"
RPUSH mylist "Hello"
LRANGE mylist 0 -1

3. Sets

Sets are unordered collections of unique strings. Redis sets use a hashtable when it's small or a specialized data structure—a dictionary—when the set grows larger.

Example:

redis
SADD myset "foo"
SADD myset "bar"
SMEMBERS myset

4. Sorted Sets

Sorted sets are similar to sets but with an added score, allowing elements to be sorted. Internally, Redis implements sorted sets with a combination of a skip list and a hash table, ensuring high efficiency.

Example:

redis
ZADD myzset 1 "one"
ZADD myzset 2 "two"
ZRANGE myzset 0 -1 WITHSCORES

5. Hashes

Hashes in Redis are collections of key-value pairs. Redis is optimized to handle small hash objects, internally using a hashtable or a ziplist when the hash is small.

Example:

redis
HSET user:1000 name "John Doe"
HSET user:1000 age 30
HGETALL user:1000

6. Bitmaps

Bitmaps are designed for bitwise operations. Redis treats strings as arrays of bits, offering manipulation on the binary level.

Example:

redis
SETBIT mykey 7 1
GETBIT mykey 7

7. HyperLogLogs

HyperLogLogs are probabilistic data structures used to count unique items in a large dataset with low memory usage. They're more suited for use cases where approximate cardinality suffices.

Example:

redis
PFADD myhyperloglog "a" "b" "c"
PFCOUNT myhyperloglog

8. Streams

Streams are log data structures that support complex operations like storing multiple fields within the same entry as a stream item. Streams provide a powerful abstraction for handling time series data, complex message brokering, and more.

Example:

redis
XADD mystream * field1 value1
XRANGE mystream - +

Summary Table

Data StructureDescriptionInternal Structure
StringsBinary safe strings up to 512 MBSimple Dynamic String (SDS)
ListsOrdered collection of stringsDoubly linked list or quicklist (mix of linked list and array)
SetsUnordered collections of unique stringsHashtable or dictionary (depending on size)
Sorted SetsSets sorted by a scoreCombination of skip list and hash table
HashesField-value pairsHashtable or ziplist (depending on size)
BitmapsStrings interpreted as bitsBit-wise operations on strings
HyperLogLogsProbabilistic structures for counting unique itemsSparse and dense representation to minimize memory usage
StreamsAppend-only log of entries with multiple fieldsListpack for storing list of simple elements, while each entry is a tuple stored in a similar fashion, akin to a hash map

Conclusion

Redis efficiently utilizes these data structures to deliver remarkable performance. Each data structure serves specific use cases, and their internal mechanics guarantee that Redis remains flexible yet optimized for speed. Understanding these structures is essential for leveraging Redis's full potential in application development.


Course illustration
Course illustration

All Rights Reserved.