Databases 25 min read

Master Redis: 8 Essential Data Types and Their Real‑World Uses

This article provides a comprehensive overview of Redis' eight common data structures—including String, List, Hash, Set, Sorted Set, Bitmap, HyperLogLog, and Geospatial—detailing their internal implementations, core commands, practical code examples, and typical application scenarios for developers.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Master Redis: 8 Essential Data Types and Their Real‑World Uses

Redis Basic Data Types

String

String is a binary‑safe type that can store text, integers, floating‑point numbers, images (as base64) or serialized objects. Internally Redis uses Simple Dynamic Strings (SDS) which provide O(1) length retrieval and protect against buffer overflows.

Common commands SET key value – set a value GET key – retrieve a value SETNX key value – set only if the key does not exist MSET …, MGET … – batch set/get STRLEN key – get the length of the string INCR / DECR key – increment or decrement numeric strings EXISTS key, DEL key, EXPIRE key seconds Example usage

> SET key value
OK
> GET key
"value"
> INCR counter
1

Typical scenarios

Cache simple values such as session tokens, image URLs or serialized objects.

Implement counters, rate limiting or simple distributed locks with SETNX.

List

List is implemented as a doubly linked list (or QuickList in newer versions) and supports O(1) push/pop at both ends. It is useful for queues, stacks and ordered collections.

Common commands RPUSH key value … – append to the tail LPUSH key value … – prepend to the head LPOP / RPOP key – remove from head or tail LRANGE key start stop – slice a range (useful for pagination) LLEN key – length of the list LSET key index value – set element by index

Example usage

> RPUSH myList a b c
3
> LPOP myList
"a"
> LRANGE myList 0 -1
1) "b"
2) "c"

Typical scenarios

Message queues (simple FIFO)

Stacks (LIFO) using LPUSH / LPOP Paginated feeds via

LRANGE

Hash

Hash stores a map of field‑value pairs inside a single key, ideal for representing objects.

Common commands

HSET key field value
HGET key field
HMSET / HMGET
HGETALL key
HDEL key field …
HLEN key
HINCRBY key field increment

Example usage

> HMSET user:id name "Alice" age 30
OK
> HGET user:id name
"Alice"
> HINCRBY user:id age 1
31

Typical scenarios

Storing user profiles, product details, shopping‑cart items.

Partial updates without rewriting the whole object.

Set

Set is an unordered collection of unique elements, implemented with hash tables and integer sets.

Common commands

SADD key member …
SMEMBERS key
SINTER / SUNION / SDIFF

– intersection, union, difference SINTERSTORE / SUNIONSTORE / SDIFFSTORE – store the result SPOP / SRANDMEMBER – random element(s) SCARD key – cardinality SISMEMBER key member Example usage

> SADD mySet a b c
3
> SINTER mySet otherSet
1) "b"

Typical scenarios

Deduplication of IDs, UV counting (often with HyperLogLog).

Computing common followers, friends, or recommendation lists.

Random draws for lotteries.

Sorted Set (Zset)

Sorted Set adds a floating‑point score to each member, enabling ordered retrieval. Internally it uses a skip‑list plus a hash table.

Common commands

ZADD key score member …
ZCARD key
ZRANGE / ZREVRANGE key start stop [WITHSCORES]
ZRANK / ZREVRANK key member
ZINTERSTORE / ZUNIONSTORE / ZDIFFSTORE

Example usage

> ZADD leaderboard 100 user1 200 user2
2
> ZREVRANGE leaderboard 0 -1 WITHSCORES
1) "user2"
2) "200"
3) "user1"
4) "100"

Typical scenarios

Leaderboards, ranking systems, priority queues.

Time‑based feeds where the score represents a timestamp.

Redis Special Data Types

Bitmap

Bitmap is not a separate Redis type; it is a set of bit‑level operations on a String treated as a bit vector. It is useful for storing binary flags efficiently.

Common commands

SETBIT key offset value
GETBIT key offset
BITCOUNT key [start end]
BITOP AND|OR|XOR|NOT dest key …

Example usage

# set bit 7 to 1
SETBIT mykey 7 1
# clear it
SETBIT mykey 7 0
# count set bits
BITCOUNT mykey

Typical scenarios

User sign‑in tracking, activity flags, simple boolean analytics.

HyperLogLog

HyperLogLog provides cardinality estimation with a fixed 12 KB memory footprint, suitable for massive unique‑count problems. The algorithm yields an error around 0.81 %.

Common commands

PFADD key element …
PFCOUNT key …
PFMERGE destkey sourcekey …

Example usage

> PFADD hll user1 user2 user3
1
> PFCOUNT hll
3

Typical scenarios

Estimating daily active users, unique visitors, or event counts at scale.

Geospatial (GEO)

GEO indexes store longitude/latitude pairs using the Sorted Set score field (via GeoHash). They enable radius queries and distance calculations.

Common commands

GEOADD key lon lat member …
GEOPOS key member …
GEODIST key member1 member2 unit
GEORADIUS key lon lat radius unit [WITHCOORD] [ASC|DESC] [COUNT n]
GEORADIUSBYMEMBER key member radius unit …

Example usage

> GEOADD places 116.33 39.89 beijing
1
> GEODIST places beijing shanghai km
1064.5

Typical scenarios

Finding nearby users, stores or assets.

Location‑based services such as “people near me”.

Summary

Redis offers five core data structures (String, List, Hash, Set, Sorted Set) each backed by specific internal representations (SDS, linked list/QuickList, hash table, skip‑list, etc.). In addition, three special structures—Bitmap, HyperLogLog and Geospatial—extend Redis functionality for binary flag storage, probabilistic cardinality estimation, and location indexing.

Key take‑aways:

String : binary‑safe, O(1) length, use for simple caching, counters, or distributed locks.

List : doubly linked list, O(1) push/pop, ideal for queues, stacks, and paginated feeds.

Hash : field‑value map, efficient object storage and partial updates.

Set : unordered unique collection, supports set algebra (intersection, union, difference) and random element extraction.

Sorted Set : ordered by score, perfect for leaderboards, priority queues, and time‑based feeds.

Bitmap : bit‑level operations on a String, compact representation for binary states.

HyperLogLog : probabilistic distinct count with ~0.81 % error, constant 12 KB memory.

Geospatial : latitude/longitude stored via GeoHash in a Sorted Set, enables radius and distance queries.

For detailed command reference, see the official Redis documentation at https://redis.io/commands/.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CacheredisData TypesNoSQLcommands
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.