Share
Embed
ElixirConf 2019 - Build Efficient Data Processing Pipelines in Elixir Using Broadway
Announced in February 2019, Broadway is a new open-source tool developed by Plataformatec that aims to streamline data processing pipelines with Elixir. It allows developers to build complex GenStage topologies that can consume and process data efficiently from different sources, including Amazon SQS and RabbitMQ. In this talk, we'll discuss some of the main concepts behind Broadway, how it leverages OTP to achieve fault-tolerance, sharing implementation details and architectural aspects. You'll learn how to build efficient data processing pipelines and how to optimize them using real-time metrics based on Telemetry events.
Marlus Saraiva
August 30, 2019
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
78 slides
-
Agenda • Why Broadway? • How does it work? •
Implementation details • Fault tolerance • Graceful shutdown • Telemetry integration -
Broadway An open source tool developed by Plataformatec that aims
to streamline data processing pipelines with Elixir -
Data pipelines “A set of data processing elements connected in
series, where the output of one element is the input of the next one” - Wikipedia -
Why Broadway? Many companies are using Elixir to building data
processing pipelines -
Why Broadway? • Reimplementing the same features • Assembling complex
GenStage topologies • Running into the same common pitfalls Many companies are using Elixir to building data processing pipelines -
Desired Features • Concurrency • Back-pressure • Batching • Graceful
shutdowns • Consume data from different sources like SQS, RabbitMQ, … • More… -
Desired Features • Concurrency • Back-pressure • Batching • Graceful
shutdowns • Consume data from different sources like SQS, RabbitMQ, … • More… -
Desired Features • Concurrency • Back-pressure • Batching • Graceful
shutdowns • Consume data from different sources like SQS, RabbitMQ, … • More… -
Desired Features • Concurrency • Back-pressure • Batching • Graceful
shutdowns • Consume data from different sources like SQS, RabbitMQ, … • More… -
Desired Features • Concurrency • Back-pressure • Batching • Graceful
shutdowns • Consume data from different sources like SQS, RabbitMQ, … • More… -
Complex GenStage pipelines • How to define the right topology
for the pipeline? • How to structure the supervision tree correctly? • How to handle graceful shutdown without data loss? ? -
Complex GenStage pipelines • How to define the right topology
for the pipeline? • How to structure the supervision tree correctly? • How to handle graceful shutdown without data loss? ? -
Broadway Configuration def handle_message(…) do ... end def handle_batch(…) do
... end Behaviour + = Pipeline -
Broadway defmodule MyBroadway do use Broadway def start_link(_opts) do Broadway.start_link(MyBroadway,
producers: [...], processors: [...], batchers: [...] ) end def handle_message(...) do ... end def handle_batch(...) do ... end end Behaviour -
Broadway defmodule MyBroadway do use Broadway def start_link(_opts) do Broadway.start_link(MyBroadway,
producers: [...], processors: [...], batchers: [...] ) end def handle_message(...) do ... end def handle_batch(...) do ... end end Configuration -
Broadway defmodule MyBroadway do use Broadway def start_link(_opts) do Broadway.start_link(MyBroadway,
producers: [...], processors: [...], batchers: [...] ) end def handle_message(...) do ... end def handle_batch(...) do ... end end Callbacks -
Configuration (defines the topology) producers: [ default: [ module: {Counter,
[]}, stages: 2 ] ], processors: [ default: [stages: 3] ] Producers Processors -
producers: [ default: [ module: {Counter, []}, stages: 2 ]
], processors: [ default: [stages: 3] ] Producers Processors Configuration (defines the topology) -
producers: [ default: [ module: {Counter, []}, stages: 1 ]
], processors: [ default: [stages: 2] ], batchers: [ sqs: [stages: 2], s3: [stages: 1] ] S3 SQS Producers Processors Batchers Batch Processors Configuration (defines the topology) -
producers: [ default: [ module: {Counter, []}, stages: 1 ]
], processors: [ default: [stages: 2] ], batchers: [ sqs: [stages: 2], s3: [stages: 1] ] S3 SQS Producers Processors Batchers Batch Processors Configuration (defines the topology) -
producers: [ default: [ module: {Counter, []}, stages: 1 ]
], processors: [ default: [stages: 2] ], batchers: [ sqs: [stages: 2], s3: [stages: 1] ] S3 SQS Producers Processors Batchers Batch Processors Configuration (defines the topology) -
producers: [ default: [ module: {Counter, []}, stages: 1 ]
], processors: [ default: [stages: 2] ], batchers: [ sqs: [stages: 2], s3: [stages: 1] ] S3 SQS Producers Processors Batchers Batch Processors Configuration (defines the topology) -
Callbacks (define the custom code that runs inside the pipeline)
def handle_message(...) do ... if sqs?(message) do message |> Message.put_batcher(:sqs) else message |> Message.put_batcher(:s3) end end def handle_batch(:sqs, ...) do ... end def handle_batch(:s3, ...) do ... end S3 SQS Producers Processors Batchers Batch Processors -
Callbacks (define the custom code that runs inside the pipeline)
def handle_message(...) do ... if sqs?(message) do message |> Message.put_batcher(:sqs) else message |> Message.put_batcher(:s3) end end def handle_batch(:sqs, ...) do ... end def handle_batch(:s3, ...) do ... end S3 SQS Producers Processors Batchers Batch Processors -
def handle_message(...) do ... if sqs?(message) do message |> Message.put_batcher(:sqs)
else message |> Message.put_batcher(:s3) end end def handle_batch(:sqs, ...) do ... end def handle_batch(:s3, ...) do ... end S3 SQS Callbacks (define the custom code that runs inside the pipeline) Producers Processors Batchers Batch Processors -
def handle_message(...) do ... if sqs?(message) do message |> Message.put_batcher(:sqs)
else message |> Message.put_batcher(:s3) end end def handle_batch(:sqs, ...) do ... end def handle_batch(:s3, ...) do ... end S3 SQS Callbacks (define the custom code that runs inside the pipeline) Producers Processors Batchers Batch Processors -
def handle_message(...) do ... if sqs?(message) do message |> Message.put_batcher(:sqs)
else message |> Message.put_batcher(:s3) end end def handle_batch(:sqs, ...) do ... end def handle_batch(:s3, ...) do ... end S3 SQS Callbacks (define the custom code that runs inside the pipeline) Producers Processors Batchers Batch Processors -
def handle_message(...) do ... if sqs?(message) do message |> Message.put_batcher(:sqs)
else message |> Message.put_batcher(:s3) end end def handle_batch(:sqs, ...) do ... end def handle_batch(:s3, ...) do ... end S3 SQS Callbacks (define the custom code that runs inside the pipeline) Producers Processors Batchers Batch Processors -
Complex GenStage pipelines • How to define the right topology
for the pipeline? • How to structure the supervision tree correctly? • How to handle graceful shutdown without data loss? ? -
Complex GenStage pipelines • How to define the right topology
for the pipeline? • How to structure the supervision tree correctly? • How to handle graceful shutdown without data loss? ? -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 -
ProducerSupervisor :one_for_one Terminator Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher Consumer_1
Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 ( max_restarts = 0 ) -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 ( max_restarts = 0 ) -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 ( max_restarts = 0 ) -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 ( max_restarts = 0 ) -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 ( max_restarts = 0 ) -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 ( max_restarts = 0 ) -
ProducerSupervisor :one_for_one Terminator Producer_1 Processor_2 Processor_1 Consumer_1 Batcher Consumer_2 Batcher
Consumer_1 Broadway Pipeline Supervisor :rest_for_one Broadway GenServer ProcessorSupervisor :one_for_all BatcherPartitionSupervisor :one_for_one ConsumerSupervisor :one_for_all ConsumerSupervisor :one_for_all BatcherConsumerSuperv_1 :rest_for_one BatcherConsumerSuperv_2 :rest_for_one Supervision Producer_2 ( max_restarts = 0 ) -
Complex GenStage pipelines ? • How to define the right
topology for the pipeline? • How to structure supervisions trees correctly? • How to handle graceful shutdown without data loss? -
Complex GenStage pipelines ? • How to define the right
topology for the pipeline? • How to structure supervisions trees correctly? • How to handle graceful shutdown without data loss? -
Graceful Shutdown ProducerSupervisor :one_for_one Terminator Broadway Pipeline Supervisor :rest_for_one ProcessorSupervisor
:one_for_all BatcherPartitionSupervisor :one_for_one -
Producers Processors Batchers Batch Processors Graceful Shutdown Step 1: Tell
processors to stop subscribing to producers Messages Terminator -
Producers Processors Batchers Batch Processors Graceful Shutdown Step 1: Tell
processors to stop subscribing to producers Messages Terminator -
Producers Processors Batchers Batch Processors Graceful Shutdown Step 1: Tell
processors to stop subscribing to producers Messages Terminator -
Producers Processors Batchers Batch Processors Graceful Shutdown Messages Terminator Step
2: Cancel and shutdown producers • Tell producers to: • Stop receiving messages • Stop accepting demand • Flush all events in the buffer • Cancel consumers -
Processors Batchers Batch Processors Graceful Shutdown Step 3: Monitors consumers
and wait until they’re dead Terminator monitor and wait Producers Messages -
Processors Batchers Batch Processors Graceful Shutdown Step 3: Monitors consumers
and wait until they’re dead Terminator monitor and wait Producers Messages -
Batchers Batch Processors Graceful Shutdown Step 3: Monitors consumers and
wait until they’re dead Terminator monitor and wait Producers Messages -
Batch Processors Graceful Shutdown Step 3: Monitors consumers and wait
until they’re dead Terminator monitor and wait Producers Messages -
Graceful Shutdown Step 3: Monitors consumers and wait until they’re
dead Terminator Producers Messages -
Graceful Shutdown Step 3: Monitors consumers and wait until they’re
dead Producers Messages -
Graceful Shutdown ProducerSupervisor :one_for_one Terminator Broadway Pipeline Supervisor :rest_for_one ProcessorSupervisor
:one_for_all BatcherPartitionSupervisor :one_for_one -
Complex GenStage pipelines ? • How to define the right
topology for the pipeline? • How to structure supervisions trees correctly? • How to handle graceful shutdown without data loss? -
Metrics and Statistics :telemetry “If You Can't Measure It, You
Can't Improve It!” -
Whats next? •Metrics and Statistics (WIP) •Multiple processors •Kafka connector
(WIP) •Rate limiter •More…