Mastering Redis HSET: Basics, Commands, and Internal Encoding
This article introduces Redis HSET, covering its definition, suitable use cases, common commands for creating, reading, updating, and deleting hash fields, as well as the underlying encoding mechanisms (ziplist vs hashtable) and best practices, including the deprecation of HMSET.
What is Redis HSET?
Redis HSET is a hash table where both field and value are strings, stored in memory. Each hash can store up to 2^32‑1 key‑value pairs (over 40 billion).
Applicable Scenarios
Suitable for O(1) dictionary lookups of a field, e.g., task configuration where the task type is the field and the configuration parameters are the value.
Common Operations
We discuss creation, query, update, and deletion.
Creation
Use HSET or HSETNX to create a hash.
Query
HGET – retrieve a single element.
HGETALL – retrieve all data.
HLEN – get the number of elements.
HSCAN – cursor‑based iteration.
Update
HSET can add new elements; HDEL deletes elements.
Deletion
DEL removes an entire HSET object.
Write Commands
HSET
127.0.0.1:6379> HSET hsetniuniu f1 v1 f2 v2 f3 v3
(integer) 3HSETNX
127.0.0.1:6379> HSETNX hsetniuniu f1 newv1
(integer) 0
127.0.0.1:6379> HSETNX hsetniuniu f4 v4
(integer) 1
127.0.0.1:6379> HSETNX hsetniuniu f5 v5
(integer) 1Since f1 already exists, its value remains unchanged; f4 and f5 are added.
HDEL
127.0.0.1:6379> HDEL hsetniuniu f4 f5
(integer) 2DEL
127.0.0.1:6379> DEL hsetniuniu
(integer) 1HMSET
Syntax: HMSET key field value [field value ...] – sets multiple field‑value pairs. Historically HMSET was required for multi‑field sets, but since Redis 4.0.0 HSET also supports multiple fields. HMSET is deprecated.
As per Redis 4.0.0, HMSET is considered deprecated. Please use HSET in new code.
Read Commands
HGETALL
127.0.0.1:6379> HGETALL hsetniuniu
1) "f1"
2) "v1"
3) "f2"
4) "v2"
5) "f3"
6) "v3"HGET
127.0.0.1:6379> HGET hsetniuniu f1
"v1"HLEN
127.0.0.1:6379> HLEN hsetniuniu
(integer) 3HSCAN
127.0.0.1:6379> HSCAN hsetniuniu 0 COUNT 10
1) "0"
2) "f1"
3) "v1"
4) "f2"
5) "v2"
6) "f3"
7) "v3"Using MATCH to filter results:
127.0.0.1:6379> HSCAN hsetniuniu 0 MATCH *2
1) "0"
2) "f2"
3) "v2"Underlying Encoding
Encoding Formats
HSET uses either a ziplist or a hashtable. Ziplist is used when all keys and values are shorter than 64 bytes and the number of entries is less than 512; otherwise a hashtable is used.
Ziplist stores entries compactly; hashtable stores entries similarly to the Set structure but with non‑null values.
Summary
HSET is a hash dictionary that can store multiple field‑value mappings, such as student scores or task configurations. Its internal encoding switches between ziplist and hashtable based on size thresholds, concepts also seen in List and Set chapters.
NiuNiu MaTe
Joined Tencent (nicknamed "Goose Factory") through campus recruitment at a second‑tier university. Career path: Tencent → foreign firm → ByteDance → Tencent. Started as an interviewer at the foreign firm and hopes to help others.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
