Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Redis · Command

HSET

Creates or updates one or more fields in a Redis hash. It returns how many fields were newly added, excluding fields whose existing values were overwritten.

How it works

HSET writes one or more field-value pairs into the hash stored at a key. Existing fields are overwritten; if the key does not exist, Redis creates a new hash. The integer reply counts only fields that were newly added, not fields whose existing values were changed.

Syntax

HSET key field value [field value ...]

Parameters

NameTypeDescription
keykeyThe Redis key containing the target hash.
fieldfieldThe first hash field to set.
valuevalueThe value assigned to the first field.
[field value ...]field-value pairsOptional additional field-value pairs, repeated as needed.

Returns

An integer equal to the number of fields newly added to the hash; updates to existing fields are not counted.

Key points

  • Use HSET to add a field to a hash or change the value associated with an existing field.
  • The command maps each supplied field to the value immediately following it.
  • Repeated writes to the same field are replacement operations rather than merges.

Common mistakes

  • A missing key is initialized automatically as a hash.
  • Arguments must consist of at least one field-value pair; additional pairs can be supplied in the same command.

Example: Create and inspect a hash

HSET user:42 name "Ada" role "engineer"
HGETALL user:42

Creates or updates two fields, then reads the complete hash.

When to use it

Model flat records

Store a compact record under one Redis key.

Partial updates

Change selected attributes without replacing the whole hash.

Multi-field writes

Initialize several related attributes in one command.

Interview questions

QWhat does HSET return?

HSET stores field-value pairs in a Redis hash and returns the number of fields that were newly created. Updating an existing field returns zero for that field, so the reply is not the total number of pairs written.

Follow-up: How can several fields be written atomically from the command interface? Supply multiple field-value pairs in one HSET invocation; the command accepts repeated pairs after the first one.

QWhat happens when the target key does not exist?

If the key is absent, HSET creates a key containing a hash. If the key already contains a hash, the specified fields are added or modified.

Follow-up: What should be checked when the key has another Redis data type? The command cannot operate on that incompatible type and should be treated as a wrong-type error.

QWhat is the HSET syntax?

The command accepts a key followed by one field and value, with optional additional field-value pairs. Its syntax is HSET key field value [field value ...].

Follow-up: Which command retrieves one field after writing it? HGET reads the value associated with a specified hash field.

  • HGET
  • HGETALL

Source: HSET | Docs. Checked 2026-10-16.