Using Jedis: Integrate the Jedis Client for Synchronous Operations with Redis in Spring Boot
If you’re building high-performance applications in Java, Redis is likely on your radar. It’s a blazing fast in-memory key-value store, perfect for caching, session management, and pub-sub systems. While Spring Boot offers built-in support via Spring Data Redis, you might prefer Jedis — a simple, lightweight Redis client — for synchronous operations.
In this tutorial, you'll learn how to integrate Jedis with Spring Boot, configure it properly, and build a simple REST API to interact with Redis.
易 What Is Jedis?
Jedis is a Java client library for Redis that offers a synchronous, blocking API. It is ideal when you want direct control over Redis operations without the overhead of reactive programming or extra abstractions.
⚒ Prerequisites
- Java 17+
- Spring Boot 3.x
- Redis installed locally or via Docker
- Maven or Gradle
️ Step 1: Add Jedis to Your Project
Maven
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>5.1.0</version>
</dependency>
Gradle
implementation 'redis.clients:jedis:5.1.0'
⚙️ Step 2: Configure Jedis in Spring Boot
@Configuration
public class JedisConfig {
@Value("${spring.redis.host}")
private String redisHost;
@Value("${spring.redis.port}")
private int redisPort;
@Bean
public Jedis jedis() {
return new Jedis(redisHost, redisPort);
}
}
spring.redis.host=localhost
spring.redis.port=6379
Step 3: Create a Redis Service Layer
@Service
public class RedisService {
private final Jedis jedis;
public RedisService(Jedis jedis) {
this.jedis = jedis;
}
public void save(String key, String value) {
jedis.set(key, value);
}
public String get(String key) {
return jedis.get(key);
}
public void delete(String key) {
jedis.del(key);
}
}
Step 4: Build a REST Controller
@RestController
@RequestMapping("/api/redis")
public class RedisController {
private final RedisService redisService;
public RedisController(RedisService redisService) {
this.redisService = redisService;
}
@PostMapping
public ResponseEntity<String> saveData(@RequestParam String key, @RequestParam String value) {
redisService.save(key, value);
return ResponseEntity.ok("Saved successfully");
}
@GetMapping
public ResponseEntity<String> getData(@RequestParam String key) {
String value = redisService.get(key);
return value != null ? ResponseEntity.ok(value) : ResponseEntity.notFound().build();
}
@DeleteMapping
public ResponseEntity<String> deleteData(@RequestParam String key) {
redisService.delete(key);
return ResponseEntity.ok("Deleted successfully");
}
}
離 Testing It Out
With your Spring Boot application running, try these endpoints using curl
or Postman:
- POST
/api/redis?key=test&value=hello
- GET
/api/redis?key=test
- DELETE
/api/redis?key=test
Why Choose Jedis?
- ✅ Simple, blocking API
- ✅ Lightweight and fast
- ✅ No reactive complexity
- ✅ Perfect for quick caching and session scenarios
⚠️ Considerations
Jedis is not thread-safe by default when using a single instance. If you plan to scale this in a multi-threaded environment, consider using JedisPool
, or explore Lettuce or Redisson for more advanced use cases.
Conclusion
Integrating Jedis with Spring Boot is straightforward and gives you full control over Redis commands in a synchronous manner. Whether you're building a caching layer or a simple session store, Jedis provides the performance and simplicity many applications need.
Start small, keep it fast, and scale as you go!
Resources
Keywords for SEO
Spring Boot Redis
, Jedis Spring Boot
, Java Redis Integration
, Jedis Redis Example
, Synchronous Redis with Spring
, Spring Boot Jedis Client
, Redis Java Tutorial