Middle
What is a message broker and what is it used for?
sobes.tech AI
Answer from AI
A message broker is a software component that facilitates the transfer of messages between different applications or services in distributed systems. It acts as an intermediary, receiving messages from senders and delivering them to recipients, often with support for queues, routing, and guaranteed delivery.
Message brokers are used for:
- Asynchronous interaction between system components.
- Offloading services, allowing them not to wait for a response immediately.
- Increasing the reliability and scalability of applications.
- Integrating heterogeneous systems.
Examples of popular message brokers: RabbitMQ, Apache Kafka, ActiveMQ.
Example of usage in Python with RabbitMQ:
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='task_queue')
channel.basic_publish(exchange='', routing_key='task_queue', body='Hello World!')
print("[x] Sent 'Hello World!'")
connection.close()