RabbitMQ
Binary Files
Message Queuing
Data Transmission
Software Development

Sending a binary file through RabbitMQ (file transfer with pika)

Codemia Knowledge Hub ·

To send a binary file through RabbitMQ, read the file as bytes and publish those bytes as the message body. The consumer receives the same bytes and writes them back to a file. This works for images, documents or archives, but RabbitMQ is not designed to move very large files efficiently, so keep messages small or split files into chunks.

Message structure

A RabbitMQ message has two parts:

  • Header: metadata such as routing and delivery properties.
  • Body: the payload, which can hold binary data.

Publishing a file

  1. Read the file as binary data, typically into a byte array.
  2. Put the bytes in the message body.
  3. Publish to an exchange with a routing key that determines where the message goes.

A Python example using the pika client library:

python
1import pika
2import os
3
4# Establish a connection to RabbitMQ server
5connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
6channel = connection.channel()
7
8# Declare the exchange
9channel.exchange_declare(exchange='binary_exchange', exchange_type='direct')
10
11# Reading the binary file
12file_path = 'path/to/your/file.pdf'
13with open(file_path, 'rb') as file:
14    binary_data = file.read()
15
16# Publishing the message
17channel.basic_publish(exchange='binary_exchange',
18                      routing_key='binary',
19                      body=binary_data)
20
21print(" [x] Sent 'Binary file'")
22connection.close()

Receiving the file

  1. Set up a queue bound to the exchange used for the files.
  2. Consume messages and write each body to disk.
python
1import pika
2
3# Establish a connection to RabbitMQ server
4connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
5channel = connection.channel()
6
7# Declare the exchange and a queue bound to it
8channel.exchange_declare(exchange='binary_exchange', exchange_type='direct')
9channel.queue_declare(queue='binary_queue', durable=True)
10channel.queue_bind(exchange='binary_exchange', queue='binary_queue', routing_key='binary')
11
12# Setting up the consumer
13def callback(ch, method, properties, body):
14    with open('received_file.pdf', 'wb') as file:
15        file.write(body)
16    print(" [x] Received binary file")
17
18channel.basic_consume(queue='binary_queue', on_message_callback=callback, auto_ack=True)
19
20print(' [*] Waiting for messages. To exit press CTRL+C')
21channel.start_consuming()

Size, integrity and performance

  • Message size limits: RabbitMQ does not handle very large files efficiently, and most deployments restrict message sizes to a few MB.
  • Data integrity: validate the received data with checksums or hashes to confirm it was not corrupted.
  • Performance: large files can slow down both the broker and the network.
ConsiderationDetail Impact or Mitigation Strategy
Message Size LimitsPreferably under several MBs; break larger files into smaller chunks if necessary.
Data IntegrityUse checksums or hashes to ensure the integrity of received data.
PerformanceEvaluate the impact on RabbitMQ and network performance and adjust system configuration as necessary.

Compression, chunking and correlation IDs

  • Compress files before sending to reduce message size.
  • Chunk large files into several smaller messages.
  • Use messaging patterns such as message sequences or correlation IDs for more complex transfers.

Test the setup under realistic file sizes before relying on it.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design