How to Replace RabbitMQ With Valkey or Redis in Spring Cloud Stream Framework

The in-memory data store Redis and its open-source offshoot Valkey are known for their impressive performance as publish/subscribe message brokers. By operating in memory instead of reading from disk, Redis and Valkey offer extremely low latency and high throughput. From real-time chats to streaming media, the pub/sub capabilities of Redis or Valkey are perfect for a wide range of applications.

Whether it’s text chats or streaming video, handling large volumes of data in real time is known as stream processing. Spring Cloud Stream is one of the most popular frameworks for building stream processing applications. Given the blazing-fast performance of Redis and Valkey, either one seems like a natural fit for Spring Cloud Stream. 

However, Spring Cloud Stream lacks native integration with Redis. This has led to developers turning to messaging systems such as RabbitMQ for pub/sub and message delivery. Valkey or Redis can guarantee message delivery, but since Spring Cloud Stream users have to build their own integration, they’ll often default to RabbitMQ. But with Redisson PRO’s special implementation of the pub/sub topic, you can now easily combine the Spring Cloud Stream framework with the Valkey or Redis stream structure.

Spring Cloud Stream and Valkey or Redis: Guaranteed Message Delivery

Redis or Valkey offers several advantages over RabbitMQ and similar alternatives as a message broker. Valkey and Redis can handle enormous data sets with in-memory performance. Redis and Valkey are both highly scalable, as is Spring Cloud Stream, an important consideration as applications grow in popularity.

Before Redisson PRO’s implementation of guaranteed message delivery, Spring Cloud Stream customers felt like they couldn’t switch from RabbitMQ. The Spring Cloud Stream developers haven’t made their own official Valkey or Redis integration yet, and creating such an integration on the client side would be exceptionally challenging. Fortunately, Redisson PRO offers a simple implementation of Spring Cloud Stream so that developers can use Redis or Valkey as the message broker.

Setting Up Spring Cloud Stream With Valkey or Redis as Pub/Sub Message Broker

Redisson PRO implements the Spring Cloud Stream binder, a library that allows developers to bind a Spring Cloud Stream app to a message broker. Therefore, with Redisson PRO, it only takes a few basic steps to set up Spring Cloud Stream with Valkey or Redis:

  1. Add the Spring Cloud Stream binder library to your classpath.
  2. Define a bean for receiving messages that implements the Consumer interface.
  3. In the configuration file application.properties, define a channel ID to specify the channel the consumer bean should listen to.
  4. Define a bean for publishing messages that implements the Supplier interface.
  5. In the configuration file application.properties, define a channel ID to specify the channel to which the supplier bean should publish messages.

Spring Cloud Stream + Valkey or Redis: Code Sample

For example, here’s how you add the Spring Cloud Stream binder library to a classpath using Maven:

<dependency>
   <groupId>pro.redisson</groupId>
   <artifactId>redisson</artifactId>
   <version>3.40.2</version>
</dependency> 

using Gradle:

compile 'pro.redisson:spring-cloud-stream-binder-redisson:3.40.2'

Here is some sample code for registering the input binder to receive messages:

@Bean
public Consumer receiveMessage() {
    return obj -> {
        // consume received object
    };
}

Next, define the channel ID in the configuration file application.properties. To use the example receiveMessage bean as described above, connected to the channel called my-channel:

spring.cloud.stream.bindings.receiveMessage-in-0.destination=my-channel

Now register the output binder for publishing messages as follows:

@Bean
public Supplier feedSupplier() {
    return () -> {
        // ...
        return new MyObject();
    };
}

Again, define the channel ID in the configuration file application.properties. In this example for the feedSupplier bean defined above connected to the my-channel channel:

spring.cloud.stream.bindings.feedSupplier-out-0.destination=my-channel

spring.cloud.stream.bindings.feedSupplier-out-0.producer.useNativeEncoding=true

Redisson PRO: The Solution for Valkey or Redis in Spring Cloud Stream Framework

To learn more about Spring Cloud Stream and Redis or Valkey integration, and other Redisson PRO features, visit the Redisson PRO website.


Similar articles