Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Creating Distributed Java Applications With Redis

DZone's Guide to

Creating Distributed Java Applications With Redis

If you make distributed systems with Java, Redisson can provide you with an in-memory data grid. See how Redisson and JDK's implementations match up.

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

How can we scale a Java application with minimal effort?

As we know, almost every multithreaded Java application use classes from the java.util and java.util.concurrent packages. So, it would be easier to scale the application if we have a distributed implementations of the equivalent classes. The benefit of such an approach is allowing the use of the well-known Java API, and therefore changes required in existing code would be minimal.

Redisson is a Redis-based in-memory data grid for Java. It offers implementations of the most important Java classes required for distributed applications.

Below is a list of common Java classes along with their corresponding distributed implementations based on Redis for each of them:

Distributed Locks and Synchronizers

JDK API
Redisson implementation

java.util.concurrent.locks.Lock

org.redisson.api.RLock

java.util.concurrent.locks.ReadWriteLock

org.redisson.api.RReadWriteLock

java.util.concurrent.CountDownLatch

org.redisson.api.RCountDownLatch

java.util.concurrent.Semaphore

org.redisson.api.RSemaphore


Distributed Collections

JDK API

Redisson implementation

java.util.Set

org.redisson.api.RSet

java.util.concurrent.ConcurrentMap

org.redisson.api.RMap

java.util.Queue

org.redisson.api.RQueue, 

org.redisson.api.RPriorityQueue, 

org.redisson.api.RDelayedQueue

java.util.Deque

org.redisson.api.RDeque,

org.redisson.api.RPriorityDeque

java.util.concurrent.BlockingQueue

org.redisson.api.RBlockingQueue

java.util.concurrent.BlockingDeque

org.redisson.api.RBlockingDeque


Distributed Services

JDK API

Redisson implementation

Java RMI

org.redisson.api.RRemoteService

java.util.concurrent.ExecutorService

org.redisson.api.RExecutorService

java.util.concurrent.ScheduledExecutorService


org.redisson.api.RScheduledExecutorService


Distributed Objects

JDK API

Redisson implementation

java.util.concurrent.atomic.AtomicReference

org.redisson.api.RBucket

java.util.concurrent.atomic.AtomicLong

org.redisson.api.RAtomicLong

java.util.BitSet

org.redisson.api.RBitSet

byte[], java.io.InputStream, java.io.OutputStream


org.redisson.api.RBinaryStream


Redisson supports several types of Redis connections.

  1. Replicated nodes: automatic master server change discovery (also supports AWS ElastiCache and Azure Redis Cache).

  2. Cluster nodes: automatic servers discovery, status and topology update (also supports AWS ElastiCache Cluster and Azure Redis Cache).

  3. Sentinel nodes: automatic server discovery and status update.

  4. Master with Slave nodes.

  5. Single node.

    Redisson developers maintain two version lines:
    2.x.x - supports JDK 1.6, 1.7, 1.8 and Android
    3.x.x - supports JDK 1.8.

Quickstart

1. Define the dependency

Maven

<dependency>
   <groupId>org.redisson</groupId>
   <artifactId>redisson</artifactId>
   <version>3.3.1</version>
</dependency>  


Gradle

compile 'org.redisson:redisson:3.3.1'  


2. Create a config object

// from JSON
Config config = Config.fromJSON(...)
// from YAML
Config config = Config.fromYAML(...)
// or dynamically
Config config = new Config();
config.useClusterServers()


3. Create a Redisson instance

RedissonClient redisson = Redisson.create(config);


4. Get the object you need

RMap<MyKey, MyValue> map = redisson.getMap("myMap");

RLock lock = redisson.getLock("myLock");

RExecutorService executor = redisson.getExecutorService("myExecutorService");

// plus over 30 different objects and services ...


And there you have it! All taken care of.

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:
java ,distributed applications ,redisson ,in-memory data grid ,tutorial

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}