Open In App

A Complete Guide to Redis Hashes

Last Updated : 05 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Redis Hashes are data structures that allow you to store multiple field-value pairs under a single key. Each field-value pair within a hash is referred to as a “field” and “value”. Hashes are useful for representing objects or entities that have various attributes. They are memory-efficient and provide fast access to individual fields.

Redis-hashes

Syntax:

The general syntax for working with Redis Hashes using various Redis commands is as follows:

<command> <key> <field> <value>

Note: <command> is the Redis command for working with Hashes, <key> is the name of the Hash, <field> is the field within the Hash, and <value> is the value associated with the field.

Common Redis Hash Commands:

1. HSET (Hash Set):

  • Sets the value of a field in a Hash.
  • If the field does not exist, it creates the field and assigns the value.

Syntax:

HSET <key> <field> <value>

2. HGET (Hash Get):

  • Retrieves the value of a field in a Hash.

Syntax:

HGET <key> <field>

Example for HSET and HGET:

Input:

HSET user:123 name “Alice”

HSET user:123 age 30

HGET user:123 name

Output:

“Alice”

3. HMSET (Hash Multiple Set):

  • Sets multiple fields and values in a Hash.

Syntax:

HMSET <key> <field1> <value1> [<field2> <value2> … <fieldN> <valueN>]

4. HMGET (Hash Multiple Get):

  • Retrieves the values of multiple fields in a Hash.

Syntax:

HMGET <key> <field1> [<field2> … <fieldN>]

Example for HMSET and HMGET:

Input:

HMSET user:456 name “Bob” age 25 email “bob@example.com”

HMGET user:456 name age email

Output:

1) “Bob”

2) “25”

3) “bob@example.com”

5. HDEL (Hash Delete):

  • Deletes one or more fields from a Hash.

Syntax:

HDEL <key> <field1> [<field2> … <fieldN>]

Example for HDEL:

Input:

HDEL user:123 age

HGET user:123 age

Output:

(nil)

6. HGETALL (Hash Get All):

  • Retrieves all fields and values of a Hash.

Syntax:

HGETALL <key>

Example for HGETALL:

Input:

HSET user:789 name “Charlie” country “USA”

HGETALL user:789

Output:

1) “name”

2) “Charlie”

3) “country”

4) “USA”

7. HEXISTS (Hash Exists):

  • The HEXISTS command checks whether a field exists in a hash.

Syntax:

HEXISTS <key> <field>

Example for HEXISTS:

Input:

HMSET user:1 username alice age 30

HEXISTS user:1 age

Output:

(integer) 1

8. HINCRBY (Hash Increment by Integer):

  • The HINCRBY command increments the integer value of a field in a hash by a specified amount.

Syntax:

HINCRBY <key> <field> <increment>

Example for HINCRBY:

Input:

HSET counter views 100

HINCRBY counter views 50

Output:

(integer) 150

9. HKEYS (Hash Keys):

  • The HKEYS command retrieves all the field names in a hash.

Syntax:

HKEYS <key>

Example for HKEYS:

Input:

HSET user:2 username bob email bob@example.com age 25

HKEYS user:2

Output:

1) “username”

2) “email”

3) “age”

10. HLEN (Hash Length):

  • The HLEN command returns the number of fields in a hash.

Syntax:

HLEN <key>

Example for HLEN:

Input:

HSET book:123 title “Redis in Action” author “Josiah L. Carlson”

HLEN book:123

Output:

(integer) 2

11. HVALS (Hash Values):

  • The HVALS command retrieves all the values in a hash.

Syntax:

HVALS <key>

Example for HVALS:

Input:

HSET preferences:456 theme “dark” language “en”

HVALS preferences:456

Output:

1) “dark”

2) “en”

Time Complexity and Limits of Redis hashes:

Generally, the time complexity for most of the Redis hash commands are O(1) only but a some commands such as HVALS, HGETALL, HKEYS are O(N), where N is the size of the input. Up to 4,294,967,295 (232 – 1) field-value pairs can be stored in each hash. In reality, the only factor limiting your hashes is the total amount of memory on the VMs hosting your Redis setup.

Note: Redis Hashes are particularly useful when you need to represent structured data within a single key. They provide an efficient way to store, retrieve, and modify field-value pairs, making them suitable for scenarios such as storing user profiles, configuration settings, and more.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads