Connect to the server
Connect your Java application to a Redis database
Jedis 7.2.0 introduced a new client connection API:
| New API class | Replaces | Use case |
|---|---|---|
RedisClient |
UnifiedJedis, JedisPool, JedisPooled |
Single connection (with connection pooling) |
RedisClusterClient |
JedisCluster |
Redis Cluster connections |
RedisSentinelClient |
JedisSentinelPool |
Redis Sentinel connections |
The old client classes are now considered deprecated.
Basic connection
The following code opens a basic connection to a local Redis server:
package org.example;
import redis.clients.jedis.RedisClient;
public class Main {
public static void main(String[] args) {
RedisClient jedis = RedisClient.create("redis://localhost:6379");
// Code that interacts with Redis...
jedis.close();
}
}
Jedis supports connection URLs through RedisClient.create().
A URL can set the host, port, credentials, database, and whether to use TLS,
so you can use one instead of passing those options separately.
After you have connected, you can check the connection by storing and retrieving a simple string value:
...
String res1 = jedis.set("bike:1", "Deimos");
System.out.println(res1); // OK
String res2 = jedis.get("bike:1");
System.out.println(res2); // Deimos
...
Connect to a Redis cluster
To connect to a Redis cluster, use RedisClusterClient.
import redis.clients.jedis.RedisClusterClient;
import redis.clients.jedis.HostAndPort;
//...
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7379));
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7380));
RedisClusterClient jedis = RedisClusterClient.create(jedisClusterNodes);
A command that takes several keys, such as msetex(), needs all of its keys to
hash to the same slot. From Jedis 8.1.0, msetex() throws a
JedisClusterOperationException before it writes anything if the keys are in
different slots. Earlier versions split the command by slot, which could set some
keys and not others. To keep related keys in one slot, give them a shared
hash tag, such as {user:42}:name and {user:42}:session.
Connect to Redis Sentinel
To connect through Redis Sentinel,
use RedisSentinelClient. Pass the name of the primary that the Sentinels monitor to
masterName(), and the Sentinel addresses to sentinels(). The client asks the Sentinels
for the current primary and switches to the new one after a failover.
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.RedisSentinelClient;
import java.util.HashSet;
import java.util.Set;
//...
Set<HostAndPort> sentinels = new HashSet<>();
sentinels.add(new HostAndPort("localhost", 26379));
sentinels.add(new HostAndPort("localhost", 26380));
sentinels.add(new HostAndPort("localhost", 26381));
RedisSentinelClient jedis = RedisSentinelClient.builder()
.masterName("mymaster")
.sentinels(sentinels)
.build();
jedis.set("foo", "bar");
System.out.println(jedis.get("foo")); // bar
jedis.close();
The Sentinels and the data nodes have separate credentials. Pass a client configuration for
the primary to clientConfig(), and one for the Sentinels to sentinelClientConfig():
import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.JedisClientConfig;
//...
// Credentials for the primary and replica nodes.
JedisClientConfig masterConfig = DefaultJedisClientConfig.builder()
.password("secret!") // use your Redis password
.build();
// Credentials for the Sentinel nodes.
JedisClientConfig sentinelConfig = DefaultJedisClientConfig.builder()
.password("sentinelSecret!") // use your Sentinel password
.build();
RedisSentinelClient jedis = RedisSentinelClient.builder()
.masterName("mymaster")
.sentinels(sentinels)
.clientConfig(masterConfig)
.sentinelClientConfig(sentinelConfig)
.build();
RedisSentinelClient always sends commands to the primary. It doesn't support reading
from replicas.
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 \
-storepass REPLACE_WITH_YOUR_PASSWORD \
-file redis_ca.pem
Establish a secure connection with your Redis database using this snippet.
package org.example;
import redis.clients.jedis.*;
import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
public class Main {
public static void main(String[] args) throws GeneralSecurityException, IOException {
HostAndPort address = new HostAndPort("my-redis-instance.cloud.redislabs.com", 6379);
SslOptions sslOptions = SslOptions.builder()
.truststore(new File("./truststore.jks"), "secret!".toCharArray())
.trustStoreType("jks")
// optional: use if Redis requires client certificate
.keystore(new File("./redis-user-keystore.p12"), "secret!".toCharArray())
.keyStoreType("pkcs12")
.build();
JedisClientConfig config = DefaultJedisClientConfig.builder()
.sslOptions(sslOptions)
.user("default") // use your Redis user. More info https://redis.io/docs/latest/operate/oss_and_stack/management/security/acl/
.password("secret!") // use your Redis password
.build();
RedisClient jedis = RedisClient.builder()
.hostAndPort(address)
.clientConfig(config)
.build();
jedis.set("foo", "bar");
System.out.println(jedis.get("foo")); // prints bar
}
}
Connect using Smart client handoffs (SCH)
Smart client handoffs (SCH) is a feature of Redis Cloud and Redis Software servers that lets them actively notify clients about planned server maintenance shortly before it happens. This lets a client take action to avoid disruptions in service. See Smart client handoffs for more information about SCH.
SCH requires Jedis v8.1.0 or later and the
RESP3 protocol.
It works with RedisClient (standard routing) and RedisClusterClient
(OSS Cluster API connections).
The older Jedis, JedisPool and JedisCluster classes don't support it.
By default, RedisClient and RedisClusterClient attempt to connect via SCH
when you select RESP3, but fall back to a non-SCH connection if the server
doesn't support it. To configure SCH explicitly, pass a
MaintenanceNotificationsConfig object to the builder's
maintenanceNotifications() method:
RedisClient jedis = RedisClient.builder()
.hostAndPort(new HostAndPort("localhost", 6379))
.clientConfig(DefaultJedisClientConfig.builder()
.protocol(RedisProtocol.RESP3)
.build())
.maintenanceNotifications(MaintenanceNotificationsConfig.builder()
.mode(MaintenanceNotificationsConfig.Mode.ENABLED)
.relaxedTimeout(30_000)
.relaxedBlockingTimeout(0)
.build())
.build();
RedisClusterClient.builder() accepts the same maintenanceNotifications() method.
The MaintenanceNotificationsConfig builder accepts the following parameters:
| Name | Description |
|---|---|
mode |
Whether or not to enable SCH. The options are Mode.DISABLED, Mode.ENABLED (require SCH and fail the connection setup if not supported), and Mode.AUTO (use SCH if the server supports it and the connection uses RESP3). The default is Mode.AUTO. |
relaxedTimeout |
The socket read timeout, in milliseconds, for non-blocking commands while the server is performing maintenance. The default is 10000. |
relaxedBlockingTimeout |
The socket read timeout, in milliseconds, for blocking commands such as BLPOP while the server is performing maintenance. The default is 0 (no timeout). |
relaxedWindowMaxDuration |
A Duration after which relaxed timeouts revert on their own if the notification that ends the maintenance is lost. The default is 60 seconds. |
endpointType |
The type of address to request when the server moves an endpoint. The options are EndpointType.INTERNAL_IP, INTERNAL_FQDN, EXTERNAL_IP, EXTERNAL_FQDN and NONE (reconnect to the configured endpoint). By default, Jedis chooses a type based on the connection. This setting applies to RedisClient only. |
Relaxed timeouts never tighten a timeout. While maintenance is in progress, a connection uses the looser of the relaxed value and the timeout you configured.
To turn SCH off, pass MaintenanceNotificationsConfig.DISABLED:
RedisClient jedis = RedisClient.builder()
.hostAndPort(new HostAndPort("localhost", 6379))
.maintenanceNotifications(MaintenanceNotificationsConfig.DISABLED)
.build();
Redis Cloud supports relaxed timeouts only (and not pre-handoffs) for SCH if you are using
Google Cloud Private Service Connect
or the OSS Cluster API with AWS PrivateLink
(see Smart client handoffs for more information).
To use relaxed timeouts with these connections, set the endpointType to
EndpointType.NONE. All other configurations have full support for both relaxed timeouts and pre-handoffs.
How Jedis responds to maintenance
With RedisClient, Jedis handles five notifications:
- Before a shard migration (
MIGRATING) or failover (FAILING_OVER), it relaxes the timeouts on the connection that received the notification. It restores them when the matchingMIGRATEDorFAILED_OVERnotification arrives. - When the server moves the endpoint (
MOVING), it relaxes the timeouts for the whole pool, opens new connections to the new address and retires the connections to the old address. A connection that you hold past the grace period is disconnected by the server, and using it throws aJedisConnectionException.
With RedisClusterClient, Jedis handles two notifications:
- When a slot migration starts (
SMIGRATING), it relaxes the timeouts on every connection of the client. - When the migration completes (
SMIGRATED), it updates its slot map in place and drops the connection pool of a node that has no slots left. It doesn't need aCLUSTER SLOTSrefresh or aMOVEDredirection to find the new owner. LeavemaxAttemptsat its default so that normal redirection still covers any notification that arrives late or out of order.
Jedis doesn't move a live Pub/Sub subscription. If maintenance ends a subscription,
the listening loop fails with a JedisConnectionException and your code must
subscribe again.
For MultiDbClient, SCH is off by default. To enable it for a database, pass
a MaintenanceNotificationsConfig to DatabaseConfig.Builder.maintenanceNotificationsConfig().
Maintenance notifications don't trigger a failover themselves, but relaxed
timeouts help the active database stay usable so that fewer failures reach the circuit breaker.
Connect using client-side caching
Client-side caching is a technique to reduce network traffic between the client and server, resulting in better performance. See Client-side caching introduction for more information about how client-side caching works and how to use it effectively.
To enable client-side caching, specify the RESP3 protocol and pass a cache configuration object during the connection.
The example below shows the simplest client-side caching connection to the default host and port,
localhost:6379.
All of the connection variants described above accept these parameters, so you can
use client-side caching with a connection pool or a cluster connection in exactly the same way.
Client-side caching requires Jedis v5.2.0 or later. To maximize compatibility with all Redis products, client-side caching is supported by Redis v7.4 or later.
The Redis server products support opt-in/opt-out mode and broadcasting mode for CSC, but these modes are not currently implemented by Jedis.
HostAndPort endpoint = new HostAndPort("localhost", 6379);
DefaultJedisClientConfig config = DefaultJedisClientConfig
.builder()
.password("secretPassword")
.protocol(RedisProtocol.RESP3)
.build();
CacheConfig cacheConfig = CacheConfig.builder().maxSize(1000).build();
RedisClient client = RedisClient.builder()
.hostAndPort(endpoint)
.clientConfig(config)
.cacheConfig(cacheConfig)
.build();
Once you have connected, the usual Redis commands will work transparently with the cache:
client.set("city", "New York");
client.get("city"); // Retrieved from Redis server and cached
client.get("city"); // Retrieved from cache
You can see the cache working if you connect to the same Redis database
with redis-cli and run the
MONITOR command. If you run the
code above but without passing cacheConfig during the connection,
you should see the following in the CLI among the output from MONITOR:
1723109720.268903 [...] "SET" "city" "New York"
1723109720.269681 [...] "GET" "city"
1723109720.270205 [...] "GET" "city"
The server responds to both get("city") calls.
If you run the code with cacheConfig added in again, you will see
1723110248.712663 [...] "SET" "city" "New York"
1723110248.713607 [...] "GET" "city"
The first get("city") call contacted the server, but the second
call was satisfied by the cache.
Removing items from the cache
You can remove individual keys from the cache with the
deleteByRedisKey() method of the cache object. This removes all cached items associated
with each specified key, so all results from multi-key commands (such as
MGET) and composite data structures
(such as hashes) will be
cleared at once. The example below shows the effect of removing a single
key from the cache:
client.hget("person:1", "name"); // Read from the server
client.hget("person:1", "name"); // Read from the cache
client.hget("person:2", "name"); // Read from the server
client.hget("person:2", "name"); // Read from the cache
Cache myCache = client.getCache();
myCache.deleteByRedisKey("person:1");
client.hget("person:1", "name"); // Read from the server
client.hget("person:1", "name"); // Read from the cache
client.hget("person:2", "name"); // Still read from the cache
You can also clear all cached items using the flush()
method:
client.hget("person:1", "name"); // Read from the server
client.hget("person:1", "name"); // Read from the cache
client.hget("person:2", "name"); // Read from the server
client.hget("person:2", "name"); // Read from the cache
Cache myCache = client.getCache();
myCache.flush();
client.hget("person:1", "name"); // Read from the server
client.hget("person:1", "name"); // Read from the cache
client.hget("person:2", "name"); // Read from the server
client.hget("person:2", "name"); // Read from the cache
The client will also flush the cache automatically if any connection (including one from a connection pool) is disconnected.
Configure a connection pool
A connection pool maintains several open connections and reuses them efficiently. When you open a connection from a pool, the pool allocates one of its open connections. When you subsequently close the same connection, it is not actually closed but simply returned to the pool for reuse. This avoids the overhead of repeated connecting and disconnecting. See Connection pools and multiplexing for more information.
From Jedis 7.2.0, the RedisClient class provides connection pooling automatically,
but it's important to configure the connection pool correctly to get the best performance.
Each pool holds a specified number of connections, creates more connections when necessary, and terminates them when they are no longer needed.
Here is a simplified connection lifecycle in a pool:
- A connection is requested from the pool.
- A connection is served:
- An idle connection is served when non-active connections are available, or
- A new connection is created when the number of connections is under
maxTotal.
- The connection becomes active.
- The connection is released back to the pool.
- The connection is marked as stale.
- The connection is kept idle for
minEvictableIdleTime. - The connection becomes evictable if the number of connections is greater than
minIdle. - The connection is ready to be closed.
Pass a ConnectionPoolConfig object to the RedisClient builder to configure the pool.
(ConnectionPoolConfig is a wrapper around GenericObjectPoolConfig from Apache Commons Pool2).
ConnectionPoolConfig poolConfig = new ConnectionPoolConfig();
// maximum active connections in the pool,
// tune this according to your needs and application type
// default is 8
poolConfig.setMaxTotal(8);
// maximum idle connections in the pool, default is 8
poolConfig.setMaxIdle(8);
// minimum idle connections in the pool, default 0
poolConfig.setMinIdle(0);
// Enables waiting for a connection to become available.
poolConfig.setBlockWhenExhausted(true);
// The maximum number of seconds to wait for a connection to become available
poolConfig.setMaxWait(Duration.ofSeconds(1));
// Enables sending a PING command periodically while the connection is idle.
poolConfig.setTestWhileIdle(true);
// controls the period between checks for idle connections in the pool
poolConfig.setTimeBetweenEvictionRuns(Duration.ofSeconds(1));
RedisClient jedis = RedisClient.builder()
.hostAndPort("localhost", 6379)
.poolConfig(poolConfig)
.build();
Retrying a command after a connection failure
If a connection is lost before a command is completed, the command will fail with a JedisConnectionException. Although the connection pool manages the connections
for you, you must request a new connection from the pool to retry the command.
You would typically do this in a loop that makes several attempts to reconnect
before aborting and reporting that the error isn't transient. The example below
shows a retry loop that uses a simple
exponential backoff
strategy:
RedisClient jedis = RedisClient.builder()
.hostAndPort("localhost", 6379)
.poolConfig(poolConfig)
.clientConfig(config)
.build();
// Set max retry attempts
final int MAX_RETRIES = 5;
// Example of retrying a command
String key = "retry-example";
String value = "success";
int attempts = 0;
boolean success = false;
while (!success && attempts < MAX_RETRIES) {
try {
attempts++;
String result = jedis.set(key, value);
System.out.println("Command succeeded on attempt " + attempts + ": " + result);
success = true;
} catch (JedisConnectionException e) {
System.out.println("Connection failed on attempt " + attempts + ": " + e.getMessage());
if (attempts >= MAX_RETRIES) {
System.out.println("Max retries reached. Giving up.");
throw e;
}
// Wait before retrying
try {
Thread.sleep(500 * attempts); // Exponential backoff
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
}