Redis Java client with code example

Redisson is a thread-safe Redis client for the Java programming language. It allows you to use all of the familiar Java collections and data structures on top of Redis - such as List, Map, Queue, Lock, Semaphore and many more.

Redisson has practically zero learning curve for Java developers who already know these standard interfaces and want to use Redis. There's no need to learn the Redis API itself and its over 400 different commands. As a result, you can spend less time learning the framework and more time on important tasks such as business logic development on Java.

This Redis Java client includes features such as:


  • 40+ different objects and services for Java
  • Thread-safe implementation
  • Asynchronous API
  • Reactive Streams API
  • RxJava2 API
  • Distributed objects
  • Distributed collections
  • Distributed locks and synchronizers
  • Distributed services
  • Implementations of Spring Cache, Spring Transaction API, Spring Boot Starter, Spring Session, Hibernate Cache, XA Transaction API, JCache API (JSR-107), and Tomcat Session Manager
  • Support for SSL and OSGi
  • Support for Android
  • Support for many popular codecs (Jackson JSON, Avro, Smile, CBOR, MsgPack, Kryo, Amazon Ion, FST, LZ4, Snappy and JDK Serialization)

If performance is at a premium for your project, consider upgrading to Redisson PRO. In a benchmark environment, Redisson PRO exhibited significantly better throughput and execution time over the open-source version of Redisson.


Installing Redisson

Redisson is a Redis Java client compatible with both Maven and Gradle for build automation. The necessary code is below:


Maven
Below is dependency description for this Redis Java client:

<dependency>
   <groupId>org.redisson</groupId>
   <artifactId>redisson</artifactId>
   <version>3.11.5</version>
</dependency>  
Gradle
Below is dependency description for this Redis Java client:

compile 'org.redisson:redisson:3.11.5'
Redis Java code example
Redisson is a Redis java client that easily allows you to integrate Redis with Java. You'll get access to familar Java data structures and collections based on Redis. Below is example code that shows how simple it can be to start using Redis with Java thanks to Redisson:

import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.redisson.Redisson;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;

public class MapExamples {

    public static void main(String[] args) throws IOException {
        // connects to 127.0.0.1:6379 by default
        RedissonClient redisson = Redisson.create();
        
        RMap map =  redisson.getMap("myMap");
        map.put("a", 1);
        map.put("b", 2);
        map.put("c", 3);
        
        boolean contains = map.containsKey("a");
        
        Integer value = map.get("c");
        Integer updatedValue = map.addAndGet("a", 32);
        
        Integer valueSize = map.valueSize("c");
        
        Set keys = new HashSet();
        keys.add("a");
        keys.add("b");
        keys.add("c");
        Map mapSlice = map.getAll(keys);
        
        // use read* methods to fetch all objects
        Set allKeys = map.readAllKeySet();
        Collection allValues = map.readAllValues();
        Set> allEntries = map.readAllEntrySet();
        
        // use fast* methods when previous value is not required
        boolean isNewKey = map.fastPut("a", 100);
        boolean isNewKeyPut = map.fastPutIfAbsent("d", 33);
        long removedAmount = map.fastRemove("b");
        
        redisson.shutdown();
    }
    
}

Consider Redisson PRO version for ultra-fast performance and support by SLA.


Redis Java clients comparison: Redisson vs. Jedis vs. Lettuce

Of course, Redisson isn’t the only project for integrating Redis and Java. Your other options include Jedis and Lettuce. So why choose Redisson over these alternatives?

First, if performance is important to you, then Redisson is the better choice over Jedis. According to a benchmark test between Redisson PRO and Jedis, Redisson PRO is faster than Jedis for all Redis commands except the blocking commands. Redisson PRO is also able to improve Redis’ performance, which is already excellent at 55000-75000 ops/sec, to a blazingly fast 100000-213000 ops/sec.

Both Redisson and Lettuce use the asynchronous Netty client-server framework. Redisson also includes integrations with a number of other frameworks and libraries, such as Apache Tomcat and Hibernate. Redisson distinguishes itself from Lettuce because it is a higher-level client with another layer of abstraction, offering collections and other interfaces instead of raw Redis commands.

Similar articles