Databases 22 min read

Eight Redis Data Types and Their Core Commands

This article introduces Redis's eight fundamental data types—String, List, Set, Hash, Sorted Set, Geospatial, HyperLogLog, and Bitmap—explaining their characteristics, typical use‑cases, and providing concrete command‑line examples that demonstrate creation, manipulation, and querying of each type.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Eight Redis Data Types and Their Core Commands

Redis offers eight primary data structures, each suited for different storage and retrieval scenarios. The following sections describe each type and include representative Redis CLI commands.

1. String

Simple key‑value pairs that can store text, numbers, or binary data. Common operations include SET, GET, APPEND, INCR / DECR, and range queries.

127.0.0.1:6379> set key1 v1
127.0.0.1:6379> get key1 "v1"
127.0.0.1:6379> append key1 "hello"
127.0.0.1:6379> get key1 "v1hello"
127.0.0.1:6379> strlen key1

2. List

Ordered collections implemented as linked lists, usable as stacks, queues, or blocking queues. Commands start with LPUSH, RPUSH, LPOP, RPOP, LRANGE, etc.

127.0.0.1:6379> lpush list one
127.0.0.1:6379> rpush list right
127.0.0.1:6379> lrange list 0 -1
127.0.0.1:6379> lpop list
127.0.0.1:6379> rpop list
127.0.0.1:6379> lindex list 1
127.0.0.1:6379> llen list

3. Set

Unordered collections of unique strings. Useful for membership tests, set operations, and random sampling. Core commands include SADD, SMEMBERS, SREM, SINTER, SDIFF, SUNION, and SPOP.

127.0.0.1:6379> sadd myset "hello"
127.0.0.1:6379> smembers myset
127.0.0.1:6379> srem myset "hello"
127.0.0.1:6379> sinter key1 key2
127.0.0.1:6379> sdiff key1 key2
127.0.0.1:6379> sunion key1 key2

4. Hash

Maps between fields and values, ideal for representing objects with mutable attributes. Commands such as HSET, HGET, HMSET, HGETALL, HINCRBY, and HDEL manipulate hash entries.

127.0.0.1:6379> hset myhash field xxx
127.0.0.1:6379> hget myhash field
127.0.0.1:6379> hmset myhash field1 hello field2 world
127.0.0.1:6379> hgetall myhash
127.0.0.1:6379> hincrby myhash field3 1
127.0.0.1:6379> hdel myhash field1

5. Sorted Set (Zset)

Ordered sets where each member has a floating‑point score, enabling range queries and ranking. Key commands are ZADD, ZRANGE, ZREVRANGE, ZRANGEBYSCORE, ZREM, and ZCARD.

127.0.0.1:6379> zadd salary 2500 xiaohong
127.0.0.1:6379> zrange salary 0 -1
127.0.0.1:6379> zrevrange salary 0 -1
127.0.0.1:6379> zrem salary xiaohong
127.0.0.1:6379> zcard salary

6. Geospatial

Introduced in Redis 3.2, the GEO type stores longitude/latitude pairs and provides distance calculations. Commands include GEOADD, GEOPOS, GEODIST, GEORADIUS, and GEORADIUSBYMEMBER.

127.0.0.1:6379> geoadd china:city 116.40 39.90 beijing
127.0.0.1:6379> geopos china:city beijing
127.0.0.1:6379> geodist china:city beijing shanghai km
127.0.0.1:6379> georadius china:city 110 30 1000 km
127.0.0.1:6379> georadiusbymember china:city shanghai 1000 km

7. HyperLogLog

Probabilistic data structure for cardinality estimation using only ~12 KB of memory, suitable for UV counting. Core commands are PFADD, PFCOUNT, and PFMERGE.

127.0.0.1:6379> pfadd mykey a b c d e f g h i j
127.0.0.1:6379> pfcount mykey
127.0.0.1:6379> pfadd mykey2 i j z x c v b n m
127.0.0.1:6379> pfmerge mykey3 mykey mykey2
127.0.0.1:6379> pfcount mykey3

8. Bitmap

Bit‑level storage for boolean flags, often used for daily activity tracking. Commands include SETBIT, GETBIT, and BITCOUNT.

127.0.0.1:6379> setbit sign 0 1   # Monday
127.0.0.1:6379> setbit sign 3 1   # Thursday
127.0.0.1:6379> getbit sign 3
127.0.0.1:6379> bitcount sign   # total days checked in

These examples illustrate how Redis's diverse data structures can be leveraged to solve a wide range of application problems, from simple caching to complex geospatial queries.

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.

databaseredisData Typescommandskey-value store
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

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.