To use Redis with Java, you need a Java Redis client. The following sections demonstrate the use of two Java client libraries for Redis: Lettuce and Jedis. Additional Java clients for Redis can be found under the Java section of the Redis Clients page.

Lettuce

Lettuce is a thread-safe Redis client that supports both synchronous and asynchronous connections.

Install Lettuce

See Lettuce’s README file for installation instructions.

Add the following Maven dependency to your pom.xml file to use Lettuce:

<dependency>
    <groupId>biz.paluch.redis</groupId>
    <artifactId>lettuce</artifactId>
    <version>3.2.Final</version>
</dependency>

You can also download the latest Lettuce release from the GitHub repository.

Connect to Redis

The following code creates a connection to Redis using Lettuce:

import com.lambdaworks.redis.*;
    
public class ConnectToRedis {
    
  public static void main(String[] args) {
    RedisClient redisClient = new RedisClient(
      RedisURI.create("redis://[email protected]:port"));
    RedisConnection<String, String> connection = redisClient.connect();
    
    System.out.println("Connected to Redis");
    
    connection.close();
    redisClient.shutdown();
  }
}

To adapt this example to your code, replace the following values in line 7’s URI string with your database’s values:

  • Set password to your database’s password or remove [email protected] to connect without authentication
  • Set host to your database’s host
  • Set port to your database’s port

Lettuce is thread-safe, and the same Lettuce connection can be used from different threads. Using multiple connections is also possible.

Spring integration

If you’re using Spring, add the following XML to your bean configuration file to create a Lettuce instance:

<bean id="RedisClient" class="com.lambdaworks.redis.support.RedisClientFactoryBean">
    <property name="uri" value="redis://host:port"/>
</bean>

Then use it within your managed beans as follows:

import com.lambdaworks.redis.*;
import org.springframework.beans.factory.annotation.Autowired;
    
public class MySpringBean {
    
    private RedisClient redisClient;
    
    @Autowired
    public void setRedisClient(RedisClient redisClient) {
        this.redisClient = redisClient;
    }

    public String ping() {
    
        RedisConnection<String, String> connection = redisClient.connect();
        String result = connection.ping();
        connection.close();
        return result;
    }
}

Once your standalone application exits, remember to shutdown Lettuce by using the shutdown method:

redisClient.shutdown();

If you are using Spring and CDI, the frameworks manage the resources for you, and you do not have to close the client using the shutdown method.

Example code for Redis commands

Once connected to Redis, you can read and write data with Redis command functions.

The following code snippet assigns the value bar to the Redis key foo, reads it back, and prints it:

// open a connection to Redis
...
    
connection.set("foo", "bar");
String value = connection.get("foo");
System.out.println(value);

Example output:

$ java ReadWriteExample
Connected to Redis
bar

SSL

For an added security measure, you can secure the connection using SSL connections. Lettuce supports SSL connections natively.

import com.lambdaworks.redis.*;
    
public class ConnectToRedisSSL {
    
    public static void main(String[] args) {
        RedisClient redisClient = new RedisClient(
            RedisURI.create("rediss://[email protected]:port"));
        RedisConnection<String, String> connection = redisClient.connect();
        System.out.println("Connected to Redis using SSL");
    
        connection.close();
        redisClient.shutdown();
    }
}

Jedis

Jedis is a simple and complete Java client for Redis.

Install Jedis

See the Jedis README file for installation instructions.

Add the following Maven dependency to your pom.xml file to use Jedis:

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.6.2</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

You can also download the latest Jedis release from the GitHub repository. To build it, extract the source and run the following command:

$ cd jedis
~/jedis$ make package

Connect to Redis

The following code creates a connection to Redis using Jedis:

import redis.clients.jedis.Jedis;
     
public class JedisExample {
     
  public static void main(String[] args) throws Exception {
    Jedis jedis = new Jedis("hostname", port);
    jedis.auth("password");
    System.out.println("Connected to Redis");
  }
}

To adapt this example to your code, replace the following values with your database’s values:

  • In line 6, set hostname to your database’s hostname or IP address
  • In line 6, set port to your database’s port
  • In line 7, set password to your database’s password

Example code for Redis commands

Once connected to Redis, you can read and write data with Redis command functions.

The following code snippet assigns the value bar to the Redis key foo, reads it back, and prints it:

// open a connection to Redis
...
 
jedis.set("foo", "bar");
String value = jedis.get("foo");
System.out.println(value);

Example output:

$ java JedisExample
Connected to Redis
bar

Connection pooling

Jedis isn’t thread-safe, and the same Jedis instance shouldn’t be used from different threads. Instead, use JedisPool to handle multiple Jedis instances and connection maintenance.

JedisPool requires Apache Commons Pool 2.3. Download it from Apache Commons or add the following Maven dependency to the pom.xml file:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.3</version>
</dependency>

The following code instantiates a pool of connections:

JedisPool pool = new JedisPool(new JedisPoolConfig(), "hostname", port, Protocol.DEFAULT_TIMEOUT, "password");

Spring integration

If you’re using Spring, add the following XML to your bean configuration file to create a JedisPool:

<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg index="0" ref="jedisPoolConfig" />
        <constructor-arg index="1" value="hostname" />
        <constructor-arg index="2" value="port" />
        <constructor-arg index="3" value="Protocol.DEFAULT_TIMEOUT" />
        <constructor-arg index="4" value="password" />
    </bean>
    
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" >
</bean>

JedisPool is thread-safe and can be stored in a static variable and shared among threads. The following code gets a Jedis instance from the JedisPool:

Jedis redis = null;
    try
    {
        redis = redisPool.getResource();
        return redis.get(keyName);
    }
    catch (JedisConnectionException e)
    {
        if (redis != null)
        {
            redisPool.returnBrokenResource(redis);
            redis = null;
        }
        throw e;
    }
    finally
    {
        if (redis != null)
        {
            redisPool.returnResource(redis);
        }
    }
}

Once your application exits, remember to dispose of the JedisPool by using the destroy method:

redisPool.destroy();

SSL

Jedis does not support SSL connections natively.

For an added security measure, you can secure the connection using stunnel or this Jedis fork, which includes SSL support.