Lesson 2 of 3 Amazon EMR

Optimising EMR Performance and Cost

This lesson explains the levers that make EMR workloads cheaper or faster, since exam questions usually ask for the cheapest or fastest way to run a workload.

Learning outcomes

After completing this lesson, you will be able to:

  • Reduce cost with Spot Instances, instance fleets and managed scaling.
  • Tune Spark executors and partitions.
  • Improve performance through data layout.
  • Troubleshoot jobs with archived logs and the Spark History Server.

Reducing cost

The largest EMR saving usually comes from Spot Instances, which cost far less than On-Demand capacity but can be reclaimed. Task nodes, which only compute, are ideal for Spot, while primary and core nodes, whose loss would disrupt the cluster or lose data, stay On-Demand or Reserved. Instance fleets diversify Spot capacity across several instance types and Availability Zones, which reduces the chance of interruptions.

Graviton instances give better price performance for most Spark workloads. EMR managed scaling resizes clusters to match the workload, within limits the customer sets. Finally, transient clusters, which start for a job and terminate afterwards, and EMR Serverless avoid paying for idle capacity; both depend on keeping data in Amazon S3 rather than in cluster storage.

The following diagram compares a day of an always-on 20-node EMR cluster with a transient, cheaper design.

The 20-node cluster before and after. Before, On-Demand nodes run around the clock for six hours of jobs. After, the scheduler creates a transient cluster for the jobs and terminates it, primary and core nodes stay On-Demand, task nodes run on Spot with instance fleets on Graviton types, and managed scaling adds task nodes only while work is queued.
The job window is illustrative.
Select all that apply

Lowering EMR cost

Exam skills 1.2.4

Identify the effective ways to lower EMR cost.

Log in to try this activity Interactive activities are free with an account.

Tuning Spark

Spark performance depends on a few configuration and coding choices. Executors, the processes that run tasks, should be sized with sensible memory and core counts for the instance type, and the number of partitions set so that each task processes a reasonable amount of data. Adaptive query execution adjusts partition counts and join strategies at run time based on actual data sizes, and should be enabled.

Broadcast joins send small tables to every executor, avoiding a shuffle of the large table. Data should be cached only when it is reused, because caching consumes memory that tasks could otherwise use. Running jobs on the EMR runtime for Apache Spark adds performance optimisations that need no code changes.

Match the pairs

Spark tuning

Exam skills 1.4.1

Match each Spark problem with its tuning choice.

Log in to try this activity Interactive activities are free with an account.

Data layout

The layout of data in storage often matters more than cluster tuning. Columnar formats, such as Parquet and ORC, with compression let Spark read only the columns it needs. Partitioning by common filters lets jobs skip irrelevant data. Files of roughly 128 MB to 1 GB avoid the overhead of many small files while keeping enough parallelism.

When writing, the EMR S3-optimized committer speeds up writes of Parquet output to Amazon S3 by avoiding the slow rename operations that the default Hadoop committer performs, and it reduces the risk of incomplete output if a job fails.

True or false

Data layout for Spark

Exam skills 2.4.5 · 1.4.1

Decide whether each statement about data layout is true.

Log in to try this activity Interactive activities are free with an account.

Troubleshooting

EMR keeps the information needed to troubleshoot jobs even after clusters terminate. Step and container logs are archived in Amazon S3. The Spark History Server and YARN user interfaces remain available as persistent application user interfaces, showing each job's stages and tasks, and CloudWatch provides cluster and application metrics.

Data skew is one of the most common performance problems: it appears as a few tasks in a stage that run much longer than the rest, because one key holds a disproportionate share of the data. It is fixed by salting the hot key or repartitioning the data, as described in the lesson Detecting and Fixing Data Skew.

Worked example

Halving a cluster's cost

A 20-node EMR cluster runs On-Demand instances around the clock, but jobs run for only 6 hours a night.

ChangeEffect
Transient cluster created by the scheduler, terminated after the jobsPays for 6 hours instead of 24
Primary and core nodes On-Demand; task nodes on Spot with instance fleetsUp to 90% off the task nodes; Spot loss does not lose HDFS data
Graviton instance typesBetter price performance
Managed scalingAdds task nodes only while work is queued

Task nodes are the safe place for Spot because they hold no HDFS data. A job that is slow because one Spark task processes far more data than the others is a skew problem, fixed by repartitioning or salting the join key rather than by adding nodes.

Check yourself

Which feature resizes an EMR cluster automatically?

Show answer

EMR managed scaling, which adds and removes capacity based on the workload.

Exam Tips

Remember for the exam

  • The lowest-cost compute for task nodes is Spot Instances, with instance fleets.
  • To resize clusters automatically, use EMR managed scaling.
  • To avoid paying for idle clusters, use transient clusters or EMR Serverless.
  • Keep data in S3 so that clusters can be terminated.
  • Faster queries come from columnar formats, partitioning and fewer, larger files.
  • A few tasks running much longer than the rest indicate data skew.