Redis · Chapter 4 of 26

Hashes

By the end of this chapter you can keep a whole object, like a player's card or a fan's wallet, in one Redis key as a hash: set and read its fields with HSET, HGET, HMGET and HGETALL, look inside it with HEXISTS, HLEN, HKEYS and HVALS, remove fields with HDEL, count money with HINCRBY, set a field only once with HSETNX, and choose between a hash, many string keys and one JSON string. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Redis from the command line
  • 4 tasks
  • Redis
  • In the all labs plan

The first 2 chapters of Redis are free. This one comes with the all labs plan.

Why you need this

A player's card on the match centre is four facts: name, team, role and credits. With strings that is four keys, player:P101:name, player:P101:team and so on, and 176 players make 704 keys. Reading one card is an MGET of four names you must spell right every time, and deleting a player means remembering all four.

The other idea is one string of JSON per player. Then a change to one fact means reading the whole JSON, changing it in the app and writing the whole thing back. Venkat sees the danger at once with wallets: if two app servers do that at the same moment, one for a top-up and one for an entry fee, the second write throws away the first. Money disappears, or appears from nowhere.

Redis has a type made for this: a hash, one key holding many fields, each with its own value, and commands that change one field without touching the others.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    player-card.redis: a player's card in one hash

    Store player P124's card as one hash under the key player:P124, with four fields: name is Dhruv Rathore, team is MUM, role is BAT and credits is 9.5. Set all four with one HSET command (Redis answers 4).

    Then read the whole card back with HGETALL player:P124.

    The hash must have exactly these four fields. Field names and values are case-sensitive.

  2. Task 2

    wallet.redis: top up a wallet and pay an entry fee

    Fan u1007's wallet is the hash wallet:u1007, in whole paise: balance is 10000 (100 rupees) and bonus is 2500 (25 rupees). Venkat needs two changes to balance, one command each: a top-up of 500 rupees (add 50000), then the Mega Contest's entry fee of 49 rupees (take away 4900).

    End with HGET wallet:u1007 balance, which answers "55100". bonus stays 2500.

    Use HINCRBY for both changes: never HSET the wallet, which would write back a number another server may have changed. The Check makes the wallet first; setup.redis does the same in your own Redis.

  3. Task 3

    contest-field.redis: a field set only once

    Winner Takes All, contest 75, is the hash contest:75 (name Winner Takes All, entry_fee 19900, spots 10). Its last spot was just taken, and two app servers both try to record when it filled, in a field filled_at.

    Replay both, in this order, each with HSETNX: server A sets filled_at to 19:12:04 (Redis answers 1), then server B sets filled_at to 19:12:05 (Redis answers 0, and nothing changes).

    End with HGET contest:75 filled_at, which answers "19:12:04". The other three fields stay as they are; do not use HSET on contest:75. The Check makes the contest first; setup.redis does the same in your own Redis.

  4. Task 4 · interview question

    Interview: city.redis, change one field and nothing else

    Fan u1009 is the hash fan:u1009: name Tushar Bhat, handle tushar820, city Hyderabad, joined 2025-10-01. He moved to Pune. city.redis replays what the old code sent: it read the whole fan, deleted the key and wrote the fields back, and it forgot the handle.

    Fix the file: change city to Pune and touch nothing else. Do not DEL the key, and every HSET on fan:u1009 must set only the city field. At the end the hash holds all four fields, with city Pune and the other three as they were. The Check makes the fan first; setup.redis does the same in your own Redis.

    Farhan will ask: why can a hash change one field without reading the rest, and what goes wrong with one JSON string when two app servers change two different fields at once?

What you will learn

  • HSET and HGET: set fields (many at once) and read one.
  • HMGET and HGETALL: read some fields, or all of them.
  • HEXISTS, HLEN, HKEYS, HVALS and HDEL: look inside a hash, and remove fields.
  • HINCRBY: add to a number in a field, for money in paise.
  • HSETNX: set a field only if it is not there yet.
  • A hash, many keys or one JSON string: which to choose, and expiry for a whole hash.

More in Redis from the command line

Build "Hashes" yourself

GullyXI, a Pune fantasy cricket app, falls over on match night. You make it fast with Redis. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Redis are free. This one comes with the all labs plan.