简体   繁体   中英

Using RabbitMQ (or pub/sub) for variables

I have an event (say activity) which may be active or not at a given time. I'm looking for a way to use RabbitMQ to figure if the event is active or not. I know the use case of RabbitMQ is pub/sub. How do I use RabbitMQ to be able to say if an event is currently active or not. More like a variable which is updated in real-time. I know I can achieve this using Firebase Database; but is there any way I can do it using RabbitMQ? If not RabbitMQ, are there any other suggestions? I cannot use mySQL etc. because that will not be real-time.

The one way this can be done using RabbitMQ - although it's weird - is using expiring/autoDelete queues as records.

You can assert a queue with expires option meaning it is deleted after x milliseconds of disuse. Then you can simply check if the queue exists. If it exists event is active if it doesn't event is not active.

If your event active/inactive state is more dynamic you could also create queues with just autoDelete option and hook some fake consumer to them. Queue will exist as long as the consumer will exists so to remove the queue simply stop the consumer.

I hope the answer makes sense.

RabbitMQ is a message broker, it is not suited for representing state across distributed systems.

For this purpose, I'd rather recommend any storage solution which provides transactions (SQL or NoSQL) as what you really want to ensure is the data is set atomically.

Solutions such as Redis, MongoDB, PostgreSQL are all providing what you need. Cloud providers nowadays offer similar solutions as managed services.

If latency when accessing the state is critical within your application (if this is what you mean with "Real Time"), then you will need to carefully consider your architecture. As the state will be stored in a remote location (be it RabbitMQ, Redis or whatever else), the network latency and it's (un)reliability will be the most important factor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM