Operations with Numbers in Redis

Operations with Numbers

Welcome back! Now that you've learned how to work with numbers in Redis, it's time to build on that knowledge and explore some basic operations with these numbers. This lesson will show you how to perform operations like incrementing, decrementing, and modifying numeric values directly in Redis.

What You'll Learn

In this lesson, you will learn how to:

  1. Increment and decrement numeric values.
  2. Modify numeric values using operations such as increments by a floating point.

Here's the code snippet that we'll be working with:

import Redis from 'ioredis';

// Create and connect Redis client
const redis = new Redis();

// Setting and getting string values
await redis.set('count', 5);
await redis.set('completion_rate', 95.5);
await redis.set('duration', 0);

await redis.decr('count'); // 4
await redis.decrby('count', 2); // 2
await redis.incr('duration'); // 1
await redis.incrby('duration', 2); // 3
await redis.incrbyfloat('completion_rate', 1.5); // 97

// Fetch the values and log them
const count = await redis.get('count');
const duration = await redis.get('duration');
const completion_rate = await redis.get('completion_rate');

console.log(`Course count: ${count}`); // 2
console.log(`Duration: ${duration}`); // 3
console.log(`Completion rate: ${completion_rate}`); // 97

await redis.disconnect();
  • After setting initial values for count, completion_rate, and duration, we perform various operations:
    • The decr operation decrements the value of count by 1, and decrby decrements it by the specified value, in this case, 2. So, the final value of count is 2.
    • The incr operation increments the value of duration by 1, and incrby increments it by the specified value, in this case, 2. So, the final value of duration is 3. Keep in mind, that the decr and incr operations are atomic, meaning they ensure thread-safe updates even when multiple clients modify the same key at the same time. These operations can only be performed on keys containing valid integer values. If the key holds a non-integer value, Redis will throw an error.
    • incrbyfloat increments the value of completion_rate by the specified floating-point value, in this case, 1.5. So, the final value of completion_rate is 97. Note, that while it supports floating-point arithmetic, be aware of potential precision issues common to floating-point operations due to how numbers are represented in memory.
  • At the end, we fetch the updated values of count, duration, and completion_rate and log them to the console.

Note that the incr, decr, incrby, and decrby operations cannot be applied to keys that contain floating-point values, which is why we use incrbyfloat to increment floating-point values. Note that to decrement floating-point values, you can use incrbyfloat with a negative value.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal