Skip to main content

Command Palette

Search for a command to run...

Redis Data Structures

Explore Redis's core data structures.

Updated
5 min read
Redis Data Structures

In this article, you can find the most useful Redis data structures with brief details for each.

To see all data structures, including extension data types, visit the link below:
Redis Data Types Documentation

The link below provides the time complexity for all Redis commands, which is very helpful in selecting the right data structure for your use case:
Redis Command Time Complexity Cheat Sheet

Strings

Redis strings store sequences of bytes, including text, serialized objects, and binary arrays.

Example

> SET bike:1 Deimos
OK
> GET bike:1
"Deimos"

Usage

  • Storing simple key-value pairs

  • Caching responses

  • Counting (using INCR, DECR)

Important Notes

  • Most string operations are O(1)

  • Be careful with the SUBSTR, GETRANGE, and SETRANGE commands, which can be O(n)

  • Redis string can be a maximum of 512 MB (Danger)

https://redis.io/docs/latest/develop/data-types/strings

https://redis.io/docs/latest/commands/?group=string

Lists

Redis lists are ordered collections of strings, implemented as linked lists.

Example

> RPUSH todo:123 "Buy milk"
> RPUSH todo:123 "Walk the dog"
> LPUSH todo:123 "Check emails"
> LRANGE todo:123 0 -1
1) "Check emails"
2) "Buy milk"
3) "Walk the dog"

Usage

  • Implementing queues (FIFO, LIFO)

  • Twitter social network takes the latest tweets posted by users into Redis lists - reference

Important Notes

  • Operations that access its head or tail are O(1)

  • Commands that manipulate elements within a list are usually O(n) (Danger)

    • Like LINDEX, LINSERT, and LSET
  • Redis streams is an alternative to lists when you need to store and process an indeterminate series of events

https://redis.io/docs/latest/develop/data-types/lists

https://redis.io/docs/latest/commands/?group=list

Sets

Redis Set is an unordered collection of unique strings.

Example

> SADD users:online Alice
> SADD users:online Bob
> SADD users:online Alice
> SMEMBERS users:online
1) "Alice"
2) "Bob"

Usage

  • Track 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.

Important Notes

  • Most set operations, including adding, removing, and checking whether an item is a set member, are O(1)

  • SMEMBERS command is O(n) and returns the entire set in a single response

https://redis.io/docs/latest/develop/data-types/sets

https://redis.io/docs/latest/commands/?group=set

Hashes

Redis hashes are key-value maps, useful for storing objects.

Example

> HSET user:100 name "Alice" age "30"
> HGET user:100 name
"Alice"
> HGETALL user:100
1) "name"
2) "Alice"
3) "age"
4) "30"

Usage

  • Caching database rows and …

Important Notes

  • New in Redis Community Edition 7.4 is the ability to specify an expiration time or a time-to-live (TTL) value for individual hash fields.

  • Most Redis hash commands are O(1).

  • A few commands and most of the expiration-related commands are O(n) (Danger)

    • Like HKEYS, HVALS, HGETALL
  • The number of fields in a hash is limited only by the total memory available on the Redis server.

https://redis.io/docs/latest/develop/data-types/hashes

https://redis.io/docs/latest/commands/?group=hash

Sorted Sets (Zsets)

Zsets store unique strings with an associated score for ordering.

Example

> ZADD leaderboard 100 "Alice"
> ZADD leaderboard 200 "Bob"
> ZADD leaderboard 150 "Charlie"
> ZRANGE leaderboard 0 -1 WITHSCORES
1) "Alice"
2) "100"
3) "Charlie"
4) "150"
5) "Bob"
6) "200"

Usage

  • Leaderboards

  • Rate limiters

Important Notes

  • Most sorted set operations are O(log(n))

  • ZRANGE command's time complexity is O(log(n) + m), where m is the number of results returned.

  • You can think of sorted sets as a mix between a Set and a Hash. Like sets, sorted sets are composed of unique, non-repeating string elements, so in some sense a sorted set is a set as well.

https://redis.io/docs/latest/develop/data-types/sorted-sets

https://redis.io/docs/latest/commands/?group=sorted-set

Streams

Streams are an append-only log-like data structure.

Example

> XADD mystream * sensor-id 1 temperature 22.5
> XREAD COUNT 1 STREAMS mystream 0
1) "mystream"
2) 1) 1) "timestamp"
      2) "sensor-id"
      3) "1"
      4) "temperature"
      5) "22.5"

Usage

  • Event sourcing (e.g., tracking user actions, clicks, etc.)

  • Sensor monitoring (e.g., readings from devices in the field)

  • Notifications (e.g., storing a record of each user's notifications in a separate stream)

Important Notes

  • Adding an entry to a stream is O(1)

  • Accessing any single entry is O(n), where n is the length of the ID.

  • Consumer groups allow fan-out processing and acknowledgments

  • Fan-out with Consumer Groups: Multiple consumers in a group distribute messages (load balancing), while independent consumers (outside a group) receive all messages.

https://redis.io/docs/latest/develop/data-types/streams

https://redis.io/docs/latest/commands/?group=stream

Geospatial indexes

Store and query geographic locations.

Example

> GEOADD locations 2.3522 48.8566 "Paris"
> GEOADD locations -74.0060 40.7128 "New York"
> GEODIST locations Paris "New York" km
"5837.2690"

Usage

  • Location-based services

  • Nearby search

Important Notes

  • O(log n) complexity for GEOADD, GEODIST, GEORADIUS

https://redis.io/docs/latest/develop/data-types/geospatial

https://redis.io/docs/latest/commands/?group=geo

Bitmaps

Bit-level operations for efficient storage and tracking.

Example

> SETBIT visits:20240403 0 1
> SETBIT visits:20240403 1 1
> GETBIT visits:20240403 0
1

Usage

  • Efficient set representations for cases where the members of a set correspond to the integers 0-N.

  • Object permissions, where each bit represents a particular permission, similar to the way that file systems store permissions.

Important Notes

  • SETBIT and GETBIT are O(1)

  • BITOP is O(n)

https://redis.io/docs/latest/develop/data-types/bitmaps

https://redis.io/docs/latest/commands/?group=bitmap

JSON

Redis JSON provides structured, hierarchical arrays and key-value objects that match the popular JSON text file format.

Example

> JSON.SET bike:1 $ '{"brand":"Deimos","model":"X1","colors":["red","black"]}'
OK
> JSON.GET bike:1
"{\"brand\":\"Deimos\",\"model\":\"X1\",\"colors\":[\"red\",\"black\"]}"
> JSON.GET bike:1 $.brand
"[\"Deimos\"]"

Usage

  • Storing complex structured data (objects, arrays)

  • Querying specific fields or nested values with JSONPath ($.field)

  • Caching structured API responses

  • Managing hierarchical data like configurations, product catalogs, or user profiles

Important Notes

  • JSON operations are generally efficient, but performance depends on document size and depth

  • JSON supports numbers, strings, booleans, nulls, arrays, and objects (full JSON spec)

  • Memory usage can grow quickly if storing large documents (Danger)

https://redis.io/docs/latest/develop/data-types/json

Redis

Part 2 of 3

Learn Redis step-by-step, from core concepts and deployment modes to real-world Java integration using Redisson.

Up next

Deployment Modes of Redis

Start by understanding Redis's different deployment modes.

Redis Data Structures