Ch 1 · Redis Hashes · Redis

Topic 1 of 5 in Redis — Complete Tutorial (Beginner) — 4 lessons.

Hash Operations

Store and manipulate structured data with Redis hash maps. A hash keeps several field-value pairs under one key, so a whole user or product lives in one place instead of across many separate keys. You will meet HSET for writing fields, HGET and HGETALL for reading them, and HINCRBY for bumping a number safely in a single step.

Hash Operations in Code

Here you see hash commands used from JavaScript on a single user:1 key. Notice how HSET writes several fields at once, while HGET and HMGET read just the fields you ask for and HGETALL returns the lot. Watch HINCRBY add to a counter field, HEXISTS check for a field and HDEL remove one. Every value comes back as a string.

Practice: Hash Operations

Now it is your turn to model a product as a hash. You will create one key holding several fields, read a single field and then the whole hash, and change a stock number without rewriting the other fields. A correct answer shows you can treat a hash like a small object and update one part of it in a single, safe command.

Solution & Challenge: Hash Operations

Compare your attempt with the worked solution, which builds a laptop product hash, reads it back and records a sale by lowering the stock. Then stretch yourself with the challenge: a small user profile system that stores each person as a hash, updates individual fields, counts logins and lists every user key with SCAN, which walks through keys in batches.

All topics in Redis Beginner

  1. Redis Hashes
  2. Redis Introduction and Setup
  3. Redis Lists
  4. Redis Sets
  5. Redis Strings and Numbers