Master Redis Data Types: Real‑World Use Cases & Implementation Guide

This comprehensive guide explains Redis's core and newer data types—including String, List, Hash, Set, Zset, BitMap, HyperLogLog, GEO, and Stream—detailing their internal implementations, essential commands, and practical scenarios such as caching, counting, locking, ranking, geolocation, and reliable message queuing.

macrozheng
macrozheng
macrozheng
Master Redis Data Types: Real‑World Use Cases & Implementation Guide

String

String is the basic key‑value type. Value can be a string or number, up to 512 MB. Internally Redis stores strings as integers or SDS (simple dynamic strings). SDS supports binary data, O(1) length lookup and safe concatenation.

Encoding options are int , raw and embstr . Small strings (≤32 bytes) use embstr, larger ones use raw. Integers are stored with int encoding.

Common commands

# SET key value
SET name lin
# GET key
GET name
# EXISTS key
EXISTS name
# STRLEN key
STRLEN name
# DEL key
DEL name

Use cases

Cache whole objects (JSON or binary).

Simple counters (INCR, INCRBY, DECR, DECRBY).

Distributed lock with SET key value NX PX ttl.

List

List is an ordered collection of strings, supporting push/pop at both head and tail. Maximum length 2³²‑1 (~4 billion). Internally implemented with quicklist (a combination of ziplist and linked list).

Common commands

# Push to head
LPUSH key value [value ...]
# Push to tail
RPUSH key value [value ...]
# Pop from head
LPOP key
# Pop from tail
RPOP key
# Range query
LRANGE key start stop
# Blocking pop
BLPOP key [key ...] timeout
BRPOP key [key ...] timeout

Use cases

Message queue (LPUSH + RPOP or RPUSH + LPOP).

Blocking read with BRPOP to avoid busy loops.

Note: List cannot form consumer groups; Stream is preferred for that.

Hash

Hash stores a map of field‑value pairs, ideal for representing objects. Internally uses ziplist for small hashes or hash table otherwise (listpack in recent versions).

Common commands

# Set field
HSET key field value
# Get field
HGET key field
# Set multiple fields
HMSET key field value [field value ...]
# Get all fields
HGETALL key
# Delete field
HDEL key field [field ...]
# Increment numeric field
HINCRBY key field increment

Use cases

Cache object attributes (e.g., user:id → name, age).

Shopping cart: HSET cart:userId productId quantity.

Set

Set is an unordered collection of unique strings. Supports typical set operations.

Common commands

# Add members
SADD key member [member ...]
# Remove members
SREM key member [member ...]
# Membership test
SISMEMBER key member
# Get all members
SMEMBERS key
# Set cardinality
SCARD key
# Set operations
SINTER key [key ...]
SUNION key [key ...]
SDIFF key [key ...]

Use cases

Deduplication (e.g., unique visitors).

Likes tracking (article → set of user IDs).

Common followings (set intersection).

Lottery (store participants in a set).

Zset (Sorted Set)

Zset stores members with a floating‑point score, providing ordering. Internally uses ziplist for small sets or skiplist for larger ones.

Common commands

# Add with score
ZADD key score member [score member ...]
# Remove member
ZREM key member [member ...]
# Get score
ZSCORE key member
# Range by rank
ZRANGE key start stop [WITHSCORES]
# Range by score
ZRANGEBYSCORE key min max [WITHSCORES]
# Increment score
ZINCRBY key increment member

Use cases

Leaderboards (e.g., article likes).

Phone number or name sorting with ZRANGEBYLEX.

BitMap

Bitmap is a string‑based bit array used for binary state tracking. Operations are O(1) and memory‑efficient.

Common commands

# Set bit
SETBIT key offset 0|1
# Get bit
GETBIT key offset
# Count bits set to 1
BITCOUNT key [start end]
# Bitwise operations
BITOP AND destkey key1 [key2 ...]
BITPOS key 1

Use cases

User sign‑in tracking.

Online status (one bit per user ID).

Counting users with consecutive sign‑ins via BITOP AND.

HyperLogLog

HyperLogLog provides approximate cardinality counting with ~0.81 % error using only 12 KB per key.

Common commands

# Add element
PFADD key element [element ...]
# Get estimate
PFCOUNT key [key ...]
# Merge
PFMERGE destkey sourcekey [sourcekey ...]

Use cases

UV counting for high‑traffic webpages.

GEO

GEO stores geographic coordinates using a Sorted Set with geohash encoding.

Common commands

# Add location
GEOADD key longitude latitude member [longitude latitude member ...]
# Get position
GEOPOS key member [member ...]
# Distance between members
GEODIST key member1 member2 m|km|ft|mi
# Radius query
GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [COUNT count] [ASC|DESC]

Use case

Ride‑hailing: store vehicle locations and query nearby cars.

Stream

Stream is a log‑structured data type designed for message queues. Each entry gets a unique ID ( ). Supports consumer groups.

Common commands

# Add entry (auto ID)
XADD stream * field value
# Read entries
XREAD STREAMS stream last_id
# Blocking read
XREAD BLOCK timeout STREAMS stream $
# Consumer group
XGROUP CREATE stream group_name $
XREADGROUP GROUP group_name consumer_name STREAMS stream >
# Acknowledge
XACK stream group_name entry_id
# Pending entries
XPENDING stream group_name

Use cases

Reliable message queue with automatic ID generation and consumer groups.

Processing guarantees via pending list and XACK.

Summary

Redis offers five core data types (String, Hash, List, Set, Zset) and four newer types (BitMap, HyperLogLog, GEO, Stream), each backed by specific internal structures optimized for performance and memory. Choosing the right type depends on the required operations: simple key‑value, ordered collections, set semantics, ranking, binary flags, cardinality estimation, geolocation, or streaming messages.

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.

performancerediscachingData Structures
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

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.