As information is generated at an unprecedented fee, streaming options have turn into important for companies searching for to harness close to real-time insights. Streaming information—from social media feeds, IoT gadgets, e-commerce transactions, and extra—requires strong platforms that may course of and analyze information because it arrives, enabling fast decision-making and actions.
That is the place Apache Spark Structured Streaming comes into play. It provides a high-level API that simplifies the complexities of streaming information, permitting builders to write down streaming jobs as in the event that they had been batch jobs, however with the ability to course of information in close to actual time. Spark Structured Streaming integrates seamlessly with varied information sources, reminiscent of Amazon Managed Streaming for Apache Kafka (Amazon MSK) and Amazon Kinesis Knowledge Streams, offering a unified answer that helps complicated operations like windowed computations, event-time aggregation, and stateful processing. Through the use of Spark’s quick, in-memory processing capabilities, companies can run streaming workloads effectively, scaling up or down as wanted, to derive well timed insights that drive strategic and important choices.
The setup of a computing infrastructure to help such streaming workloads poses its challenges. Right here, Amazon EMR Serverless emerges as a pivotal answer for working streaming workloads, enabling the usage of the newest open supply frameworks like Spark with out the necessity for configuration, optimization, safety, or cluster administration.
Beginning with Amazon EMR 7.1, we launched a brand new job --mode on EMR Serverless referred to as Streaming. You may submit a streaming job from the EMR Studio console or the StartJobRun API:
On this put up, we spotlight a few of the key enhancements launched for streaming jobs.
Efficiency
The Amazon EMR runtime for Apache Spark delivers a high-performance runtime atmosphere whereas sustaining 100% API compatibility with open supply Spark. Moreover, we’ve got launched the next enhancements to offer improved help for streaming jobs.
Amazon Kinesis connector with Enhanced Fan-Out Help
Conventional Spark streaming functions studying from Kinesis Knowledge Streams usually face throughput limitations on account of shared shard-level learn capability, the place a number of customers compete for the default 2 MBps per shard throughput. This bottleneck turns into significantly difficult in eventualities requiring real-time processing throughout a number of consuming functions.
To handle this problem, we launched the open supply Amazon Kinesis Knowledge Streams Connector for Spark Structured Streaming that helps enhanced fan-out for devoted learn throughput. Appropriate with each provisioned and on-demand Kinesis Knowledge Streams, enhanced fan-out gives every client with devoted throughput of two MBps per shard. This permits streaming jobs to course of information concurrently with out the constraints of shared throughput, considerably decreasing latency and facilitating close to real-time processing of enormous information streams. By eliminating competitors between customers and enhancing parallelism, enhanced fan-out gives sooner, extra environment friendly information processing, which boosts the general efficiency of streaming jobs on EMR Serverless. Beginning with Amazon EMR 7.1, the connector comes pre-packaged on EMR Serverless, so that you don’t have to construct or obtain any packages.
The next diagram illustrates the structure utilizing shared throughput.
The next diagram illustrates the structure utilizing enhanced fan-out and devoted throughput.

Check with Construct Spark Structured Streaming functions with the open supply connector for Amazon Kinesis Knowledge Streams for extra particulars on this connector.
Value optimization
EMR Serverless fees are primarily based on the entire vCPU, reminiscence, and storage assets utilized through the time employees are energetic, from when they’re able to execute duties till they cease. To optimize prices, it’s essential to scale streaming jobs successfully. We have now launched the next enhancements to enhance scaling at each the duty degree and throughout a number of duties.
Superb-Grained Scaling
In sensible eventualities, information volumes could be unpredictable and exhibit sudden spikes, necessitating a platform able to dynamically adjusting to workload adjustments. EMR Serverless eliminates the dangers of over- or under-provisioning assets to your streaming workloads. EMR Serverless scaling makes use of Spark dynamic allocation to accurately scale the executors based on demand. The scalability of a streaming job can also be influenced by its information supply to verify Kinesis shards or Kafka partitions are additionally scaled accordingly. Every Kinesis shard and Kafka partition corresponds to a single Spark executor core. To attain optimum throughput, use a one-to-one ratio of Spark executor cores to Kinesis shards or Kafka partitions.
Streaming operates by way of a sequence of micro-batch processes. In circumstances of short-running duties, overly aggressive scaling can result in useful resource wastage because of the overhead of allocating executors. To mitigate this, think about modifying spark.dynamicAllocation.executorAllocationRatio. The cutting down course of is shuffle conscious, avoiding executors holding shuffle information. Though this shuffle information is often topic to rubbish assortment, if it’s not being cleared quick sufficient, the spark.dynamicAllocation.shuffleTracking.timeout setting could be adjusted to find out when executors ought to be timed out and eliminated.
Let’s look at fine-grained scaling with an instance of a spiky workload the place information is periodically ingested, adopted by idle intervals. The next graph illustrates an EMR Serverless streaming job processing information from an on-demand Kinesis information stream. Initially, the job handles 100 data per second. As duties queue, dynamic allocation provides capability, which is rapidly launched on account of brief process durations (adjustable utilizing executorAllocationRatio). Once we improve enter information to 10,000 data per second, Kinesis provides shards, triggering EMR Serverless to provision extra executors. Cutting down occurs as executors full processing and are launched after the idle timeout (spark.dynamicAllocation.executorIdleTimeout, default 60 seconds), leaving solely the Spark driver working through the idle window. (Full scale-down is supply dependent. For instance, a provisioned Kinesis information stream with a hard and fast variety of shards could have limitations in absolutely cutting down even when shards are idle.) This sample repeats as bursts of 10,000 data per second alternate with idle intervals, permitting EMR Serverless to scale assets dynamically. This job makes use of the next configuration:

Resiliency
EMR Serverless ensures resiliency in streaming jobs by leveraging automated restoration and fault-tolerant architectures
Constructed-in Availability Zone resiliency
Streaming functions drive important enterprise operations like fraud detection, real-time analytics, and monitoring methods, making any downtime significantly expensive. Infrastructure failures on the Availability Zone degree may cause important disruptions to distributed streaming functions, doubtlessly resulting in prolonged downtime and information processing delays.
Amazon EMR Serverless now addresses this problem with built-in Availability Zone failover capabilities: jobs are initially provisioned in a randomly chosen Availability Zone, and, within the occasion of an Availability Zone failure, the service robotically retries the job in a wholesome Availability Zone, minimizing interruptions to processing. Though this characteristic enormously enhances utility reliability, attaining full resiliency requires enter information sources that additionally help Availability Zone failover. Moreover, in case you’re utilizing a customized digital personal cloud (VPC) configuration, it is suggested to configure EMR Serverless to function throughout a number of Availability Zones to optimize fault tolerance.
The next diagram illustrates a pattern structure.

Auto retry
Streaming functions are inclined to varied runtime failures brought on by transient points reminiscent of community connectivity issues, reminiscence stress, or useful resource constraints. With out correct retry mechanisms, these momentary failures can result in completely stopping jobs, requiring guide intervention to restart the roles. This not solely will increase operational overhead but additionally dangers information loss and processing gaps, particularly in steady information processing eventualities the place sustaining information consistency is essential.
EMR Serverless streamlines this course of by robotically retrying failed jobs. Streaming jobs use checkpointing to periodically save the computation state to Amazon Easy Storage Service (Amazon S3), permitting failed jobs to restart from the final checkpoint, minimizing information loss and reprocessing time. Though there isn’t any cap on the entire variety of retries, a thrash prevention mechanism permits you to configure the variety of retry makes an attempt per hour, starting from 1–10, with the default being set to 5 makes an attempt per hour.
See the next instance code:
Observability
EMR Serverless gives strong log administration and enhanced monitoring, enabling customers to effectively troubleshoot points and optimize the efficiency of streaming jobs.
Occasion log rotation and compression
Spark streaming functions repeatedly course of information and generate substantial quantities of occasion log information. The buildup of those logs can devour important disk area, doubtlessly resulting in degraded efficiency and even system failures on account of disk area exhaustion.
Log rotation mitigates these dangers by periodically archiving outdated logs and creating new ones, thereby sustaining a manageable measurement of energetic log information. Occasion log rotation is enabled by default for each batch in addition to streaming jobs and may’t be disabled. Rotating logs doesn’t have an effect on the logs uploaded to the S3 bucket. Nonetheless, they are going to be compressed utilizing zstd commonplace. You’ll find rotated occasion logs below the next S3 folder:
The next desk summarizes key configurations that govern occasion log rotation.
| Configuration | Worth | Remark |
| spark.eventLog.rotation.enabled | TRUE | |
| spark.eventLog.rotation.interval | 300 seconds | Specifies time interval for the log rotation |
| spark.eventLog.rotation.maxFilesToRetain | 2 | Specifies what number of rotated log information to maintain throughout cleanup |
| spark.eventLog.rotation.minFileSize | 1 MB | Specifies a minimal file measurement to rotate the log file |
Software log rotation and compression
One of the widespread errors in Spark streaming functions is the no area left on disk errors, primarily brought on by the speedy accumulation of utility logs throughout steady information processing. These Spark streaming utility logs from drivers and executors can develop exponentially, rapidly consuming accessible disk area.
To handle this, EMR Serverless has launched rotation and compression for driver and executor stderr and stdout logs. Log information are refreshed each 15 seconds and may vary from 0–128 MB. You’ll find the newest log information on the following Amazon S3 areas:
Rotated utility logs are pushed to archive accessible below the next Amazon S3 areas:
Enhanced monitoring
Spark gives complete efficiency metrics for drivers and executors, together with JVM heap reminiscence, rubbish assortment, and shuffle information, that are precious for troubleshooting efficiency and analyzing workloads. Beginning with Amazon EMR 7.1, EMR Serverless integrates with Amazon Managed Service for Prometheus, enabling you to observe, analyze, and optimize your jobs utilizing detailed engine metrics, reminiscent of Spark occasion timelines, phases, duties, and executors. This integration is offered when submitting jobs or creating functions. For setup particulars, seek advice from Monitor Spark metrics with Amazon Managed Service for Prometheus. To allow metrics for Structured Streaming queries, set the Spark property --conf spark.sql.streaming.metricsEnabled=true
It’s also possible to monitor and debug jobs utilizing the Spark UI. The net UI presents a visible interface with detailed details about your working and accomplished jobs. You may dive into job-specific metrics and details about occasion timelines, phases, duties, and executors for every job.
Service integrations
Organizations usually wrestle with integrating a number of streaming information sources into their information processing pipelines. Managing totally different connectors, coping with various protocols, and offering compatibility throughout numerous streaming platforms could be complicated and time-consuming.
EMR Serverless helps Kinesis Knowledge Streams, Amazon MSK, and self-managed Apache Kafka clusters as enter information sources to learn and course of information in close to actual time.
Whereas the Kinesis Knowledge Streams connector is natively accessible on Amazon EMR, the Kafka connector is an open supply connector from the Spark neighborhood and is offered in a Maven repository.
The next diagram illustrates a pattern structure for every connector.

Check with Supported streaming connectors to study extra about utilizing these connectors.
Moreover, you’ll be able to seek advice from the aws-samples GitHub repo to arrange a streaming job studying information from a Kinesis information stream. It makes use of the Amazon Kinesis Knowledge Generator to generate check information.
Conclusion
Operating Spark Structured Streaming on EMR Serverless provides a sturdy and scalable answer for real-time information processing. By benefiting from the seamless integration with AWS providers like Kinesis Knowledge Streams, you’ll be able to effectively deal with streaming information with ease. The platform’s superior monitoring instruments and automatic resiliency options present excessive availability and reliability, minimizing downtime and information loss. Moreover, the efficiency optimizations and cost-effective serverless mannequin make it a perfect selection for organizations seeking to harness the ability of close to real-time analytics with out the complexities of managing infrastructure.
Check out utilizing Spark Structured Streaming on EMR Serverless to your personal use case, and share your questions within the feedback.
In regards to the Authors
Anubhav Awasthi is a Sr. Large Knowledge Specialist Options Architect at AWS. He works with prospects to offer architectural steerage for working analytics options on Amazon EMR, Amazon Athena, AWS Glue, and AWS Lake Formation.
Kshitija Dound is an Affiliate Specialist Options Architect at AWS primarily based in New York Metropolis, specializing in information and AI. She collaborates with prospects to remodel their concepts into cloud options, utilizing AWS massive information and AI providers. In her spare time, Kshitija enjoys exploring museums, indulging in artwork, and embracing NYC’s out of doors scene.
Paul Min is a Options Architect at AWS, the place he works with prospects to advance their mission and speed up their cloud adoption. He’s enthusiastic about serving to prospects reimagine what’s potential with AWS. Outdoors of labor, Paul enjoys spending time together with his spouse and {golfing}.
