Jedis - Redis Java Client Library

This story will give an introduction to Jedis. Redis, famous for its memory data structure store and used as a database or cache. Jedis is a client library, best used by Spring Framework developers.

Let’s start by declaring the only dependency we will need in the pom.xml


You can take the updated maven dependency from here.

You will need to install Redis from here. After that we can directly dive in and connect to it from our Java code:

Jedis jedis = new Jedis();

The default constructor will work just fine unless you have started the service on a non-default port or on a remote machine, in which case you can configure it correctly by passing the correct values as parameters into the constructor.

Jedis Data Structures

Strings: Strings are the most basic kind of Redis value, useful for when you need to persist simple key-value data types:

import redis.clients.jedis.Jedis;

public class JedisExample {
    public static void main(String[] args) {
        Jedis jedis = new Jedis();
        jedis.set("Animal", "Dog");

Lists: Redis Lists are simply lists of strings, sorted by insertion order and make it an ideal tool to implement, for instance, message queues:

jedis.lpush("Animal", "Cat");
jedis.lpush("Animal", "Tiger");
jedis.lpush("Animal", "Lion");
List<String> list = jedis.lrange("Animal", 0, 1);
for(int i=0;i<list.size();i++){
//This will print only Lion and Tiger as the range of list is given 0 to 1.

Sets: Redis Sets are an unordered collection of unique strings. Unique means sets does not allow repetition of data in a key:

jedis.sadd("Animal", "Dog");
jedis.sadd("Animal", "Cat");
jedis.sadd("Animal", "Dog");;
System.out.println(jedis.smembers("Animal"));// will print Dog and Cat

Hashes: Redis Hashes are maps between the string fields and the string values. Hence, they are the perfect data type to represent objects.

jedis.hset("employee#1", "name", "X");
jedis.hset("employee#1", "designation", "software developer");
String name = jedis.hget("employee#1", "name");
Map<String, String> values = jedis.hgetAll("employee#1");
String designation = values.get("designation");
System.out.println(name+ " || "+designation);// prints "X || software developer"

Sorted Sets: Redis Sorted Sets are similar to Redis Sets with the unique feature of values stored in a set. The difference is, every member of a Sorted Set is associated with a score, that is used in order to take the sorted set ordered, from the smallest to the greatest score.

Map<String, Integer> scores = new HashMap<>();
scores.put("mysql", 1);
scores.put("mongodb", 2);
scores.put("redis", 3);

scores.keySet().forEach(database -> {
  jedis.zadd("ranking", scores.get(database), database);
String database = jedis.zrevrange("ranking", 0, 1).iterator().next();
long rank = jedis.zrevrank("ranking", "redis");
System.out.println(database + " " + rank);// redis 0

//The rank variable will have a value of 0 because redis is the 1st in the ranking and the ranking is zero-based.

Soon will be covering other aspects of Redis.

Ipseeta Priyadarshini

Software Developer 👩🏼‍💻 Foodie 😋 Explorer 🕵️‍♀️ Dog lover 🐶

Write your comment…

Be the first one to comment