Wednesday, July 26, 2023

Rabbit Message Queue and Redis

 In this article I will try to give some overview about why and when to use Rabbit Message Queue and Redis cache. Starting with Redis cache

Redis cache -

  • Mainly used for caching as call time to server ( say database server ) can delay. Using redis cache saves this time, as it is the cache of the database itself.
    • Whenever a database write has happened, a worker class can update the cache to be in sync with the database.
  • Key value pair type of database supporting strings, int, lists, hash as values.
  • The list can used as stacks or queues and other data structures as well like linked list,  circular queue and all.
  • Remember, the key has to be in lower case.
  • Redis say instead of sending many continuous updates in subsequent calls, these can be piped and send as a single call.
  • Calls can be direct or pub - sub type
  • StackExchange.Redis is a popular nuget package to perform almost all the database operations in the redis documentation.
  • redis - cli can be used locally to connect to the database and perform CRUD operations.
  • Convenience of using regex in calls to retrieve data fastly.

 

Rabbit Message Queue ( RMQ ) -

  • Queue or many queues behind an exchange.
  • The exchange does the routing to the particular queue based on the header information.
  • Data in a queue can be set to be durable, meaning data is not lost after RMQ is re started. For docker containers make sure to use common volumes across container restart(s).
  • Data in a durable queue can stay from two to three days.
  • MassTransit is one nuget package which can be used with different types of Enterprise service bus like RMQ, Azure queue, AWS queue, in memory queue and all without needing to write the adapters for each.
  • When a message is sent to a RMQ it first goes to the exchange and the exchange routes that message to its queue.
  • Used mainly for mass data transfers and Mass transit client consumes it almost in no time.
  • The main reason to use Enterprise service bus is to ensure reliability of the message sent. The recipient server may be busy with some operation(s) unable to provide cycles or bandwidth to consume the incoming messages from the client. These messages can be lost if not addressed in a timely manner, thereby giving poor experience to its caller.
  • Supports direct messaging to the recipient or publish subscriber mechanism where the Consumer is listening to the queue(s) in the RMQ for any new messages.

 

Writing Consumer in Mass transit should be as straight forward as implementing the `IConsumer` type and having a task named Consume with ConsumerContext parameter like below.


    public class AfceConsumer :
        IConsumer<AfceMessage>
    {

        public Task Consume(ConsumeContext context)
        {
            Console.WriteLine("Hello {name}", context.Message.Title);
            return Task.CompletedTask;
        }
    }
The above consumer will continuously consume each message one by one in almost no time when the message of type `AfceMessage` is available in the queue.
The AfceMessage can be any reference type like a class or a record or a struct.

Mass transit also has bulk message consumer to consume message in batches.

No comments:

Post a Comment

Browser background color or monitor brightness

Have you been in a situation where you are almost sitting the whole day in front of your browser and going through a document and find that ...