DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • Integrating Redis With Message Brokers
  • Build a Data Analytics Platform With Flask, SQL, and Redis
  • Building a Rust Command Line Interface to Chat With Llama 3.2

Trending

  • FIPS 140-3: The Security Standard That Protects Our Federal Data
  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load
  1. DZone
  2. Software Design and Architecture
  3. Microservices
  4. Asynchronous, Reactive, and RxJava2 Interfaces for Redis

Asynchronous, Reactive, and RxJava2 Interfaces for Redis

Let's explore asynchronous, reactive, and RxJava2 interfaces for Redis.

By 
Nikita Koksharov user avatar
Nikita Koksharov
·
Jul. 18, 19 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
16.8K Views

Join the DZone community and get the full member experience.

Join For Free

Asynchronous, reactive, and RxJava2 are all related programming models in the Java programming language. However, they aren't automatically available in Redis, the open-source software project for implementing an in-memory data store.

The good news is that all three of these programming models are available in Redis using Redisson, a third-party client library that integrates Redis with Java. In this article, we'll discuss each of these three models and how you can use Redisson to deploy them within Redis.

Asynchronous Interface for Redis

Asynchronous programming is a type of parallel programming in which a task runs separately on a different thread than the main application. This allows the application to continue running while the task executes. Once the task is complete, it notifies the main application of its success or failure.

In Redisson, each asynchronous method returns an RFuture object, which represents the result of an asynchronous computation. RFuture implements the java.util.concurrent.Future and java.util.concurrent.CompletionStage interfaces in Java.

Below is an example of how to use RFuture to apply the asynchronous programming model in Redisson:

package redis.demo;

import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RFuture;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;

public class RedisAsyncTest {

    public static void main( String[] args )
    {
        // connects to 127.0.0.1:6379 by default
        RedissonClient redisson = Redisson.create();

        // perform operations

        RBucket<String> bucket = redisson.getBucket("simpleObject");
        RFuture<Void> setFuture = bucket.setAsync("This is object value");
        setFuture.onComplete((value, exception) -> {

            // on invocation completion

        });

        RMap<String, String> map = redisson.getMap("simpleMap");
        RFuture<String> putFuture = map.putAsync("mapKey", "This is map value");
        putFuture.onComplete((value, exception) -> {

            System.out.println("previous value: " + value);

        });

        RFuture<String> getFuture = bucket.getAsync();
        getFuture.onComplete((value, exception) -> {

            System.out.println("stored object value: " + value);

        });

        RFuture<String> getMapFuture = map.getAsync("mapKey");
        getMapFuture.onComplete((value, exception) -> {

            System.out.println("stored map value: " + value);

        });

        redisson.shutdown();
    }

}

Reactive Interface for Redis

The reactive programming model is implemented in the Reactor Core library for Java 8. Reactive Streams is a specification for asynchronous stream processing, in which many different events may be produced and consumed asynchronously. In order to deal with this rapid pace, the system needs to produce events no faster than it is able to consume them.

In Redisson, each reactive method returns a reactor.core.publisher.Mono object, which signals that it has successfully completed the computation by emitting an element.

The following code sample demonstrates how the Mono object is used in Redisson:

package redis.demo;

import org.redisson.Redisson;
import org.redisson.api.RBucketReactive;
import org.redisson.api.RMapReactive;
import org.redisson.api.RedissonReactiveClient;

import reactor.core.publisher.Mono;

public class RedisReactiveTest {

    public static void main( String[] args )
    {
        // connects to 127.0.0.1:6379 by default
        RedissonReactiveClient redisson = Redisson.createReactive();

        // perform operations

        RBucketReactive<String> bucket = redisson.getBucket("simpleObject");
        Mono<Void> setMono = bucket.set("This is object value");
        setMono.subscribe(value -> {

            // on invocation completion

        });

        RMapReactive<String, String> map = redisson.getMap("simpleMap");
        Mono<String> putMono = map.put("mapKey", "This is map value");
        putMono.subscribe(value -> {

            System.out.println("previous value: " + value);

        });

        Mono<String> getMono = bucket.get();
        getMono.subscribe(value -> {

            System.out.println("stored object value: " + value);

        });

        Mono<String> getMapMono = map.get("mapKey");
        getMapMono.subscribe(value -> {

            System.out.println("stored map value: " + value);

        });

        redisson.shutdown();
    }

}

RxJava2 Interface for Redis

RxJava2 is another Java paradigm that implements asynchronous and reactive programming. The key distinction in RxJava2 is between "observables," which are data sources that emit items, and "subscribers" that listen to one or more observable.

In Redisson, each RxJava method returns one of the following observable objects: io.reactivex.Completable, io.reactivex.Maybe, io.reactivex.Flowable, or io.reactivex.Single.

  • Single: an observable that emits only one item or value or throws an error.

  • Maybe: an observable that emits zero or one item or value or throws an error.

  • Completable: an observable that emits when the task is completed or throws an error.

Below is an example of how to use the RxJava2 programming model within Redis and Redisson:

package redis.demo;

import org.redisson.Redisson;
import org.redisson.api.RBucketRx;
import org.redisson.api.RMapRx;
import org.redisson.api.RedissonRxClient;

import io.reactivex.Completable;
import io.reactivex.Maybe;

public class RedisRxTest {

    public static void main( String[] args )
    {
        // connects to 127.0.0.1:6379
        RedissonRxClient redisson = Redisson.createRx();

        // perform operations

        RBucketRx<String> bucket = redisson.getBucket("simpleObject");
        Completable completable = bucket.set("This is object value");
        completable.subscribe(() -> {

            // on invocation completion

        });

        RMapRx<String, String> map = redisson.getMap("simpleMap");
        Maybe<String> putMaybe = map.put("mapKey", "This is map value");
        putMaybe.subscribe(value -> {

            System.out.println("previous value: " + value);

        });

        Maybe<String> getMaybe = bucket.get();
        getMaybe.subscribe(value -> {

            System.out.println("stored object value: " + value);

        });

        Maybe<String> mapGetMaybe = map.get("mapKey");
        mapGetMaybe.subscribe(value -> {

            System.out.println("stored map value: " + value);

        });

        redisson.shutdown();
    }

}

Thank you!

Redis (company) Interface (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • Integrating Redis With Message Brokers
  • Build a Data Analytics Platform With Flask, SQL, and Redis
  • Building a Rust Command Line Interface to Chat With Llama 3.2

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: