Copied!
Node
Redis

Using Redis Hashes in Node.js — Store and Manage User Data Efficiently

using-redis-hashes-in-node.js
Shahroz Javed
Nov 05, 2025 . 15 views

Table Of Contents

 

Redis Hashes are one of the most powerful data structures available. They allow you to store multiple fields and values under a single key — similar to a JavaScript object. In this post, you'll learn how to store, retrieve, and update Redis hashes using Node.js.

💡 Tip: Redis Hashes are great for representing objects like users, products, or sessions — where each record contains multiple attributes.

Introduction

A Redis Hash is a collection of field-value pairs. Think of it as a mini key-value store inside a single Redis key. Instead of creating multiple keys for each property (like user:101:name, user:101:email), you can store them together under one hash key (user:101).

1. Install Redis Client

If you haven’t already installed the Redis client for Node.js, you can do so with:

npm install redis
    

2. Create and Connect Redis Client

Import the Redis client and connect to your Redis instance:

import { createClient } from "redis";

const client = createClient();

await client.connect();
console.log("Connected to Redis!");
    

3. Add a Redis Hash

You can use hSet() to store multiple fields inside a single hash key.

// Add hash (like an object)
await client.hSet("user:101", {
  name: "Shahroz",
  email: "shahroz@example.com",
  age: 27,
});

console.log("User saved successfully!");
    
⚡ Redis Hashes are ideal for storing structured data — such as user profiles, product details, or app settings.

4. Get a Single Field from the Hash

Use hGet() to retrieve one field from your hash.

const name = await client.hGet("user:101", "name");
console.log("Name:", name);
    

Output:

Name: Shahroz
    

5. Get All Fields of a Hash

To fetch all key-value pairs from a hash, use hGetAll():

const user = await client.hGetAll("user:101");
console.log("User:", user);
    

Output:

User: { name: 'Shahroz', email: 'shahroz@example.com', age: '27' }
    

6. Update a Field in the Hash

Updating a field is as easy as setting it again with hSet():

await client.hSet("user:101", "age", 28);
console.log("User age updated!");
    

Redis will automatically overwrite the old value of that field.

7. Disconnect the Client

Always close your Redis connection when done:

await client.quit();
    

Complete Code Example

import { createClient } from "redis";

const client = createClient();
await client.connect();

// Add hash
await client.hSet("user:101", {
  name: "Shahroz",
  email: "shahroz@example.com",
  age: 27,
});

// Get single field
const name = await client.hGet("user:101", "name");
console.log("Name:", name);

// Get all fields
const user = await client.hGetAll("user:101");
console.log("User:", user);

// Update field
await client.hSet("user:101", "age", 28);

await client.quit();
    
Redis Hash Example Output
Output of Redis Hash operations in Node.js

Conclusion

Redis Hashes provide a fast, lightweight way to store and manage structured data. They’re perfect for user profiles, app settings, or any situation where data fits a key–object model. Combined with Node.js, Redis becomes an extremely efficient tool for modern backend systems.

✅ Try combining Hashes with Lists or Sets to design complete mini-databases inside Redis!

13 Shares

Similar Posts