---
# Spark Streaming in production & how it works

**URL:** https://www.sigmoid.com/blogs/spark-streaming-production/
Date: 2015-04-22
Author: Sigmoid
Post Type: post
Summary: This is our next blog in the series of blogs about Spark Streaming. After talking about Spark Streaming and how it works,...Read More...
Categories: Data Management
Tags: Cloud Transformation
Featured Image: https://www.sigmoid.com/wp-content/uploads/2023/11/Spark-Streaming-in-Production-How-it-works-banner-opt.jpg
---

This is our next blog in the series of blogs about Spark Streaming.

After talking about [Spark Streaming](/blogs/spark-streaming-internals/) and [how it works](/blogs/spark-streaming-internals/), now we will look at how to implement Spark structured streaming in production. At Sigmoid we have implemented Spark Streaming in production for some [customers](/customer-analytics/) and have achieved great results by improving the design and architecture of the system. This blog post is about the design considerations and key learning for implementing Spark structured Streaming in a [production](/ebooks-whitepapers/ml-models-poc-to-production/) environment.

Most [Real time Analytics](/blogs/near-real-time-finance-data-warehousing-using-apache-spark-and-delta-lake/) Systems can be broken down into a Receiver System, a Stream Processing System and a Storage System.

      ![](/wp-content/uploads/2022/12/Spark_Streaming.png)

A good design/architecture is essential for the success of any application. A well designed system is stable, maintainable and [scalable](/blogs/value-of-analytics-with-cloud-data-warehouse/). With the design done correctly, maintenance and upgrade efforts can be minimized, which in turn  keeps the [costs low](/blogs/proven-methods-to-reduce-aws-cloud-infrastructure-cost/). Good design practices have been learnt and implemented while working on Spark Streaming at Sigmoid. Further we talk about how we can design Stable and maintainable systems with Spark Streaming.

## Spark Streaming in Production

### Stability

The system must be stable to overcome any unplanned outages as such outages might make clients furious and leave the user base frustrated.

**Fault tolerance** – Any production system should be capable of recovering from failures. [Fault Tolerance](/blogs/fault-tolerant-stream-processing/) is even more important for a real-time analytics system. Apart from creating a fault tolerant Streaming Processing System, your Receiver needs to be fault tolerant too.

     - Developing Akka based custom receivers with a supervisor allows you to create auto healing receivers. To Create a Supervisor for your actor, the actor needs to implement SupervisorStratergy. You can look atakkaworhdcount for implementing your custom Akka receiver

            The below code depicts this   

*// Restart the storage child when StorageException is thrown.*
*// After 3 restarts within 5 seconds it will be stopped.**private static SupervisorStrategy strategy = new OneForOneStrategy(3,Duration.create("5 seconds"), new Function<Throwable, Directive>() {*
*@Override*
*public Directive apply(Throwable t) {*
*if (t instanceof StorageException) {*
*return restart();*
*     * *}*
*else{*
*return escalate();*
*}}*
*});*

      - Using auto healing connection pools avoids exhaustion of the connection pool

          Track your output – The time interval and size of your output are the most suited parameters to set up alerting in your system. Setting up alerting to track these parameters makes more sense as it covers for failure scenarios at any point in your [pipeline](/etl-and-data-pipeline/).

                      - Insert timestamps in your data to track the latest updates

                      - Setup alerts to track huge changes to the output size

### Maintainability

Systems need to be designed in a modular way, this helps in bringing down the development and [maintenance costs](/events/cloud-webinar/). It is easy to customize or update modular systems. Bugs can be easily fixed without creating unwanted side effects. A good modular approach creates reusable components. We can safely say modularity brings maintainability.

The Stream processing system can be further divided into 3 subparts – Map, Aggregate and Store.

              - Map: Avoid the use of anonymous functions, as they are difficult to test and even more difficult to maintain. They can’t be tested without initializing a spark context. Move your logic out of your spark code, this would make your code more modular. This would allow you to write better Unit test cases, you can test your functions without initializing a Spark Context.

               - Aggregation: In the aggregation layer, Monoids allow to move the logic away from [Spark](/blogs/getting-data-into-spark-streaming/) code. In a crude way, Monads are classes which perform associative operations and can be plugged into spark code to perform Streaming operations.

object LongMonoid extends Monoid[(Long, Long, Long)] {
def zero = (0, 0, 0)
def plus(r: (Long, Long, Long), l: (Long, Long, Long)) = {
(l._1 + r._1, l._2 + r._2, l._3 + r._3)
}}

   [Twitter Algebird ](https://github.com/twitter/algebird)provides an API of monads, pretested and helps save a lot of development and testing effort.

     ![](/wp-content/uploads/2022/12/Spark-Streaming_Maintanability-1-scaled-1.jpg)

      - Store: It is preferable to use NoSQL databases over HDFS for Streaming Applications because NoSQL databases allow the application to make incremental updates. Also NoSQL Databases allow you to query the data, this is essential to verify the output of your job and test the system, this is not possible in HDFS.

### Testing your System

If the system is well designed, testing involves lesser effort and resources. A good set of automated test cases is essential to make enhancements and improvements to your system. The test cases need to have as much coverage as possible. But for functional testing, you don’t want to spin a cluster. You should also try to avoid writing integrated test cases. It’s suitable to write unit tests, and you can test your map functions and monads independently.

## About the Author

Arush was a technical team member at Sigmoid. He was involved in multiple projects including building data pipelines and real time processing frameworks.

    [lc_the_tags]

## Featured blogs

        [lc_get_posts post_type="post"
            posts_per_page="4" orderby="date" output_view="lc_get_posts_mycustom_view" output_number_of_columns="4"
            output_wrapper_class="row" output_article_class="shadow" output_hide_elements="Excerpt"
            output_excerpt_length="0" output_excerpt_text="Read More" output_heading_tag="span"
            output_featured_image_format="thumbnail" output_featured_image_class="card-img-left" ]

## Share

            [addtoany]

            Subscribe to get latest insights

                        Unlock all this and more possibilities in Financial services
                            with us.

                        [Explore offerings](/financial-services/)

## Suggested readings

					[View all](/blogs/)

						![](/wp-content/uploads/2022/12/apache-spark-for-real-time-analytics-thumbnail.png)

#### [Apache Spark for Real-time Analytics](/blogs/apache-spark-for-real-time-analytics/)

						[Read blog](/blogs/apache-spark-for-real-time-analytics/)

						![](https://www.sigmoid.com/wp-content/uploads/2023/01/Why-Apache-Arrow-is-the-Future-for-Open-Source-Columnar-thumbnail.png)

#### [Why Apache Arrow is the Future for Open Source Columnar](/blogs/apache-arrow-future-open-source-columnar-memory-analytics/)

						[Read blog](/blogs/apache-arrow-future-open-source-columnar-memory-analytics/)

						![](https://www.sigmoid.com/wp-content/uploads/2023/01/How-to-Optimize-Nested-Queries-using-Apache-Spark-thumnail.png)

#### [How to Optimize Nested Queries using Apache Spark](/blogs/optimize-nested-queries-using-apache-spark/)

						[Read blog](/blogs/optimize-nested-queries-using-apache-spark/)

---

## Categories

- Data Management

---

## Navigation

- [WordPress.org](https://wordpress.org/)
- [Documentation](https://wordpress.org/documentation/)
- [Learn WordPress](https://learn.wordpress.org/)
- [Support](https://wordpress.org/support/forums/)
- [Feedback](https://wordpress.org/support/forum/requests-and-feedback)
- [Sigmoid](https://www.sigmoid.com/)
- [Community](https://community.wpmanageninja.com/portal/space/fluent-forms/home)
- [Docs](https://wpmanageninja.com/docs/fluent-form/)
- [Developer Docs](https://developers.fluentforms.com/)
- [Documentation](https://imagify.io/documentation/)
- [Rate Imagify on WordPress.org](https://wordpress.org/support/view/plugin-reviews/imagify?rate=5#postform)
- [Manage](admin.php?page=litespeed)
- [Settings](admin.php?page=litespeed-cache)
- [Image Optimization](admin.php?page=litespeed-img_optm)
- [Company](/about-sigmoid)
- [Newsroom](/newsroom)
- [Life at Sigmoid](/careers)
- [Takshashila](/takshashila)
- [Contact Us](/contact-us)
- [AI Strategy Blueprint your AI advantage](/enterprise-ai-strategy/)
- [Generative AI Drive innovation with Generative AI](/generative-ai/)
- [Responsible AI Build trust with ethical AI practices](/responsible-ai-in-enterprise/)
- [Agentic AI Reshape business with scalable agentic systems](/agentic-ai-solutions/)
- [AI Managed Services Ensure reliable AI performance](/ai-managed-services/)
- [Advanced Analytics Transform your business with data-driven insights](/advanced-data-analytics-solutions/)
- [Start Assessment](/agentic-ai-readiness-index/)
- [Data Strategy Strong data foundations for scalable AI](/data-analytics-strategy/)
- [Data Management Leverage data as a strategic asset](/ai-data-management-services/)
- [Data Ops Automate data for speed and quality](/data-devops/)
- [Data Engineering Deliver insights faster with scalable pipelines](/data-engineering/)
- [Cloud Transformation Modernize data to maximise efficiency](/cloud-migration/)
- [Download Whitepaper](/ebooks-whitepapers/building-data-products-in-a-data-mesh-to-drive-business-value/)
- [Data Modeling Structure data for better decisions](/data-modeling-services/)
- [Data Visualization Transform data into actionable stories](/data-visualization-service/)
- [BI Migration Enhance decision making with modern BI tools](/bi-migration/)
- [Data Observability Build trust with healthy, accurate data](/data-observability/)
- [Automated Insights Make smarter decisions with auto-generated insights](/automated-insights/)
- [Download Whitepaper](/ebooks-whitepapers/power-bi-hacks/)
- [CPG & Retail End-to-end analytics for planning, operations, and commercial excellence](/industries/cpg-analytics/)
- [Life Sciences Trusted intelligence across clinical, commercial, and operational workflows](/industries/life-sciences/)
- [Financial Services AI-powered analytics for risk, compliance and customer experience](/industries/banking-financial-analytics-services/)
- [Read case study](/case-studies/data-clean-room-enables-real-time-insights-to-improve-operational-efficiency/)
- [MediaIQ Advanced platform for in-flight marketing measurement](/accelerators/sigmoid-mediaiq-multi-touch-attribution-tool/)
- [CampaignIQ AI-driven platform for optimized campaign budget allocation](/accelerators/sigmoid-campaigniq/)
- [AssistBot GenAI email assistant that automates human-like responses](/accelerators/sigmoid-assistbot-for-ai-email-assistant/)
- [CreativeBot GenAI tool for personalized and brand-aligned creative design](/accelerators/sigmoid-creativebot/)
- [SocialBot GenAI platform to analyze digital conversations and trends](/accelerators/#marketing|socialbot)
- [DemandIQ Predict trends accurately and optimize inventory management](/accelerators/sigmoid-demandiq/)
- [NetworkIQ Track and optimize logistics operations in real-time to quickly address disruptions](/accelerators/sigmoid-networkiq/)
- [SupplyIQ End-to-end platform to optimize supply chain operations](/accelerators/sigmoid-supplyiq/)
- [ProcurementIQ Automated procurement operations for maximum savings, compliance and efficiency](/accelerators/sigmoid-procurementiq/)
- [RapidML Accelerated deployment for machine learning models](/accelerators/sigmoid-rapidml/)
- [DataGuard Comprehensive platform for proactive data quality management](/accelerators/data-quality-tool-sigmoid-dataguard/)
- [CloudPulse Cloud cost optimization platform with multi-cloud management](/accelerators/sigmoid-cloudpulse/)
- [RAPID GenAI foundation with built-in governance and cost clarity](/accelerators/sigmoid-rapid/)
- [AnalyticsBot GenAI based platform to streamline decision-making in analytics](/accelerators/sigmoid-analyticsbot/)
- [DataConnect Seamlessly ingest, integrate and harmonize data from diverse sources](/accelerators/sigmoid-dataconnect/)
- [Reconica AI-powered data harmonization and reconciliation engine](/accelerators/sigmoid-reconica/)
- [ConverseBot GenAI driven insights generation for automated insights from reports](/accelerators/#sales|conversebot)
- [iNRM Cross-lever revenue growth optimization platform](/accelerators/sigmoid-inrm/)
- [AssortmentIQ Optimize shelf layouts and assortment mix at scale with AI-based insights](/accelerators/sigmoid-assortmentiq/)
- [Read Whitepaper](/ebooks-whitepapers/building-agentic-ai-chatbots-for-business-process-transformation/)
- [Listen Podcast](/events/podcast/how-jack-in-the-box-is-redefining-personalization-and-supply-chain-with-ai/)
- [Blogs](/blogs/)
- [White Papers](/ebooks-whitepapers/)
- [Case Studies](/case-studies/)
- [Podcast](/events/podcast/#Podcasts)
- [Read Blog](/blogs/the-genai-adoption-triad-responsibility-ethics-and-explainability/)
- [ConverseBot](/accelerators/#sales|conversebot/)

## Tags

- Cloud Transformation

---

## Footer Links

- [Talk to our AI experts](/contact-us/)
- [AI Strategy](/enterprise-ai-strategy/)
- [Agentic AI](/agentic-ai-solutions/)
- [Generative AI](/generative-ai/)
- [AI Managed Services](/ai-managed-services/)
- [Responsible AI](/responsible-ai-in-enterprise/)
- [Advanced Analytics](/advanced-data-analytics-solutions/)
- [Data Strategy](/data-analytics-strategy//)
- [Data Engineering](/data-engineering/)
- [Data Management](/ai-data-management-services/)
- [Cloud Transformation](/cloud-transformation/)
- [Data Ops](/data-devops/)
- [Data Visualization](/data-visualization-service/)
- [Automated Insights](/automated-insights/)
- [BI Migration](/bi-migration/)
- [Data Modeling](/data-modeling-services/)
- [Data Observability](/data-observability/)
- [CPG & Retail](/industries/cpg-analytics/)
- [Financial Services](/industries/banking-financial-analytics-services/)
- [Life Sciences](/industries/life-sciences/)
- [Case Studies](/case-studies/)
- [Thought Leadership](/ebooks-whitepapers/)
- [Blogs](/blogs/)
- [Company](/about-sigmoid/)
- [Newsroom](/newsroom/)
- [Accelerators](/accelerators/)
- [Careers](/careers/)
- [Privacy Policy |](/privacy-policy/)
- [Cookie Policy](/cookie-policy/)