0
0
Redisquery~30 mins

HINCRBY for numeric fields in Redis - Mini Project: Build & Apply

Choose your learning style9 modes available
Using HINCRBY to Update Numeric Fields in Redis Hashes
📖 Scenario: You are managing a simple Redis database for a small online store. You want to keep track of the number of items sold for each product using Redis hashes.
🎯 Goal: Build a Redis hash to store product sales counts and use HINCRBY to update these counts as sales happen.
📋 What You'll Learn
Create a Redis hash called product_sales with initial sales counts for three products.
Set a variable increment to the number 5 to represent new sales.
Use HINCRBY to increase the sales count of the product apple by the value of increment.
Add a final command to retrieve all fields and values from the product_sales hash.
💡 Why This Matters
🌍 Real World
Tracking product sales counts in an online store using Redis hashes is a common real-world use case.
💼 Career
Understanding how to update numeric fields efficiently in Redis is important for backend developers working with caching and real-time data.
Progress0 / 4 steps
1
Create the initial Redis hash with product sales counts
Create a Redis hash called product_sales with these exact fields and values: apple 10, banana 20, and orange 15 using the HMSET command.
Redis
Need a hint?

Use HMSET followed by the hash name and pairs of field and value.

2
Set the increment value for sales
Set a Redis string variable called increment to the number 5 using the SET command.
Redis
Need a hint?

Use SET increment 5 to store the increment value.

3
Increase the sales count for apple using HINCRBY
Use the HINCRBY command to increase the apple field in the product_sales hash by the value stored in increment. Since HINCRBY requires a number, first retrieve the value of increment and then use it in HINCRBY.
Redis
Need a hint?

Since Redis commands are atomic, you can directly use the number 5 in HINCRBY here.

4
Retrieve all product sales counts
Use the HGETALL command to get all fields and values from the product_sales hash.
Redis
Need a hint?

Use HGETALL product_sales to see all product sales counts.