Redis Client (Jedis) for Java: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Jedis and why is it needed?

\n\n

Jedis is a minimalist, lightweight, and very popular Java library for working with Redis (Remote Dictionary Server). Redis is a high-performance in-memory data store that is often used as a database, cache, and message broker. Jedis provides a simple and intuitive API for interacting with a Redis server from Java applications.

\n\n

Why is Jedis needed? If your Java application uses Redis for caching, session management, task queues, or storing temporary data, Jedis is the most direct and efficient way to connect to Redis. It supports all major Redis data structures (strings, lists, sets, hashes, sorted sets), as well as features such as Pub/Sub, transactions, and Lua scripts.

\n\n

Jedis is known for its simplicity and lack of unnecessary dependencies. Unlike heavier clients (e.g., Lettuce), Jedis does not require connection pool configuration out of the box (although it supports it), making it an ideal choice for small and medium-sized projects where simplicity and development speed are important.

\n\n

Installing Jedis

\n\n

Jedis is distributed via Maven Central. To add the library to your project, you need to add a dependency to the pom.xml file (for Maven) or build.gradle (for Gradle).

\n\n

Installation via Maven

\n

Add the following block to your pom.xml:

\n
<dependency>\n    <groupId>redis.clients</groupId>\n    <artifactId>jedis</artifactId>\n    <version>5.1.0</version> <!-- Use the latest stable version -->\n</dependency>
\n\n

Installation via Gradle

\n

Add the line to your build.gradle:

\n
implementation 'redis.clients:jedis:5.1.0'
\n\n

Important: Version 5.x is the latest major update of Jedis, which includes support for Redis 7.x and new features. It is recommended to always use the latest stable version to get all fixes and new features.

\n\n

Quick start with a code example

\n\n

Let's write a simple program that connects to Redis, saves a value, and reads it back.

\n\n

Assume the Redis server is running locally on the default port 6379.

\n\n
import redis.clients.jedis.Jedis;\n\npublic class JedisQuickStart {\n    public static void main(String[] args) {\n        // 1. Create a connection to Redis\n        // Default is localhost:6379\n        try (Jedis jedis = new Jedis("localhost", 6379)) {\n            \n            // 2. Check the connection (PING)\n            String response = jedis.ping();\n            System.out.println("Ping response: " + response); // Should be "PONG"\n\n            // 3. Save a value\n            jedis.set("user:1000:name", "Alice");\n            jedis.set("user:1000:age", "30");\n\n            // 4. Read a value\n            String name = jedis.get("user:1000:name");\n            String age = jedis.get("user:1000:age");\n\n            System.out.println("User name: " + name);\n            System.out.println("User age: " + age);\n\n            // 5. Check if a key exists\n            boolean exists = jedis.exists("user:1000:name");\n            System.out.println("Key exists: " + exists);\n\n            // 6. Delete a key\n            long deleted = jedis.del("user:1000:age");\n            System.out.println("Deleted keys: " + deleted);\n        }\n    }\n}
\n\n

This example demonstrates basic operations: connection, writing, reading, checking existence, and deletion. Note the use of try-with-resources — this ensures the connection is closed after use.

\n\n

Main Jedis methods

\n\n

Jedis provides hundreds of methods covering all Redis capabilities. Below are the 10 most commonly us

Recommendations