Over a million developers have joined DZone.

Java Remote Method Invocation with Redisson

This article shows how you can use a Redis-based in-memory data grid for Java remote method invocations.

· Java Zone

Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience, brought to you in partnership with JetBrains


Remote methods invocations are always been a hot topic for discussion in the Java world. What can Redisson framework have to offer to solve this task?

Usually remote method invocation implies the existence of a client side (invokes remote method) and server side (executes remote method).

Let's assume YourServiceImpl class contains a method you need to invoke remotely and implements YourService interface.

YourServiceImpl object should be registered in Redisson via RRemoteService object:

YourService yourService = new YourServiceImpl();

RRemoteService remoteService = redisson.getRemoteService();
remoteService.register(YourService.class, yourService);

The service can be registered:

  1. On a different node.

    on a different node

  2. Or in a different JVM on the same node with an independent client-side Redisson instance.

    Image title

  3. Or even on the same JVM with a shared client-side Redisson instance.

    Image title

To invoke methods remotely, only the service interface is required:

RRemoteService remoteService = redisson.getRemoteService();
YourService service = remoteService.get(YourService.class);

MyObject result = service.myMethod(someParam1, someParam2);

A method can be invoked: (use the same illustrations as above)

  1. From a different node.

  2. Or another JVM on same node with server side Redisson instance.

  3. Or even from same JVM sharding server side Redisson instance.

Services implementations need to be POJOs, method parameters, and result object type can literally be anything. There is no limit to the amount of client side and/or server side instances.

Remote invocations executes in parallel mode if more than one server-side workers are available.
The number of server side workers for each service is defined during registration:

RRemoteService remoteService = redisson.getRemoteService();

// can handle only 1 invocation concurrently
remoteService.register(YourService.class, yourService);

// able to handle up to 12 invocations concurrently
remoteService.register(SomeServiceInterface.class, someServiceImpl, 12);

The total number of parallel executors can be calculated as follows:

W = R1 + R2 + R3 + ... + Rn

W - total available parallel executors (workers).
R1 - executors amount defined during service registration on the first Redisson instance.
R2 - executors amount defined during service registration on the second Redisson instance.
R3 - executors amount defined during service registration on the third Redisson instance.
Rn - executors amount defined during service registration on the Nth Redisson instance.

Commands exceeding this W-number of free executors will be queued for the next available executor. Remote invocations execute in sequential mode if there is only 1 workers are available.

Invocation Options

There are twos timeout that can be defined during invocation: ack-timeout and result-timeout.
Ack-timeout is used to determine if the method executor has received a request.

RemoteService offers a few options for each remote invocation via the org.redisson.core.RemoteInvocationOptions object. These options provide a way of changing timeouts; they can also be used to skip ack-response and/or result-response. Examples:

// 1 second ack timeout and 30 seconds execution timeout
RemoteInvocationOptions options = RemoteInvocationOptions.defaults();

// no ack but 30 seconds execution timeout
RemoteInvocationOptions options = RemoteInvocationOptions.defaults().noAck();

// 1 second ack timeout then forget the result
RemoteInvocationOptions options = RemoteInvocationOptions.defaults().noResult();

// 1 minute ack timeout then forget about the result
RemoteInvocationOptions options = RemoteInvocationOptions.defaults().expectAckWithin(1, TimeUnit.MINUTES).noResult();

// no ack and forget about the result (fire and forget)
RemoteInvocationOptions options = RemoteInvocationOptions.defaults().noAck().noResult();

RRemoteService remoteService = redisson.getRemoteService();
YourService service = remoteService.get(YourService.class, options);

Asynchronous Calls

Remote invocations can be made in an asynchronous manner via a separate interface marked with the @RRemoteAsync annotation. The method signature needs to be the same as the corresponding method in the remote interface. Each method should return the org.redisson.core.RFuture object, which extends java.util.concurrent.Future and java.util.concurrent.CompletionStage interfaces. The latter provides a few useful methods. Asynchronous interface validation will be performed during the RRemoteService.get method invocation. It's not necessary to define all of the original methods, only those which need to be called in an asynchronous manner.

public interface YourService {
    Long someMethod1(Long param1, String param2);
    void someMethod2(MyObject param);
    MyObject someMethod3();

// async interface for YourService
public interface YourServiceAsync {
    RFuture<Long> someMethod1(Long param1, String param2);
    RFuture<Void> someMethod2(MyObject param);

RRemoteService remoteService = redisson.getRemoteService();
YourServiceAsync asyncService = remoteService.get(YourServiceAsync.class);

RFuture<Long> res = asyncService.someMethod1(12L, "param");
res.thenApply(r -> {

Data Serialization

Redisson uses codec to serialize arguments and result objects transfered during method invocation. Many popular codecs are available for usage:

Codec class name



Jackson JSON codec


CBOR binary json codec


MsgPack binary json codec


Kryo binary codec


JDK Serialization codec


FST codec


LZ4 compression codec


Snappy compression codec

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

java,remote method invocation

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

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

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

{{ parent.tldr }}

{{ parent.urlSource.name }}