Junior — Middle
Can you give an example of a situation where you would use RabbitMQ for message processing?
sobes.tech AI
Answer from AI
RabbitMQ is often used for organizing asynchronous task processing and message exchange between services.
Example situation:
A web application receives orders from users. To avoid blocking the user during order processing (e.g., checking stock availability, payment, notifications), the application places the order information into a RabbitMQ queue. A separate service (consumer) reads messages from this queue and performs all necessary order processing operations.
This allows to unload the main application thread, increase scalability, and improve system reliability.
Example code in Python using the pika library:
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='orders')
# Sending a message
channel.basic_publish(exchange='', routing_key='orders', body='Order #123')
connection.close()
The consumer will listen to the 'orders' queue and process incoming messages.