Over a million developers have joined DZone.

RPC With Redis-Based Java Remote Services

DZone 's Guide to

RPC With Redis-Based Java Remote Services

Learn more about remote procedure call with Redis-based Java remote services.

· Integration Zone ·
Free Resource

What Is RPC?

RPC (Remote Procedure Call) is a technique in distributed computing that involves making a client request in the form of a procedure call to a program running on a remote server.

RPC allows clients and servers to communicate without having to worry about the details of the network or the remote interaction. This allows developers to focus on building software, independent of concerns such as hardware, operating systems, and protocols. RPC is an alternative to other client-server architectures such as REST (representational state transfer).

Java Remote Services With Redis and Redisson

Redis is an in-memory data store that is one of the most popular solutions for implementing a NoSQL database. The Redisson client is a library for Redis that provides access to many common objects and constructions in the Java programming language.

In particular, Redisson allows developers to use Java remote services in order to execute remote procedure calls. The remote interface may have any type of method parameters and any type of return object, providing a true RPC implementation. Redisson stores the method request and the result of the execution inside a Redis database.

The relevant Redisson interface is RRemoteService. There are two types of RRemoteService instances: server-side and client-side.

The server-side instance is used to register Java Remote Service by its interface before it is invoked. Example code for SomeServiceInterface remote interface is below:

RRemoteService remoteService = redisson.getRemoteService();
SomeServiceImpl someServiceImpl = new SomeServiceImpl();
// register remote service before any remote invocation
// can handle only 1 invocation concurrently
remoteService.register(SomeServiceInterface.class, someServiceImpl);
// register remote service able to handle up to 12 invocations concurrently
remoteService.register(SomeServiceInterface.class, someServiceImpl, 12);

Note that the default setting is to handle only 1 concurrent invocation.

The client-side instance is used to invoke a remote method once the object has been registered. Example code is below:

RRemoteService remoteService = redisson.getRemoteService();
SomeServiceInterface service = remoteService.get(SomeServiceInterface.class);
String result = service.doSomeStuff(1L, "secondParam", new AnyParam());

The server-side and client-side instances must use the same remote interface and must be backed by Redisson instances that were created using the same configuration for server connections.

Redisson does not impose any limits as to the number of client and/or server instances.

APIs for RPC With Java Remote Services

You have three API options for performing RPC with Redis and Redisson:

  • Asynchronous

  • Reactive

  • RxJava2

The interface must match the methods in the remote interface. For example, if we have the following remote interface:

public interface RemoteInterface {

    Long someMethod1(Long param1, String param2);

    void someMethod2(MyObject param);

    MyObject someMethod3();


The asynchronous interface must be annotated with @RRemoteAsync and returns an object of the form org.redisson.api.RFuture and must have the following form:

public interface RemoteInterfaceAsync {

      RFuture someMethod1(Long param1, String param2);

      RFuture someMethod2(MyObject param);


RedissonClient redisson = Redisson.create(config);
RRemoteService remoteService = redisson.getRemoteService();
RemoteInterfaceAsync asyncService = remoteService.get(RemoteInterfaceAsync.class);

asyncService.someMethod1(1L, "myparam");

The reactive interface must be annotated with @RRemoteReactive and returns an object of the form reactor.core.publisher.Mono and must have the following form:

public interface RemoteInterfaceReactive {

     Mono someMethod1(Long param1, String param2);

     Mono someMethod2(MyObject param);

RedissonReactiveClient redisson = Redisson.createReactive(config);
RRemoteService remoteService = redisson.getRemoteService();
RemoteInterfaceReactive asyncService = remoteService.get(RemoteInterfaceReactive.class);

asyncService.someMethod1(1L, "myparam");

The RxJava2 interface must be annotated with @RRemoteRx and returns an object of one of three forms: io.reactivex.Completable, io.reactivex.Single, or io.reactivex.Maybe and must have the following form:

public interface RemoteInterfaceRx {

    Single someMethod1(Long param1, String param2);

    Completable someMethod2(MyObject param);

RedissonRxClient redisson = Redisson.createRx(config);
RRemoteService remoteService = redisson.getRemoteService();
RemoteInterfaceReactive asyncService = remoteService.get(RemoteInterfaceRx.class);

asyncService.someMethod1(1L, "myparam");

Let me know your thoughts in the comments. 

java ,redis ,rpc ,integration ,redisson ,rest ,java remote services ,nosql ,tutorial

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}