Another neat little library: MongolianBarbecue


Another neat little library: MongolianBarbecue

The process of building larger software projects holds many small opportunities for separating out small utilities and helper libraries, and one such opportunity has again arisen πŸ™‚

This time it’s a message queue implementation based on MongoDB: MongolianBarbecue (sounds a little bit like something with “mongo” and “queue”…. πŸ€“)

It’s of course not meant to be able to replace real message brokers and queueing systems, but if you’re in a tight spot, and all you have is MongoDB, then it might just save the day! 🀠

Using it is easy – you can configure it like this:

var config = new Config(
    connectionString: "mongodb://MONGOBONGO01/Readme", 
    collectionName: "messages"

and then you send a message to a queue named my-queue like this:

var producer = config.CreateProducer();

var payload = Encoding.UTF8.GetBytes("Hej med dig, min ven!");
var message = new Message(payload);

await producer.SendAsync("my-queue", message);

When it’s time to receive a message, you create a consumer for a specific queue like this:

var consumer = config.CreateConsumer("my-queue");

and then you get the next message like this:

var messageOrNull = await consumer.GetNextAsync();

// do stuff with the message if it's not null

MongolianBarbecue uses lease-based locking of messages, so it’s safe to use in any kind of distributed scenario you can come up with (e.g. like competing consumers). Moreover, it is pretty easy to use it to implement distributed locking, which can be used to implement exclusive access to a distributed resource.

Neat! 😎

About the author

Mogens administrator

Author of Rebus

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.