Java guide

Connect your Java application to a Redis database

Install Redis and the Redis client, then connect your Java application to a Redis database.


Jedis is a Java client for Redis designed for performance and ease of use.


To include Jedis as a dependency in your application, edit the dependency file, as follows.

  • If you use Maven:

  • If you use Gradle:

    repositories {
    dependencies {
        implementation 'redis.clients:jedis:4.3.1'
  • If you use the JAR files, download the latest Jedis and Apache Commons Pool2 JAR files from Maven Central or any other Maven repository.

  • Build from source


For many applications, it's best to use a connection pool. You can instantiate and use a Jedis connection pool like so:

package org.example;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class Main {
    public static void main(String[] args) {
        JedisPool pool = new JedisPool("localhost", 6379);

        try (Jedis jedis = pool.getResource()) {
            // Store & Retrieve a simple string
            jedis.set("foo", "bar");
            System.out.println(jedis.get("foo")); // prints bar
            // Store & Retrieve a HashMap
            Map<String, String> hash = new HashMap<>();;
            hash.put("name", "John");
            hash.put("surname", "Smith");
            hash.put("company", "Redis");
            hash.put("age", "29");
            jedis.hset("user-session:123", hash);
            // Prints: {name=John, surname=Smith, company=Redis, age=29}

Because adding a try-with-resources block for each command can be cumbersome, consider using JedisPooled as an easier way to pool connections.

import redis.clients.jedis.JedisPooled;


JedisPooled jedis = new JedisPooled("localhost", 6379);
jedis.set("foo", "bar");
System.out.println(jedis.get("foo")); // prints "bar"

Connect to a Redis cluster

To connect to a Redis cluster, use JedisCluster.

import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.HostAndPort;


Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
jedisClusterNodes.add(new HostAndPort("", 7379));
jedisClusterNodes.add(new HostAndPort("", 7380));
JedisCluster jedis = new JedisCluster(jedisClusterNodes);

Connect to your production Redis with TLS

When you deploy your application, use TLS and follow the Redis security guidelines.

Before connecting your application to the TLS-enabled Redis server, ensure that your certificates and private keys are in the correct format.

To convert user certificate and private key from the PEM format to pkcs12, use this command:

openssl pkcs12 -export -in ./redis_user.crt -inkey ./redis_user_private.key -out redis-user-keystore.p12 -name "redis"

Enter password to protect your pkcs12 file.

Convert the server (CA) certificate to the JKS format using the keytool shipped with JDK.

keytool -importcert -keystore truststore.jks \ 
  -file redis_ca.pem

Establish a secure connection with your Redis database using this snippet.

package org.example;

import redis.clients.jedis.*;


public class Main {

    public static void main(String[] args) throws GeneralSecurityException, IOException {
        HostAndPort address = new HostAndPort("", 6379);

        SSLSocketFactory sslFactory = createSslSocketFactory(
                "secret!", // use the password you specified for keytool command
                "secret!" // use the password you specified for openssl command

        JedisClientConfig config = DefaultJedisClientConfig.builder()
                .user("default") // use your Redis user. More info
                .password("secret!") // use your Redis password

        JedisPooled jedis = new JedisPooled(address, config);
        jedis.set("foo", "bar");
        System.out.println(jedis.get("foo")); // prints bar

    private static SSLSocketFactory createSslSocketFactory(
            String caCertPath, String caCertPassword, String userCertPath, String userCertPassword)
            throws IOException, GeneralSecurityException {

        KeyStore keyStore = KeyStore.getInstance("pkcs12");
        keyStore.load(new FileInputStream(userCertPath), userCertPassword.toCharArray());

        KeyStore trustStore = KeyStore.getInstance("jks");
        trustStore.load(new FileInputStream(caCertPath), caCertPassword.toCharArray());

        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("X509");

        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("PKIX");
        keyManagerFactory.init(keyStore, userCertPassword.toCharArray());

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);

        return sslContext.getSocketFactory();

Example: Indexing and querying JSON documents

Make sure that you have Redis Stack and Jedis installed.

Import dependencies and add a sample User class:

import redis.clients.jedis.JedisPooled;

class User {
    private String name;
    private String email;
    private int age;
    private String city;

    public User(String name, String email, int age, String city) { = name; = email;
        this.age = age; = city;


Connect to your Redis database with JedisPooled.

JedisPooled jedis = new JedisPooled("localhost", 6379);

Let's create some test data to add to your database.

User user1 = new User("Paul John", "", 42, "London");
User user2 = new User("Eden Zamir", "", 29, "Tel Aviv");
User user3 = new User("Paul Zamir", "", 35, "Tel Aviv");

Create an index. In this example, all JSON documents with the key prefix user: are indexed. For more information, see Query syntax.


Use JSON.SET to set each user value at the specified path.

jedis.jsonSetWithEscape("user:1", user1);
jedis.jsonSetWithEscape("user:2", user2);
jedis.jsonSetWithEscape("user:3", user3);

Let's find user Paul and filter the results by age.

var query = new Query("Paul @age:[30 40]");
var result = jedis.ftSearch("idx:users", query).getDocuments();
// Prints: [id:user:3, score: 1.0, payload:null, properties:[$={"name":"Paul Zamir","email":"","age":35,"city":"Tel Aviv"}]]

Return only the city field.

var city_query = new Query("Paul @age:[30 40]");
var city_result = jedis.ftSearch("idx:users", city_query.returnFields("city")).getDocuments();
// Prints: [id:user:3, score: 1.0, payload:null, properties:[city=Tel Aviv]]

Count all users in the same city.

AggregationBuilder ab = new AggregationBuilder("*")
        .groupBy("@city", Reducers.count().as("count"));
AggregationResult ar = jedis.ftAggregate("idx:users", ab);

for (int idx=0; idx < ar.getTotalResults(); idx++) {
    System.out.println(ar.getRow(idx).getString("city") + " - " + ar.getRow(idx).getString("count"));
// Prints:
// London - 1
// Tel Aviv - 2

Learn more

Rate this page