Partitioning of Publish/Subscribe Topics in Valkey and Redis
The publish/subscribe (pub/sub) pattern is a powerful tool for developers of distributed applications. Instead of direct point-to-point communication between distributed components, publishers send messages to a topic, and subscribers to the topic receive the messages.
Pub/sub essentially decouples an application architecture, improving scalability for demanding, high-performance apps like streaming services or social media platforms. Redis and its open-source equivalent, Valkey, are well-known for their blazing performance as pub/sub message brokers.
Scaling With Topic Partitioning
As the number of publishers, subscribers, and messages increases, a single pub/sub channel can become a bottleneck. Topic partitioning solves this problem by splitting a single logical topic into multiple channels and distributing the message load across multiple Valkey or Redis instances.
Redisson PRO: The Only Java Client for Partitioning of Pub/Sub Topics
The problem for Java developers is that they have to implement topic partitioning themselves because most third-party Valkey and Redis clients for Java don’t support partitioning of pub/sub topics. That's an incredibly difficult and time-consuming task. But with Redisson PRO, developers can easily implement partitioning for all three types: topic, sharded topic, and reliable topic.
Partitioning With Redisson PRO
Redisson PRO is the only Java client for Valkey and Redis that supports partitioning of publish/subscribe topics, saving developers the headache of implementing it themselves. Redisson PRO automatically distributes messages across different partitions, ensuring even distribution of the message load.
With simple Java methods for all three types of topics, Redisson PRO simplifies the implementation of pub/sub in demanding clustered applications. Here is a look at the Java objects for pub/sub topics implemented in Redisson, and how to use the partitioning features that only Redisson PRO offers.
Topic Partitioning
Redisson PRO’s RTopic
object implements pub/sub for Redis or Valkey, allowing developers to subscribe to events published with multiple instances of the object with the same name. Listeners are re-subscribed automatically after reconnection or failover.
Here’s a code sample showing RTopic
’s ease of use:
RTopic topic = redisson.getTopic("myTopic"); int listenerId = topic.addListener(SomeObject.class, new MessageListener() { @Override public void onMessage(CharSequence channel, SomeObject message) { //... } }); // in other thread or JVM RTopic topic = redisson.getTopic("myTopic"); long clientsReceivedMessage = topic.publish(new SomeObject());
Topic partitioning is just as easy with the RClusteredTopic object, as in this code sample:
RClusteredTopic topic = redisson.getClusteredTopic("myTopic"); int listenerId = topic.addListener(MyObject.class, new MessageListener() { @Override public void onMessage(CharSequence channel, MyObject message) { //... } }); // in other thread or JVM RClusteredTopic topic = redisson.getClusteredTopic("myTopic"); long clientsReceivedMessage = topic.publish(new MyObject());
Sharded Topic Partitioning
Redisson PRO also supports sharded topics with the RShardedTopic
object. Messages published via RShardedTopic
aren’t broadcast across all Valkey or Redis nodes as with RTopic
. This reduces the network bandwidth used between Valkey or Redis nodes and each node's CPU load.
Here’s a code sample showing how to use RShardedTopic:
RShardedTopic topic = redisson.getShardedTopic("myTopic"); int listenerId = topic.addListener(SomeObject.class, new MessageListener() { @Override public void onMessage(CharSequence channel, SomeObject message) { //... } }); // in other thread or JVM RShardedTopic topic = redisson.getShardedTopic("myTopic"); long clientsReceivedMessage = topic.publish(new SomeObject());
Sharded topic partitioning is very similar to standard topic partitioning:
RClusteredTopic topic = redisson.getClusteredTopic("myTopic"); int listenerId = topic.addListener(MyObject.class, new MessageListener() { @Override public void onMessage(CharSequence channel, MyObject message) { //... } }); // in other thread or JVM RClusteredTopic topic = redisson.getClusteredTopic("myTopic"); long clientsReceivedMessage = topic.publish(new MyObject());
Reliable Topic Partitioning
When developers must ensure message delivery to distributed topics, there is Redisson’s RReliableTopic
object. Each RReliableTopic
instance (i.e., subscriber) has its own watchdog, which is started when the first listener is registered. However, using the object is just as easy as with a standard topic or sharded topic:
RReliableTopic topic = redisson.getReliableTopic("anyTopic"); topic.addListener(SomeObject.class, new MessageListener() { @Override public void onMessage(CharSequence channel, SomeObject message) { //... } }); // in other thread or JVM RReliableTopic topic = redisson.getReliableTopic("anyTopic"); long subscribersReceivedMessage = topic.publish(new SomeObject());
And here is how you set up reliable topic partitioning with the RClusteredReliableTopic
object:
RClusteredReliableTopic topic = redisson.getClusteredReliableTopic("myTopic"); int listenerId = topic.addListener(MyObject.class, new MessageListener() { @Override public void onMessage(CharSequence channel, MyObject message) { //... } }); // in other thread or JVM RClusteredReliableTopic topic = redisson.getClusteredReliableTopic("myTopic"); long clientsReceivedMessage = topic.publish(new MyObject());
Redisson PRO: The Only Choice for Partitioning of Pub/Sub Topics in Valkey and Redis
Partitioning improves pub/sub scalability by distributing the message load across multiple Redis or Valkey instances, allowing distributed applications to handle a higher volume of messages. With support for topics, sharded topics, and reliable topic implementations, developers can focus on application logic instead of writing code around complex partitioning schemes.
Redisson PRO is the only third-party Java client that implements partitioning of publish/subscribe topics in Valkey and Redis with a powerful yet easy-to-use collection of Java methods. To learn more, visit the Redisson PRO website today.