Use Dynamic Batching

Learn about a batching technique called dynamic batching.

Dynamic batching

Static batching is great when we know how to group our messages in advance. However, often messages contain dynamic data, which means that static batching is not going to work. Thankfully, Broadway gives us the option to define batches at runtime using :batch_key. This is also called dynamic batching.

To use dynamic batching, we set :batch_key on the message using Broadway.Message.put_batch_key/2 in our handle_message/3 callback. The batch key then partitions the data within each batch for each batcher group.

We can demonstrate how dynamic batching works by improving our email notifications. Right now, if a user makes multiple bookings, they get multiple emails. This won’t be very customer-friendly and will also increase our bill from the email service provider.

Batch ticket confirmations

Instead, let’s batch ticket confirmations and send a single email to the customer. We group notifications by email address and prevent sending too many emails at once by introducing a ten-second window.

To accomplish this, we create another pipeline called NotificationsPipeline. We create the notifications_pipeline.ex file in the lib directory and add the following logic:

Get hands-on with 1200+ tech skills courses.