or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

authentication.mdclient-side-caching.mdclustering.mdcommands-operations.mdconnection-management.mdcore-clients.mdexceptions.mdindex.mdmodules.mdparameters.mdpubsub.mdtransactions-pipelining.md

index.mddocs/

0

# Jedis Redis Java Client

1

2

Jedis is a blazingly small and sane Redis Java client providing comprehensive access to Redis functionality. It offers multiple deployment patterns including single instance, cluster, sentinel, and sharded configurations with support for connection pooling, pipelining, transactions, and all Redis modules.

3

4

## Package Information

5

6

- **Package Name**: redis.clients:jedis

7

- **Package Type**: maven

8

- **Language**: Java

9

- **Version**: 6.0.0

10

- **Installation**: Add to Maven dependencies: `<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>6.0.0</version></dependency>`

11

12

## Core Imports

13

14

```java

15

import redis.clients.jedis.Jedis;

16

import redis.clients.jedis.JedisPool;

17

import redis.clients.jedis.JedisPoolConfig;

18

import redis.clients.jedis.JedisCluster;

19

import redis.clients.jedis.UnifiedJedis;

20

import redis.clients.jedis.exceptions.JedisException;

21

```

22

23

## Basic Usage

24

25

```java

26

import redis.clients.jedis.Jedis;

27

import redis.clients.jedis.JedisPool;

28

import redis.clients.jedis.JedisPoolConfig;

29

30

// Simple connection

31

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

32

jedis.set("key", "value");

33

String value = jedis.get("key");

34

jedis.close();

35

36

// Connection pooling (recommended for production)

37

JedisPoolConfig poolConfig = new JedisPoolConfig();

38

poolConfig.setMaxTotal(128);

39

JedisPool pool = new JedisPool(poolConfig, "localhost", 6379);

40

41

try (Jedis jedis = pool.getResource()) {

42

jedis.set("key", "value");

43

String value = jedis.get("key");

44

45

// Hash operations

46

jedis.hset("user:1", "name", "John");

47

jedis.hset("user:1", "age", "30");

48

String name = jedis.hget("user:1", "name");

49

}

50

pool.close();

51

```

52

53

## Architecture

54

55

Jedis follows a modular architecture with clear separation between:

56

57

- **Core Clients**: Main client classes for different deployment patterns

58

- **Connection Management**: Pooling, factories, and connection lifecycle

59

- **Command Interfaces**: Organized by Redis data types and operations

60

- **Parameters**: Type-safe configuration objects for Redis commands

61

- **Response Types**: Structured objects for Redis responses

62

- **Module Support**: Full integration with Redis modules (Search, JSON, Bloom, TimeSeries)

63

- **Exception Handling**: Comprehensive exception hierarchy for error management

64

65

## Capabilities

66

67

### Core Client Classes

68

69

Primary client implementations for different Redis deployment patterns.

70

71

```java { .api }

72

// Single Redis instance client

73

public class Jedis implements JedisCommands, JedisBinaryCommands, AutoCloseable {

74

public Jedis(String host, int port);

75

public Jedis(String host, int port, JedisClientConfig config);

76

public String set(String key, String value);

77

public String get(String key);

78

public Long del(String... keys);

79

public void close();

80

}

81

82

// Unified client for any deployment type

83

public class UnifiedJedis implements JedisCommands, JedisBinaryCommands, AutoCloseable {

84

public UnifiedJedis(JedisClientConfig config, ConnectionProvider connectionProvider);

85

public UnifiedJedis(String host, int port);

86

}

87

88

// Redis Cluster client

89

public class JedisCluster extends UnifiedJedis {

90

public JedisCluster(Set<HostAndPort> nodes);

91

public JedisCluster(Set<HostAndPort> nodes, JedisClientConfig config);

92

public JedisCluster(Set<HostAndPort> nodes, int connectionTimeout, int soTimeout);

93

}

94

95

// Connection pool for single instance

96

public class JedisPool implements Pool<Jedis>, Closeable {

97

public JedisPool(JedisPoolConfig poolConfig, String host, int port);

98

public JedisPool(JedisPoolConfig poolConfig, String host, int port, JedisClientConfig clientConfig);

99

public Jedis getResource();

100

public void close();

101

}

102

103

// Easier connection management with automatic pooling

104

public class JedisPooled extends UnifiedJedis implements JedisCommands, JedisBinaryCommands {

105

public JedisPooled();

106

public JedisPooled(String host, int port);

107

public JedisPooled(HostAndPort hostAndPort, JedisClientConfig clientConfig);

108

public JedisPooled(GenericObjectPoolConfig<Connection> poolConfig, String host, int port);

109

}

110

```

111

112

[Core Client Classes](./core-clients.md)

113

114

### Connection Management

115

116

Connection pooling, factories, and lifecycle management for optimal performance.

117

118

```java { .api }

119

// Pool configuration

120

public class JedisPoolConfig extends GenericObjectPoolConfig<Jedis> {

121

public void setMaxTotal(int maxTotal);

122

public void setMaxIdle(int maxIdle);

123

public void setMinIdle(int minIdle);

124

public void setTestOnBorrow(boolean testOnBorrow);

125

}

126

127

// Client configuration interface

128

public interface JedisClientConfig {

129

int getConnectionTimeoutMillis();

130

int getSoTimeoutMillis();

131

String getUser();

132

String getPassword();

133

int getDatabase();

134

String getClientName();

135

boolean isSsl();

136

}

137

138

// Server endpoint representation

139

public class HostAndPort {

140

public HostAndPort(String host, int port);

141

public String getHost();

142

public int getPort();

143

}

144

```

145

146

[Connection Management](./connection-management.md)

147

148

### Commands and Operations

149

150

Comprehensive Redis command support organized by data types.

151

152

```java { .api }

153

// String operations

154

public interface StringCommands {

155

String set(String key, String value);

156

String set(String key, String value, SetParams params);

157

String get(String key);

158

String getSet(String key, String value);

159

Long incr(String key);

160

Long incrBy(String key, long increment);

161

Double incrByFloat(String key, double increment);

162

}

163

164

// Hash operations

165

public interface HashCommands {

166

Long hset(String key, String field, String value);

167

Long hset(String key, Map<String, String> hash);

168

String hget(String key, String field);

169

Map<String, String> hgetAll(String key);

170

Long hdel(String key, String... fields);

171

Boolean hexists(String key, String field);

172

}

173

174

// List operations

175

public interface ListCommands {

176

Long lpush(String key, String... strings);

177

Long rpush(String key, String... strings);

178

String lpop(String key);

179

String rpop(String key);

180

List<String> lrange(String key, long start, long stop);

181

Long llen(String key);

182

}

183

```

184

185

[Commands and Operations](./commands-operations.md)

186

187

### Clustering and High Availability

188

189

Redis Cluster support with automatic failover and multi-cluster scenarios.

190

191

```java { .api }

192

// Cluster commands interface

193

public interface ClusterCommands {

194

String clusterNodes();

195

String clusterInfo();

196

List<String> clusterGetKeysInSlot(int slot, int count);

197

Long clusterCountKeysInSlot(int slot);

198

String clusterFailover(ClusterFailoverOption failoverOption);

199

}

200

201

// Sentinel pool for high availability

202

public class JedisSentinelPool extends Pool<Jedis> {

203

public JedisSentinelPool(String masterName, Set<String> sentinels);

204

public JedisSentinelPool(String masterName, Set<String> sentinels, JedisPoolConfig poolConfig);

205

public JedisSentinelPool(String masterName, Set<String> sentinels, JedisClientConfig clientConfig);

206

}

207

208

// Multi-cluster failover configuration

209

public class MultiClusterClientConfig {

210

public static class Builder {

211

public Builder fallbackExceptionList(List<Class<? extends Exception>> fallbackExceptionList);

212

public Builder circuitBreakerSlidingWindowSize(int circuitBreakerSlidingWindowSize);

213

public Builder circuitBreakerFailureRateThreshold(float circuitBreakerFailureRateThreshold);

214

public MultiClusterClientConfig build();

215

}

216

}

217

```

218

219

[Clustering and High Availability](./clustering.md)

220

221

### Pub/Sub Messaging

222

223

Redis publish/subscribe messaging with support for pattern subscriptions.

224

225

```java { .api }

226

// Pub/Sub message handler

227

public abstract class JedisPubSub {

228

public abstract void onMessage(String channel, String message);

229

public abstract void onPMessage(String pattern, String channel, String message);

230

public abstract void onSubscribe(String channel, int subscribedChannels);

231

public abstract void onUnsubscribe(String channel, int subscribedChannels);

232

public void subscribe(String... channels);

233

public void psubscribe(String... patterns);

234

public void unsubscribe(String... channels);

235

public void punsubscribe(String... patterns);

236

}

237

238

// Binary pub/sub for non-text data

239

public abstract class BinaryJedisPubSub {

240

public abstract void onMessage(byte[] channel, byte[] message);

241

public abstract void onPMessage(byte[] pattern, byte[] channel, byte[] message);

242

public void subscribe(byte[]... channels);

243

public void psubscribe(byte[]... patterns);

244

}

245

```

246

247

[Pub/Sub Messaging](./pubsub.md)

248

249

### Transactions and Pipelining

250

251

ACID transactions and command pipelining for performance optimization.

252

253

```java { .api }

254

// Transaction support

255

public class Transaction implements AutoCloseable {

256

public Response<String> set(String key, String value);

257

public Response<String> get(String key);

258

public Response<Long> incr(String key);

259

public List<Object> exec();

260

public String discard();

261

public void close();

262

}

263

264

// Command pipelining

265

public class Pipeline implements AutoCloseable {

266

public Response<String> set(String key, String value);

267

public Response<String> get(String key);

268

public Response<Long> incr(String key);

269

public void sync();

270

public List<Object> syncAndReturnAll();

271

public void close();

272

}

273

274

// Pipeline response wrapper

275

public class Response<T> {

276

public T get();

277

public boolean isSet();

278

}

279

```

280

281

[Transactions and Pipelining](./transactions-pipelining.md)

282

283

### Redis Modules

284

285

Full support for Redis modules including RediSearch, RedisJSON, Bloom filters, and TimeSeries.

286

287

```java { .api }

288

// RediSearch commands

289

public interface RediSearchCommands {

290

String ftCreate(String indexName, IndexOptions indexOptions, Schema schema);

291

SearchResult ftSearch(String indexName, String query);

292

SearchResult ftSearch(String indexName, Query query);

293

AggregationResult ftAggregate(String indexName, AggregationBuilder aggr);

294

}

295

296

// RedisJSON commands

297

public interface RedisJsonCommands {

298

String jsonSet(String key, Path path, Object object);

299

<T> T jsonGet(String key, Class<T> clazz);

300

<T> T jsonGet(String key, Class<T> clazz, Path... paths);

301

Long jsonDel(String key, Path path);

302

}

303

304

// Bloom filter commands

305

public interface BloomFilterCommands {

306

String bfReserve(String key, double errorRate, long capacity);

307

Boolean bfAdd(String key, String item);

308

Boolean bfExists(String key, String item);

309

List<Boolean> bfMAdd(String key, String... items);

310

}

311

```

312

313

[Redis Modules](./modules.md)

314

315

### Parameters and Configuration

316

317

Type-safe parameter objects for configuring Redis commands.

318

319

```java { .api }

320

// SET command parameters

321

public class SetParams implements IParams {

322

public SetParams ex(long secondsToExpire);

323

public SetParams px(long millisecondsToExpire);

324

public SetParams nx();

325

public SetParams xx();

326

public SetParams keepTtl();

327

}

328

329

// SCAN parameters

330

public class ScanParams implements IParams {

331

public ScanParams match(String pattern);

332

public ScanParams count(int count);

333

public ScanParams type(String type);

334

}

335

336

// Sorted set parameters

337

public class ZAddParams implements IParams {

338

public ZAddParams nx();

339

public ZAddParams xx();

340

public ZAddParams ch();

341

public ZAddParams incr();

342

}

343

```

344

345

[Parameters and Configuration](./parameters.md)

346

347

### Exception Handling

348

349

Comprehensive exception hierarchy for robust error handling.

350

351

```java { .api }

352

// Base Jedis exception

353

public class JedisException extends RuntimeException {

354

public JedisException(String message);

355

public JedisException(String message, Throwable cause);

356

}

357

358

// Connection related exceptions

359

public class JedisConnectionException extends JedisException {

360

public JedisConnectionException(String message);

361

public JedisConnectionException(String message, Throwable cause);

362

}

363

364

// Cluster specific exceptions

365

public class JedisClusterException extends JedisException {

366

public JedisClusterException(String message);

367

}

368

369

public class JedisMovedDataException extends JedisRedirectionException {

370

public JedisMovedDataException(String message, HostAndPort targetNode, int slot);

371

public HostAndPort getTargetNode();

372

public int getSlot();

373

}

374

```

375

376

[Exception Handling](./exceptions.md)

377

378

### Client-Side Caching

379

380

High-performance client-side caching support for improved response times and reduced server load.

381

382

```java { .api }

383

// Cache interface for client-side caching

384

public interface Cache {

385

void flush();

386

void flushAll();

387

void invalidate(CacheKey key);

388

Cacheable getValue(CacheKey key);

389

void setValue(CacheKey key, Cacheable value);

390

CacheStats getStats();

391

}

392

393

// Cache configuration

394

public class CacheConfig {

395

public CacheConfig(int maxSize, int ttlSeconds);

396

public CacheConfig maxSize(int maxSize);

397

public CacheConfig ttl(int ttlSeconds);

398

public CacheConfig evictionPolicy(EvictionPolicy evictionPolicy);

399

}

400

401

// Default cache implementation

402

public class DefaultCache implements Cache {

403

public DefaultCache(int maxSize);

404

public DefaultCache(CacheConfig config);

405

}

406

407

// Cache entry wrapper

408

public class CacheEntry {

409

public Object getValue();

410

public long getTimestamp();

411

public boolean isExpired();

412

}

413

```

414

415

[Client-Side Caching](./client-side-caching.md)

416

417

### Authentication

418

419

Token-based authentication and advanced security features.

420

421

```java { .api }

422

// Authentication manager for token-based auth

423

public class AuthXManager {

424

public AuthXManager(RedisCredentialsProvider credentialsProvider);

425

public void authenticate(Connection connection);

426

public void refreshToken();

427

}

428

429

// Token credentials

430

public class TokenCredentials implements RedisCredentials {

431

public TokenCredentials(String token);

432

public String getToken();

433

}

434

435

// Authentication exception

436

public class JedisAuthenticationException extends JedisException {

437

public JedisAuthenticationException(String message);

438

}

439

```

440

441

[Authentication](./authentication.md)

442

443

## Response Types

444

445

```java { .api }

446

// Scan result wrapper

447

public class ScanResult<T> {

448

public String getCursor();

449

public List<T> getResult();

450

}

451

452

// Sorted set member with score

453

public class Tuple {

454

public String getElement();

455

public double getScore();

456

}

457

458

// Stream entry

459

public class StreamEntry {

460

public StreamEntryID getID();

461

public Map<String, String> getFields();

462

}

463

464

// Geospatial response

465

public class GeoRadiusResponse {

466

public String getMember();

467

public double getDistance();

468

public GeoCoordinate getCoordinate();

469

public long getHash();

470

}

471

```

472

473

## Key Features

474

475

- **Multiple Deployment Patterns**: Single instance, cluster, sentinel, and sharded configurations

476

- **Connection Pooling**: Production-ready connection pool management with Apache Commons Pool

477

- **Full Redis Support**: All Redis commands and data types with type-safe APIs

478

- **Module Integration**: Native support for RediSearch, RedisJSON, Bloom filters, and TimeSeries

479

- **Performance Optimization**: Pipelining and transaction support for high-throughput scenarios

480

- **High Availability**: Automatic failover with Redis Sentinel and Cluster

481

- **Client-Side Caching**: Built-in caching support with configurable eviction policies and TTL

482

- **Token-Based Authentication**: Advanced authentication with AuthX support including Microsoft EntraID

483

- **SSL/TLS Support**: Secure connections with certificate validation

484

- **Authentication**: Support for Redis ACL, legacy AUTH commands, and token-based authentication

485

- **Monitoring**: Integration with Redis MONITOR and slow log