What it is
Redisson is a Java client for Redis that provides a high-level, distributed Java data structures framework. It allows Java applications to interact with Redis using objects, collections, locks, atomic variables, and other advanced features in a thread-safe and cluster-aware manner.
Redisson provides distributed implementations of Java objects like Map, Set, List, Queue, Lock, AtomicLong, and Semaphore. It supports cluster mode, replication, reactive programming, and transaction-like features over Redis.
Installation
Add dependency in pom.xml:
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.21.7</version>
</dependency>Getting started
The smallest useful thing you can do with it, and what each part means.
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
RedissonClient redisson = Redisson.create(config);import org.redisson.api.RMap;
RMap<String, String> map = redisson.getMap("myMap");
map.put("key", "value");
System.out.println(map.get("key"));Advanced usage
Where the library earns its place over a simpler alternative.
import org.redisson.api.RLock;
RLock lock = redisson.getLock("myLock");
lock.lock();
try {
// critical section
} finally {
lock.unlock();
}import org.redisson.api.RAtomicLong;
RAtomicLong atomicLong = redisson.getAtomicLong("counter");
atomicLong.incrementAndGet();
System.out.println(atomicLong.get());import org.redisson.api.RTopic;
RTopic topic = redisson.getTopic("myTopic");
topic.addListener(String.class, (channel, msg) -> System.out.println("Received: " + msg));
topic.publish("Hello Redisson!");import org.redisson.api.RMapReactive;
RMapReactive<String, String> reactiveMap = redisson.getMap("reactiveMap");
reactiveMap.put("key", "value").subscribe();Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- RedisConnectionException
- Occurs when Redisson cannot connect to the Redis server. Check host, port, and network connectivity.
- RedisTimeoutException
- Occurs when a Redis operation exceeds the timeout. Adjust timeout settings or optimize Redis commands.
- IllegalStateException
- Occurs if the client is shut down or configuration is invalid. Ensure proper client lifecycle management.
Best practices
- Use distributed locks and atomic objects for multi-instance applications to avoid race conditions.
- Prefer asynchronous or reactive APIs for high-concurrency scenarios to improve performance.
- Keep Redis data structures optimized to reduce memory usage and latency.
- Use appropriate TTL (time-to-live) settings for cache entries.
- Monitor Redis cluster and Redisson metrics for resource utilization and scaling.
Background
Why it exists, and what it was reacting to.
Redisson was created to simplify the integration of Redis into Java applications, offering a rich set of distributed Java objects and utilities. It supports reactive, asynchronous, and synchronous APIs and is widely used for caching, distributed locking, pub/sub messaging, and managing high-concurrency applications.
