Solution & Challenge: Hash Operations · Redis

Ch 1 · Redis Hashes — card 4 of 4.

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.

await redis.hset("product:1", "name", "Laptop", "price", "999.99", "stock", "50");
console.log("Name:", await redis.hget("product:1", "name"));
console.log("All:", await redis.hgetall("product:1"));
await redis.hincrby("product:1", "stock", -1);
console.log("Stock after sale:", await redis.hget("product:1", "stock"));