<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Apache Storm - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Apache Storm - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sun, 26 Jul 2026 22:34:42 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/apache-storm/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ Apache Storm is awesome. This is why (and how) you should be using it. ]]>
                </title>
                <description>
                    <![CDATA[ By Usama Ashraf Continuous data streams are ubiquitous and are becoming even more so with the increasing number of IoT devices being used. Of course, this means huge volumes of data are stored, processed, and analyzed to provide predictive, actionabl... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/apache-storm-is-awesome-this-is-why-you-should-be-using-it-d7c37519a427/</link>
                <guid isPermaLink="false">66c344daa1d481faeda49b03</guid>
                
                    <category>
                        <![CDATA[ analytics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Apache Storm ]]>
                    </category>
                
                    <category>
                        <![CDATA[ data ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 23 May 2018 23:33:48 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*mI5DoLNNv5Z84E2-0SR-0g.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Usama Ashraf</p>
<p>Continuous data streams are ubiquitous and are becoming even more so with the <a target="_blank" href="http://www.businessinsider.com/75-billion-devices-will-be-connected-to-the-internet-by-2020-2013-10#ixzz3Il8nN9oB%20%20%20">increasing number of IoT devices being used</a>. Of course, this means huge volumes of data are stored, processed, and analyzed to provide predictive, actionable results.</p>
<p>But petabytes of data take a long time to analyze, even with tools such as <a target="_blank" href="http://hadoop.apache.org/">Hadoop</a> (as good as MapReduce may be) or <a target="_blank" href="https://spark.apache.org/">Spark</a> (a remedy to the limitations of MapReduce).</p>
<p>Often, we don’t need to deduce patterns over long periods of time. Of the petabytes of incoming data collected over months, at any given moment, we might not need to take into account all of it, just a real-time snapshot. Perhaps we don’t need to know the longest trending hashtag over five years, but just the one right now.</p>
<p>This is what <a target="_blank" href="https://storm.apache.org/">Apache Storm</a> is built for, to accept tons of data coming in extremely fast, possibly from various sources, analyze it, and publish real-time updates to a UI or some other place… <em>without storing any actual data</em>.</p>
<p>This article is not the ultimate guide to Apache Storm, nor is it meant to be. Storm’s pretty huge, and just one long-read probably can’t do it justice anyways. Of course, any additions, feedback or constructive criticism will be greatly appreciated.</p>
<p>OK, now that that’s out of the way, let’s see what we’ll be covering:</p>
<ul>
<li>The necessity of Storm, the ‘why’ of it, what it is and what it isn’t</li>
<li>A bird’s eye view of how it works.</li>
<li>What a Storm topology roughly looks like in code (Java)</li>
<li>Setting up and playing with a production-worthy Storm cluster on Docker.</li>
<li>A few words on message processing reliability.</li>
</ul>
<p>I’m also assuming that you’re at least somewhat familiar with <a target="_blank" href="https://www.docker.com/">Docker</a> and containerization.</p>
<h3 id="heading-how-it-works">How It Works</h3>
<p>The architecture of Apache Storm can be compared to a network of roads connecting a set of checkpoints. Traffic begins at a certain checkpoint (called a <strong>spout</strong>) and passes through other checkpoints (called <strong>bolts</strong>).</p>
<p>The traffic is of course the stream of data that is retrieved by the <strong>spout</strong> (from a data source, a public API for example) and routed to various <strong>bolts</strong> where the data is filtered, sanitized, aggregated, analyzed, and sent to a UI for people to view (or to any other target).</p>
<p>The network of spouts and bolts is called a <strong>topology</strong>, and the data flows in the form of <strong>tuples</strong> (list of values that may have different types).</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*pJ4nzdWQJ5wLqrUsBvkUQQ.png" alt="Image" width="700" height="371" loading="lazy">
_Source: [https://dzone.com/articles/apache-storm-architecture](https://dzone.com/articles/apache-storm-architecture*" rel="noopener" target="<em>blank" title=")</em></p>
<p>One important thing to talk about is the direction of the data traffic.</p>
<p>Conventionally, we would have one or multiple spouts reading the data from an API, a queuing system, and so on. The data would then flow <em>one-way</em> to one or multiple bolts which may forward it to other bolts and so on. Bolts may publish the analyzed data to a UI or to another bolt.</p>
<p>But the traffic is almost always unidirectional, like a <a target="_blank" href="https://en.wikipedia.org/wiki/Directed_acyclic_graph">directed acyclic graph</a> (DAG). Although it is certainly possible to make cycles, we’re unlikely to need such a convoluted topology.</p>
<p><a target="_blank" href="https://www.tutorialspoint.com/apache_storm/apache_storm_installation.htm">Installing a Storm release</a> involves a number of steps, which you’re free to follow on your machine. But later on I’ll be using Docker containers for a Storm cluster deployment, and the images will take care of setting up everything we need.</p>
<h3 id="heading-some-code">Some code</h3>
<p>While Storm does offer <a target="_blank" href="http://storm.apache.org/about/multi-language.html">support for other languages</a>, most topologies are written in Java, since it’s the most efficient option we have.</p>
<p>A very basic spout, that just emits random digits, may look like this:</p>
<p>And a simple bolt that takes in the stream of random digits and emits only the even ones:</p>
<p>Another simple bolt that’ll receive the filtered stream from <code>EvenDigitBolt</code>, and just multiply each even digit by 10 and emit it forward:</p>
<p>Putting them together to form our topology:</p>
<h3 id="heading-parallelism-in-storm-topologies">Parallelism in Storm topologies</h3>
<p>Fully understanding parallelism in Storm can be daunting, at least in my experience. A topology requires at least one process to operate on. Within this process, we can parallelize the execution of our spouts and bolts using threads.</p>
<p>In our example, <code>RandomDigitSpout</code> will launch just one thread, and the data spewed from that thread will be distributed among two threads of the <code>EvenDigitBolt</code>.</p>
<p>But the way this distribution happens, referred to as the <strong>stream grouping</strong>, can be important. For example, you may have a stream of temperature recordings from two cities, where the tuples emitted by the spout look like this:</p>
<pre><code><span class="hljs-comment">// City name, temperature, time of recording</span>
</code></pre><pre><code>(“Atlanta”,       <span class="hljs-number">94</span>, “<span class="hljs-number">2018</span>–<span class="hljs-number">05</span>–<span class="hljs-number">11</span> <span class="hljs-number">23</span>:<span class="hljs-number">14</span>”)(“New York City”, <span class="hljs-number">75</span>, “<span class="hljs-number">2018</span>–<span class="hljs-number">05</span>–<span class="hljs-number">11</span> <span class="hljs-number">23</span>:<span class="hljs-number">15</span>”)(“New York City”, <span class="hljs-number">76</span>, “<span class="hljs-number">2018</span>–<span class="hljs-number">05</span>–<span class="hljs-number">11</span> <span class="hljs-number">23</span>:<span class="hljs-number">16</span>”)(“Atlanta”,       <span class="hljs-number">96</span>, “<span class="hljs-number">2018</span>–<span class="hljs-number">05</span>–<span class="hljs-number">11</span> <span class="hljs-number">23</span>:<span class="hljs-number">15</span>”)(“New York City”, <span class="hljs-number">77</span>, “<span class="hljs-number">2018</span>–<span class="hljs-number">05</span>–<span class="hljs-number">11</span> <span class="hljs-number">23</span>:<span class="hljs-number">17</span>”)(“Atlanta”,       <span class="hljs-number">95</span>, “<span class="hljs-number">2018</span>–<span class="hljs-number">05</span>–<span class="hljs-number">11</span> <span class="hljs-number">23</span>:<span class="hljs-number">16</span>”)(“New York City”, <span class="hljs-number">76</span>, “<span class="hljs-number">2018</span>–<span class="hljs-number">05</span>–<span class="hljs-number">11</span> <span class="hljs-number">23</span>:<span class="hljs-number">18</span>”)
</code></pre><p>Suppose we’re attaching just one bolt whose job is to calculate the changing average temperature of each city.</p>
<p>If we can reasonably expect that in any given time interval we’ll get roughly an equal number of tuples from both the cities, it would make sense to dedicate two threads to our bolt. We can send the data for Atlanta to one of them and New York to the other.</p>
<p>A <strong>fields grouping</strong> would serve our purpose, which partitions data among the threads by the value of the field specified in the grouping:</p>
<pre><code><span class="hljs-comment">// The tuples with the same city name will go to the same thread.builder.setBolt(“avg-temp-bolt”, new AvgTempBolt(), 2)       .fieldsGrouping(“temp-spout”, new Fields(“city_name”));</span>
</code></pre><p>And of course, there are <a target="_blank" href="http://www.corejavaguru.com/bigdata/storm/stream-groupings">other types of groupings as well</a>. For most cases, though, the grouping probably won’t matter much. You can just shuffle the data and throw it among the bolt threads randomly (<strong>shuffle grouping</strong>).</p>
<p>Now there’s another important component to this: the number of worker processes that our topology will run on.</p>
<p>The total number of threads that we specified will then be equally divided among the worker processes. So, in our example random digit topology, we had one spout thread, two even-digit bolt threads, and four multiply-by-ten bolt threads (giving seven in total).</p>
<p>Each of the two worker processes would be responsible for running two multiply-by-ten bolt threads, one even-digit bolt, and one of the processes will run the one spout thread.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*E9IoN2Mjur31Bn-NCgrwAg.jpeg" alt="Image" width="770" height="444" loading="lazy"></p>
<p>Of course, the two worker processes will have their main threads, which in turn will launch the spout and bolt threads. So all in all we’ll have nine threads. These are collectively called <strong>executors</strong>.</p>
<p>It’s important to realize that if you set a spout’s parallelism hint to be greater than one (multiple executors), you can end up emitting the same data several times.</p>
<p>Say the spout reads from the public Twitter stream API and uses two executors. That means that the bolts receiving the data from the spout will get the same tweet twice. It is only <em>after</em> the spout emits the tuples that data parallelism comes into play. In other words, the tuples get divided among the bolts according to the specified stream grouping.</p>
<p>Running multiple workers on a single node would be fairly pointless. Later, however, we’ll use a proper, distributed, multi-node cluster and see how workers are divided on different nodes.</p>
<h3 id="heading-building-our-topology">Building our topology</h3>
<p>Here’s the directory structure I suggest:</p>
<pre><code>yourproject/            pom.xml             src/                jvm/                    packagename/                          RandomDigitSpout.java                          EvenDigitBolt.java                          MultiplyByTenBolt.java                          OurSimpleTopology.java
</code></pre><p><a target="_blank" href="https://maven.apache.org/">Maven</a> is commonly used for building Storm topologies, and it requires a <code>pom.xml</code> file (The POM) that <a target="_blank" href="https://maven.apache.org/guides/introduction/introduction-to-the-pom.html">defines various configuration details, project dependencies, and so on</a>. Getting into the <a target="_blank" href="https://maven.apache.org/pom.html">nitty-gritty of the POM</a> will probably be overkill here.</p>
<ul>
<li>First, we’ll run <code>mvn clean</code> inside <code>yourproject</code> to clear any compiled files we may have, making sure to compile each module from scratch.</li>
<li>And then <code>mvn package</code> to compile our code and package it in an executable JAR file, inside a newly created <code>target</code> folder. This might take quite a few minutes the first time, especially if your topology has many dependencies.</li>
<li>To submit our topology: <code>storm jar target/packagename-{version number}.jar packagename.OurSimpleTopology</code></li>
</ul>
<p>Hopefully by now the gap between concept and code in Storm has been somewhat bridged. However, no serious Storm deployment will be a single topology instance running on one server.</p>
<h3 id="heading-what-a-storm-cluster-looks-like">What a Storm cluster looks like</h3>
<p>To take full advantage of Storm’s <a target="_blank" href="http://storm.apache.org/about/scalable.html">scalability</a> and <a target="_blank" href="http://storm.apache.org/releases/current/Fault-tolerance.html">fault-tolerance</a>, any production-grade topology would be submitted to a cluster of machines.</p>
<p>Storm distributions are installed on the primary node (Nimbus) and all the replica nodes (Supervisors).</p>
<p>The <em>primary</em> node runs the Storm <a target="_blank" href="https://github.com/apache/storm/blob/exclamation/storm-server/src/main/java/org/apache/storm/daemon/nimbus/Nimbus.java">Nimbus</a> daemon and the Storm UI. The <em>replica</em> nodes run the Storm <a target="_blank" href="https://github.com/apache/storm/blob/exclamation/storm-server/src/main/java/org/apache/storm/daemon/supervisor/Supervisor.java">Supervisor</a> daemons. A <a target="_blank" href="http://zookeeper.apache.org/">Zookeeper</a> daemon on a separate node is used for coordination among the primary node and the replica nodes.</p>
<p>Zookeeper, by the way, is only used for cluster management and never any kind of message passing. It’s not like spouts and bolts are sending data to each other through it or anything like that. The Nimbus daemon finds available Supervisors via ZooKeeper, to which the Supervisor daemons register themselves. It also carries out other managerial tasks, some of which will become clear shortly.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*vMWbOJP4LE0upnSYHIojSg.jpeg" alt="Image" width="714" height="602" loading="lazy"></p>
<p>The Storm UI is a web interface used to manage the state of our cluster. We’ll get to this later.</p>
<p>Our topology is submitted to the Nimbus daemon on the primary node, and then distributed among the worker processes running on the replica/supervisor nodes. Thanks to Zookeeper, it doesn’t matter how many replica/supervisor nodes you run initially, as you can always seamlessly add more. Storm will automatically integrate them into the cluster.</p>
<p>Whenever we start a Supervisor, it allocates a certain number of worker processes (that we can configure). These can then be used by the submitted topology. So in the image above, there are a total of five allocated workers.</p>
<p>Remember this line: <code>conf.setNumWorkers(5)</code></p>
<p>This means that the topology will try to use a total of five workers. And since our two Supervisor nodes have a total of five allocated workers, each of the 5 allocated worker processes will run one instance of the topology.</p>
<p>If we had run <code>conf.setNumWorkers(4)</code> then one worker process would have remained idle/unused. If the number of specified workers was six and the total allocated workers were five, then because of the limitation only five actual topology workers would’ve been functional.</p>
<p>Before we set this all up using Docker, there are a few important things to keep in mind regarding fault-tolerance:</p>
<ul>
<li>If any worker on any replica node dies, the Supervisor daemon will have it restarted. If restarting repeatedly fails, the worker will be reassigned to another machine.</li>
<li>If an entire replica node dies, its share of the work will be given to another supervisor/replica node.</li>
<li>If the Nimbus goes down, the workers will remain unaffected. However, until the Nimbus is restored, workers won’t be reassigned to other replica nodes if, say, their node crashes.</li>
<li>The Nimbus and Supervisors are themselves stateless. But with Zookeeper, some state information is stored so that things can begin where they were left off if a node crashes or a daemon dies unexpectedly.</li>
<li>Nimbus, Supervisor and Zookeeper daemons are all fail-fast. This means that they themselves are not very tolerant to unexpected errors, and will shut down if they encounter one. For this reason they have to be run under supervision using a watchdog program that monitors them constantly and restarts them automatically if they ever crash. <a target="_blank" href="http://supervisord.org/">Supervisord</a> is probably the most popular option for this (not to be confused with the Storm Supervisor daemon).</li>
</ul>
<p>Note: In most Storm clusters, the Nimbus itself is never deployed as a single instance but as a cluster. If this fault-tolerance is not incorporated and our sole Nimbus goes down, <a target="_blank" href="https://hortonworks.com/blog/fault-tolerant-nimbus-in-apache-storm/">we’ll lose the ability to submit new topologies, gracefully kill running topologies, reassign work to other Supervisor nodes if one crashes, and so on</a>.</p>
<p>For simplicity, our illustrative cluster will use a single instance. Similarly, the Zookeeper is very often deployed as a cluster but we’ll use just one.</p>
<h3 id="heading-dockerizing-the-cluster">Dockerizing The Cluster</h3>
<p>Launching individual containers and all that goes along with them can be cumbersome, so I prefer to use <a target="_blank" href="https://docs.docker.com/compose/">Docker Compose</a>.</p>
<p>We’ll be going with one Zookeeper node, one Nimbus node, and one Supervisor node initially. They’ll be defined as Compose services, all corresponding to one container each at the beginning.</p>
<p>Later on, I’ll use <a target="_blank" href="https://docs.docker.com/compose/reference/scale/">Compose scaling</a> to add another Supervisor node (container). Here’s the <a target="_blank" href="https://github.com/UsamaAshraf/coincident-hashtags-with-apache-storm/tree/exclamation">entire code</a> and the project structure:</p>
<pre><code>zookeeper/          Dockerfilestorm-nimbus/          Dockerfile          storm.yaml          code/               pom.xml               src/                   jvm/                       coincident_hashtags/                                  ExclamationTopology.java storm-supervisor/          Dockerfile          storm.yamldocker-compose.yml
</code></pre><p>And our <code>docker-compose.yml</code>:</p>
<p>Feel free to explore the Dockerfiles. They basically just install the dependencies (Java 8, Storm, Maven, Zookeeper) on the relevant containers.</p>
<p>The <code>storm.yaml</code> files override certain default configurations for the Storm installations. The line <code>ADD storm.yaml /conf</code> inside the Nimbus and Supervisor Dockerfiles puts them inside the containers where Storm can read them.</p>
<p><code>storm-nimbus/storm.yaml</code>:</p>
<p><code>storm-supervisor/storm.yaml</code>:</p>
<p>These options are adequate for our cluster. If you are curious, you can check out all the <a target="_blank" href="https://github.com/apache/storm/blob/exclamation/conf/defaults.yaml">default configurations here</a>.</p>
<p>Run <code>docker-compose up</code> at the project root.</p>
<p>After all the images have been built and all the service started, open a new terminal, type <code>docker ps</code> and you’ll see something like this:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*prWdSPqapMJY96SLJfQ-kQ.png" alt="Image" width="800" height="119" loading="lazy"></p>
<h3 id="heading-starting-the-nimbus">Starting The Nimbus</h3>
<p>Let’s SSH into the Nimbus container using its name:</p>
<p><code>docker exec -it coincidenthashtagswithapachestorm_storm-nimbus_1 bash</code></p>
<p>And then start the Nimbus daemon: <code>storm nimbus</code></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*bzBTGBMRYoJmLHR6xx_SRQ.png" alt="Image" width="800" height="97" loading="lazy"></p>
<h3 id="heading-starting-the-storm-ui">Starting The Storm UI</h3>
<p>Similarly, open another terminal, SSH into the Nimbus again and launch the UI using <code>storm ui</code>:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*v2h_5x3U8v3p7vmTrQ9cAg.png" alt="Image" width="800" height="91" loading="lazy"></p>
<p>Go to <code>localhost:8080</code> on your browser and you’ll see a nice overview of our cluster:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*pY-0-U1VccLEMDz1UaW4hg.png" alt="Image" width="800" height="423" loading="lazy"></p>
<p>The ‘Free slots’ in the Cluster Summary indicate how many total workers (on all Supervisor nodes) are available and waiting for a topology to consume them.</p>
<p>‘Used slots’ indicates how many of the total are currently busy with a topology. Since we haven’t launched any Supervisors yet, they’re both zero. We’ll get to <em>Executors</em> and <em>Tasks</em> later. Also, as we can see, no topologies have been submitted yet.</p>
<h3 id="heading-starting-a-supervisor-node">Starting A Supervisor Node</h3>
<p>SSH into the one Supervisor container and launch the Supervisor daemon:</p>
<pre><code>docker exec -it coincidenthashtagswithapachestorm_storm-supervisor_1 bashstorm supervisor
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/1*AazN5gMmeSgH6FTSy3zyvw.png" alt="Image" width="800" height="78" loading="lazy"></p>
<p>Now let’s go refresh our UI:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*hhfrf7G50tsjnOYCnHdRDg.png" alt="Image" width="800" height="422" loading="lazy"></p>
<p>Note: Any changes in our cluster may take a few seconds to reflect on the UI.</p>
<p>We have a new running Supervisor which comes with four allocated workers. These four workers are the result of specifying four ports in our <code>storm.yaml</code> for the Supervisor node. Of course, they’re all free (four Free slots).</p>
<p>Let’s submit a topology to the Nimbus and put ’em to work.</p>
<h3 id="heading-submitting-a-topology-to-the-nimbus">Submitting A Topology To The Nimbus</h3>
<p>SSH into the Nimbus on a new terminal. I’ve written the <a target="_blank" href="https://github.com/UsamaAshraf/coincident-hashtags-with-apache-storm/blob/exclamation/storm-nimbus/Dockerfile#L65">Dockerfile</a> so that we land on our working (landing) directory <code>/theproject</code>. Inside this is <code>code</code>, where our topology resides.</p>
<p><a target="_blank" href="https://github.com/UsamaAshraf/coincident-hashtags-with-apache-storm/blob/exclamation/storm-nimbus/code/src/jvm/coincident_hashtags/ExclamationTopology.java">Our topology is pretty simple</a>. It uses a spout that generates random words and a bolt that just appends three exclamation marks (!!!) to the words. Two of these bolts are added back-to-back, and so at the end of the stream we’ll get words with six exclamation marks. It also specifies that it needs three workers (<code>[conf.setNumWorkers(3)](https://github.com/UsamaAshraf/coincident-hashtags-with-apache-storm/blob/exclamation/storm-nimbus/code/src/jvm/coincident_hashtags/ExclamationTopology.java#L76)</code>).</p>
<p>Run these commands:</p>
<ol>
<li><code>cd code</code>  </li>
<li><code>mvn clean</code>  </li>
<li><code>mvn package</code>  </li>
<li><code>storm jar target/coincident-hashtags-1.2.1.jar coincident_hashtags.ExclamationTopology</code></li>
</ol>
<p>After the topology has been submitted successfully, refresh the UI:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*DpLhx5dMl7uThJgEjDXACw.png" alt="Image" width="800" height="423" loading="lazy"></p>
<p>As soon as we submitted the topology, the Zookeeper was notified. The Zookeeper in turn notified the Supervisor to download the code from the Nimbus. We now see our topology along with its three occupied workers, leaving just one free.</p>
<p>And ten word spout threads + three exclaim1 bolt threads + two exclaim bolt threads + the three main threads from the workers = total of 18 executors.</p>
<p>And you might’ve noticed something new: tasks.</p>
<h3 id="heading-what-are-tasks">What are tasks?</h3>
<p>Tasks are another concept in Storm’s parallelism. But don’t sweat it, a task is just an instance of a spout or bolt that an executor uses. They are what actually does the processing.</p>
<p>By default, the number of tasks is equal to the number of executors. In rare cases you might need each executor to instantiate more tasks.</p>
<pre><code><span class="hljs-comment">// Each of the two executors (threads) of this bolt will instantiate// two objects of this bolt (total 4 bolt objects/tasks).builder.setBolt(“even-digit-bolt”, new EvenDigitBolt(), 2)       .setNumTasks(4)        .shuffleGrouping(“random-digit-spout”);</span>
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/1*yJchF0mgDnzPTnvz39siew.jpeg" alt="Image" width="729" height="276" loading="lazy"></p>
<p>This is a shortcoming on my part, but I can’t think of a good use case where we’d need multiple tasks per executor.</p>
<p>Maybe if we were adding some parallelism ourselves, like spawning a new thread within the bolt to handle a long running task, then the main executor thread won’t block and will be able to continue processing using the other bolt.</p>
<p>However, this can make our topology hard to understand. If anyone knows of scenarios where the performance gain from multiple tasks outweighs the added complexity, please post a comment.</p>
<p>Anyways, returning from that slight detour, let’s see an overview of our topology. Click on the name under Topology Summary and scroll down to Worker Resources:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*2HQTiqg0xBhQITZFH-teYg.png" alt="Image" width="800" height="230" loading="lazy"></p>
<p>We can clearly see the division of our executors (threads) among the three workers. And of course all the three workers are on the same, single Supervisor node we’re running.</p>
<p>Now, let’s say scale out!</p>
<h3 id="heading-add-another-supervisor">Add Another Supervisor</h3>
<p>From the project root, let’s add another Supervisor node/container:</p>
<pre><code>docker-compose scale storm-supervisor=<span class="hljs-number">2</span>
</code></pre><p>SSH into the new container:</p>
<pre><code>docker exec -it coincidenthashtagswithapachestorm_storm-supervisor_2 bash
</code></pre><p>And fire up: <code>storm supervisor</code></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*DzxzUU7HgrKYiaTiSFLQUg.png" alt="Image" width="800" height="236" loading="lazy"></p>
<p>If you refresh the UI you’ll see that we’ve successfully added another Supervisor and four more workers (total of eight workers/slots). To really take advantage of the new Supervisor, let’s increase the topology’s workers.</p>
<ul>
<li>First kill the running one: <code>storm kill exclamation-topology</code></li>
<li>Change <a target="_blank" href="https://github.com/UsamaAshraf/coincident-hashtags-with-apache-storm/blob/exclamation/storm-nimbus/code/src/jvm/coincident_hashtags/ExclamationTopology.java#L77">this line</a> to: <code>conf.setNumWorkers(6)</code></li>
<li>Change the project version number in your <code>pom.xml</code>. Try using a proper scheme, like semantic versioning. I’ll just stick with 1.2.1.</li>
<li>Rebuild the topology: <code>mvn package</code></li>
<li>Resubmit it: <code>storm jar target/coincident-hashtags-1.2.1.jar coincident_hashtags.ExclamationTopology</code></li>
</ul>
<p>Reload the UI:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*j09T9vFkeyRu7ptWuTlClQ.png" alt="Image" width="800" height="420" loading="lazy"></p>
<p>You can now see the new Supervisor and the six busy workers out of a total of eight available ones.</p>
<p>Also important to note is that the six busy ones have been equally divided among the two Supervisors. Again, click the topology name and scroll down.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*8nTM2oNM-PwU7QggAAQEyg.png" alt="Image" width="800" height="364" loading="lazy"></p>
<p>We see two unique Supervisor IDs, both running on different nodes, and all our executors pretty evenly divided among them. This is great.</p>
<p>But Storm comes with another nifty way of doing so while the topology is running — rebalancing.</p>
<p>On the Nimbus we’d run:</p>
<pre><code>storm rebalance exclamation-topology -n <span class="hljs-number">6</span>
</code></pre><p>Or to change the number of executors for a particular component:</p>
<pre><code>storm rebalance exclamation-topology -e even-digit-bolt=<span class="hljs-number">3</span>
</code></pre><h3 id="heading-reliable-message-processing">Reliable Message Processing</h3>
<p>One question we haven’t tackled is about what happens if a bolt fails to process a tuple.</p>
<p>Storm provides us a mechanism by which the originating spout (specifically, the <em>task</em>) can replay the failed tuple. This processing guarantee doesn’t just happen by itself. It’s a conscious design choice, and does add latency.</p>
<p>Spouts send out tuples to bolts, which emit tuples derived from the input tuples to other bolts and so on. That one original tuple spurs an entire tree of tuples.</p>
<p>If any child tuple, so to speak, of the original one fails, then any remedial steps (rollbacks etc) may well have to be taken at multiple bolts. That could get pretty hairy, and so what Storm does is that it allows the original tuple to be emitted again right from the source (the spout).</p>
<p>Consequently, any operations performed by bolts that are a function of the incoming tuples should be <a target="_blank" href="https://en.wikipedia.org/wiki/Idempotence">idempotent</a>.</p>
<p>A tuple is considered “fully processed” when every tuple in its tree has been processed, and every tuple has to be explicitly acknowledged by the bolts.</p>
<p>However, that’s not all. There’s another thing to be done explicitly: maintain a link between the original tuple and its child tuples. Storm will then be able to trace the origin of the child tuples and thus be able to replay the original tuple. This is called <em>anchoring</em>. <a target="_blank" href="https://github.com/UsamaAshraf/coincident-hashtags-with-apache-storm/blob/exclamation/storm-nimbus/code/src/jvm/coincident_hashtags/ExclamationTopology.java#L44">And this has been done in our exclamation bolt</a>:</p>
<pre><code><span class="hljs-comment">// ExclamationBolt</span>
</code></pre><pre><code><span class="hljs-comment">// ‘tuple’ is the original one received from the test word spout.// It’s been anchored to/with the tuple going out._collector.emit(tuple, new Values(exclamatedWord.toString()));</span>
</code></pre><pre><code><span class="hljs-comment">// Explicitly acknowledge that the tuple has been processed._collector.ack(tuple);</span>
</code></pre><p>The <code>ack</code> call will result in the <code>ack</code> method on the spout being called, if it has been implemented.</p>
<p>So, say you’re reading the tuple data from some queue and you can only take it off the queue if the tuple has been fully processed. The <code>ack</code> method is where you’d do that.</p>
<p>You can also emit out tuples without anchoring:</p>
<pre><code>_collector.emit(<span class="hljs-keyword">new</span> Values(exclamatedWord.toString()))
</code></pre><p>and forgo reliability.</p>
<p>A tuple can fail two ways:</p>
<ol>
<li>A bolt dies and a tuple times out. Or, it times out for some other reason. The timeout is 30 seconds by default and can be changed using <code>config.put(Config.TOPOLOGY_MESSAGE_TIMEOUT_SECS, 60)</code></li>
<li>The <code>fail</code> method is explicitly called on the tuple in a bolt: <code>_collector.fail(tuple)</code>. You may do this in case of an exception.</li>
</ol>
<p>In both these cases, the <code>fail</code> method on the spout will be called, if it is implemented. And if we want the tuple to be replayed, it would have to be done explicitly in the <code>fail</code> method by calling <code>emit</code>, just like in <code>nextTuple()</code>. When tracking tuples, every one has to be <code>ack</code>ed or <code>fail</code>ed. Otherwise, the topology will eventually run out of memory.</p>
<p>It’s also important to know that you have to do all of this yourself when writing custom spouts and bolts. But the Storm core can help. For example, a bolt implementing <a target="_blank" href="https://storm.apache.org/releases/1.2.1/javadocs/org/apache/storm/topology/base/BaseBasicBolt.html">BaseBasicBolt</a> does acking automatically. Or built-in spouts for popular data sources like <a target="_blank" href="https://github.com/apache/storm/blob/master/external/storm-kafka/src/jvm/org/apache/storm/kafka/KafkaSpout.java">Kafka</a> take care of queuing and replay logic after acknowledgment and failure.</p>
<h3 id="heading-parting-shots">Parting Shots</h3>
<p>Designing a Storm topology or cluster is always about tweaking the various knobs we have and settling where the result seems optimal.</p>
<p>There are a few things that’ll help in this process, like using a configuration file to read parallelism hints, number of workers, and so on so you don’t have to edit and recompile your code repeatedly.</p>
<p>Define your bolts logically, one per indivisible task, and keep them light and efficient. Similarly, your spouts’ <code>nextTuple()</code> methods should be optimized.</p>
<p>Use the Storm UI effectively. By default, it doesn’t show us the complete picture, only 5% of the total tuples emitted. To monitor all of them, use <code>config.setStatsSampleRate(1.0d)</code>.</p>
<p>Keep an eye on the <strong>Acks</strong> and <strong>Latency</strong> values for individual bolts and topologies via the UI. That’s what you want to look at when tuning the parameters.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
