At Spark+AI conference this year, Daniel Tomes from Databricks gave a deep-dive talk on Spark performance optimizations. After watching it, I feel it’s super useful, so I decide to write down some important notes which address the most common performance issues from his talk.
Here is the YouTube video just in case if you are interested. https://www.youtube.com/watch?v=daXEp4HmS-E
We often encounter into situations that partition is not optimal at different stages of our workflow, so it slows down the entire job siganificantly. For example, six month ago I tried to analyze some telemetry data exported from Application Insights, but there are way to many JSON files (> 100,000 files) and each file is small (< 1MB each). This makes a
groupBy stage takes an hour to finish on 8 machines. If this is a one-time workflow, I’m okay to not optimize it. But it’s not.
Spark’s default shuffle repartition is 200 which does not work for data bigger than 20GB. So from Daniel’s talk, there is a golden equation to calculate the partition count for the best of performance.
The largest shuffle stage target size should be less than 200MB. So the partition count calculate as total size in MB divide 200.
Note: your cluster size matters as well. If your number of cores in your cluster is smaller than the partitions calculated above, you should use the number of cores. The reason is that you don’t want to waste cores during the second round of shuffle though you slightly increase the parition file size. However, if your cores are bigger than partition count from above, you might want to reduce your cluster size so that other people can benefit from it. You can also bump the partitions up to make it even faster.
To change the shuffle partition size, use the code below.
Spark partition file size is another factor you need to pay attention. The default size is 128MB per file. When you output a DataFrame to
dbfs or other storage systems, you will need to consider the size as well. So the rule of thumbs given by Daniel is the following.
Use spark default 128MB max partition bytes unless:
- You need to increase parallelism
- You have heavily nested / repetitive data
- You have data generation such as
- The source structure is not optimal.
- When you use UDF functions.
To change the max partition bytes, use the code below.
When you output files, there are many options provided by Spark, but you need to know what you are doing.
This is useful to control partitions by the records per file.
This is useful to size down the partitions. For example, the 100,000 files down to 16 files.
This is very expensive, do not use it until you really need to.
df.repartition(n, [colA, ...]).write(...)
This is required when you have a
join etc. later in your workflow. This will boost the performance a lot.
This is very helpful as it breaks the stage barrier so the parallel workflow later that uses the same DataFrame do not need to re-process the current DataFrame. Remember Spark is lazy execute,
localCheckpoint() will trigger execution to materialize the DataFrame.
Finding imbalancing is one of the most important thing in parallel processing.
If you have repetitions from your SQL plan, you can persist the DataFrame so the subsequent processing could use a materialized data. You can either use
df.persist(level). They are doing the same thing, it’s just different levels. When the workflow finished, remember to clean up by calling
df.unpersist(). Spark is not smart enough to automatically clean up the data for you.
Boradcast join if possible, but do not over use it. Broadcast join is a good technique to speed up the join. The following diagram shows you how it works.
A few things you need to pay attention when use broadcast join.
- DataFrame is bigger than the driver node’s available working memory.
- DataFrame is bigger than
- DataFrame is bigger than executor’s available working memory.
Assume you have a DataFrame that is skewed towards certain city and state. If we need aggregate some data, we need might expect long tail execution.
Databricks has an internal solution to skewed data join.
Spark community solution is the following to add a salt to be a random value between 0 and
spark.sql.shuffle.partitions - 1, then run the
groupBy and aggregation.
val saltVal = random(0, spark.conf.get("org...shuffle.partitions") - 1)
Use the Apache namespace random function as it’s a vectorized random function.
Try to avoid the following expensive operations:
coalesce()or shuffle partition count instead.
count(). Do not call it unless you need.
approxCountDistinct()instead if you can tolerate 5% error.
- If distincts are required, put them in the right place.
dropDuplicates()and use it before join and groupBy.
Traditional UDFs cannot use project Tungsten to improve the efficiency of Spark executions. Instead, you can choose the following options.
spark.sql.functionsstandard functions to solve your problem.
- Use Pandas UDF which utilizes Apache Arrow.
- Use SparkR UDF.
Rule of thumbs is to use vectorized UDFs.