Welcome back to our Redis course! Now that you know how to connect to a Redis server using Java, it's time to move forward and explore how to work with numbers in Redis. In this unit, you’ll learn how to set, increment, and decrement numeric values while also understanding Redis's atomicity guarantees. These concepts are critical for managing counters and performing real-time updates.
Redis treats all values as strings, but you can store numbers and perform numeric operations on them directly. Here’s how you can set numeric values in Redis and retrieve them:
Java1// Connect to Redis 2Jedis jedis = new Jedis("localhost", 6379); 3 4// Setting numeric values 5jedis.set("count", "5"); 6jedis.set("completion_rate", "95.5"); 7 8// Retrieving the values 9String count = jedis.get("count"); 10String completionRate = jedis.get("completion_rate"); 11 12System.out.println("Course count: " + count); 13System.out.println("Completion rate: " + completionRate); 14 15// Closing the connection 16jedis.close();
Here, we connect to the Redis server and use the set
command to store two numeric values:
count
with a value of5
.completion_rate
with a value of95.5
.
The get
method retrieves these values, and since Redis stores everything as strings, no special handling is required.
The output will be:
1Course count: 5 2Completion rate: 95.5
Redis allows you to increment numeric values of type int
atomically using the incr
and incrBy
commands.
Java1// Connect to Redis 2Jedis jedis = new Jedis("localhost", 6379); 3 4// Setting an initial value 5jedis.set("count", "0"); 6 7// Incrementing by 1 8jedis.incr("count"); 9System.out.println("After INCR: " + jedis.get("count")); 10 11// Incrementing by a specific value 12jedis.incrBy("count", 5); 13System.out.println("After INCRBY 5: " + jedis.get("count")); 14 15// Closing the connection 16jedis.close();
Here’s what happens:
incr("count")
increments the value ofcount
by 1.incrBy("count", 5)
increments the value by 5.
The output will be:
1After INCR: 1 2After INCRBY 5: 6
You can decrement numeric values of type int
using the decr
and decrBy
commands.
Java1// Connect to Redis 2Jedis jedis = new Jedis("localhost", 6379); 3 4// Setting an initial value 5jedis.set("count", "10"); 6 7// Decrementing by 1 8jedis.decr("count"); 9System.out.println("After DECR: " + jedis.get("count")); 10 11// Decrementing by a specific value 12jedis.decrBy("count", 3); 13System.out.println("After DECRBY 3: " + jedis.get("count")); 14 15// Closing the connection 16jedis.close();
In this example:
decr("count")
decrements the value by 1.decrBy("count", 3)
decrements the value by 3.
The output will be:
1After DECR: 9 2After DECRBY 3: 6
Redis operations like INCR
, DECR
, INCRBY
, and DECRBY
are atomic. This means that Redis guarantees each operation is performed in a single, indivisible step, ensuring consistency even when multiple clients access the same key simultaneously.
For example, if two clients try to increment the same key at the same time:
- Redis ensures both increments are applied safely without overwriting each other.
- You don’t need to implement locks or manual synchronization since Redis handles it for you.
Atomicity is critical in applications where accuracy and concurrency matter, such as counters, analytics, or real-time monitoring. This feature makes Redis highly reliable for handling concurrent numeric operations.
By mastering these operations and understanding Redis's atomicity guarantees, you’ll be able to implement robust and efficient systems that involve numeric data.
You've now learned how to work with numbers in Redis by setting, incrementing, and decrementing them. You also learned about Redis's atomicity guarantees, which ensure consistency and reliability in numerical operations. Up next is a practice section where you’ll apply these concepts hands-on to reinforce your understanding.
Let’s dive in and put what you've learned into practice!