Redis RPC with FastStream#
RedisBroker provides the powerful capability to perform Remote Procedure Calls (RPC) using Redis. This feature enables you to send a message and await a response, effectively creating a synchronous request-response pattern over the inherently asynchronous Redis messaging system. Below is the guide to set up and utilize the Redis RPC publishing feature with FastStream.
The RPC feature is implemented over Redis Pub/Sub independently of the original subscriber type.
RPC with Redis Overview#
In a traditional publish/subscribe setup, the publishing party sends messages without expecting any direct response from the subscribers. However, with RPC, the publisher sends a message and waits for a response from the subscriber, which can then be used for subsequent operations or processing.
FastStream allows you to define RPC-style communication channels, lists, or streams by using the
RedisBroker's publishing function with the
rpc flag set to
Implementing Redis RPC in FastStream#
To implement Redis RPC with
RedisBroker in FastStream, follow the steps below:
Initiate your FastStream application with RedisBroker
Define subscriber handlers for various Redis data types (e.g., channel, list, stream) that can process incoming messages and return responses.
Send RPC messages through
RedisBrokerand await responses on the correct data type.
After your application has started and the subscribers are ready to receive messages, you can publish messages with the
rpcoption enabled. Additionally, you can set an
rpc_timeoutto decide how long the publisher should wait for a response before timing out.
In this example, we assert that the
msg sent is the same as the response received from the subscriber, demonstrating an operational RPC pattern over three different Redis data types.
Full Example of Redis RPC with FastStream#
Combining all the code snippets above, here is the complete example of how to set up Redis RPC with FastStream
By embracing Redis RPC with FastStream, you can build sophisticated message-based architectures that require direct feedback from message processors. This feature is particularly suitable for cases where immediate processing is necessary or calling functions across different services is essential.