How to send output of two different Spout to the same Bolt?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the realm of real-time computation, Apache Storm stands out as a prominent framework designed to process large streams of data. One of the fundamental concepts of Apache Storm is the idea of "topologies," which are graphs of computation consisting of spouts (sources of data) and bolts (units of processing). In complex scenarios, it may be necessary to direct the outputs of multiple spouts to a single bolt for combined processing. This setup is crucial for tasks involving aggregation or join operations from diverse data streams.
Understanding Stream Groupings
To send outputs from multiple spouts to the same bolt in Apache Storm, you must configure your topology to ensure that both spouts direct their output to that bolt. This is controlled by stream groupings, which dictate how messages are distributed among bolt instances. The main groupings available are:
- Shuffle Grouping: Randomly distributes tuples equally across the bolt's tasks.
- Fields Grouping: Tuples are directed to the same task based on specific fields in the tuples.
- All Grouping: Sends the tuple to all tasks of the bolt (useful for broadcasting).
Technical Setup
Here is how you can technically achieve this configuration. Assume you have two spouts, Spout1 and Spout2, and one bolt, JoinBolt, which needs to receive data from both spouts.
1. Define Topology
In this setup:
Spout1andSpout2are added to the topology.JoinBoltis set with a parallelism hint of2, implying two tasks of this bolt will process tuples.- Both spouts are connected to
JoinBoltusing a fields grouping on the field"id". This setup ensures that tuples from both spouts with the sameidvalue are sent to the same task ofJoinBolt, facilitating correct joining or aggregation based on theid.
2. Implementing JoinBolt
JoinBolt needs to be designed to manage tuples coming from the two spouts. It should differentiate which spout the tuple is coming from, and process them accordingly.
Summary Table
Here is a summary of the key components and considerations:
| Component | Description | Configuration Example |
| Spout1 | Source of tuples (stream of data). | builder.setSpout("spout1", new Spout1()); |
| Spout2 | Another source of tuples. | builder.setSpout("spout2", new Spout2()); |
| JoinBolt | Processes inputs from multiple spouts. | builder.setBolt("joinBolt", new JoinBolt()).fieldsGrouping("spout1", new Fields("id")).fieldsGrouping("spout2", new Fields("id")); |
| FieldsGrouping | Ensures tuples with the same key fields directed to the same bolt task. | .fieldsGrouping("spout1", new Fields("id")) |
Additional Considerations
- Data Skew: If the data is unevenly distributed, some bolt tasks might be overloaded. Solutions include adjusting the number of tasks or using a different grouping strategy.
- Fault Tolerance: Ensure that your bolt can handle faults and replay of tuples if necessary, as Storm provides at-least-once processing guarantees.
- Performance Metrics: Monitor the performance of your bolts. Apache Storm provides built-in metrics that can help identify bottlenecks in topology.
Configuring multiple spouts to send data to the same bolt allows for versatile processing patterns in Apache Storm, accommodating a wide range of real-time data processing scenarios.

