Amar Prakash Pandey - ᕦ(ò_óˇ)ᕤ

Handling Large Broadcast Joins in Apache Spark


In Apache Spark, efficient data processing often relies on the use of broadcast joins. However, when the dataset exceeds a certain size, specifically 8GB, you may encounter the following error:

Caused by: org.apache.spark.SparkException: Cannot broadcast the table that is larger than 8GB: 13 GB

This error arises because Spark is attempting to broadcast a dataset that is larger than the maximum threshold allowed for broadcast joins. By default, Spark’s threshold for broadcasting is set to 8GB. If a dataset exceeds this limit, Spark will throw an exception and the job will fail. Here, we will discuss two ways to resolve this issue.

Solution 1: Adjusting the Broadcast Join Threshold

One straightforward solution is to configure Spark not to use broadcast joins for large datasets by setting the spark.sql.autoBroadcastJoinThreshold parameter. This can be done by adding the following configuration to your Spark job:

spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)

or you can pass the spark conf to the spark submit command

spark-submit --conf spark.sql.autoBroadcastJoinThreshold=-1 ...

Setting this parameter to -1 disables automatic broadcast joins, regardless of the dataset size. While this approach prevents Spark from attempting to broadcast large datasets, it has a limitation. If your code explicitly requests a broadcast join, this configuration alone will not resolve the issue. Explicit broadcast joins override this setting.

Solution 2: Removing Explicit Broadcast Joins

If your Spark application contains explicit broadcast joins, simply adjusting the configuration won’t suffice. You’ll need to modify your code to remove these explicit broadcast instructions. Here’s a brief example of how to do this:


val broadcastedTable = spark.sparkContext.broadcast(largeDataFrame)
val joinedData = smallDataFrame.join(broadcast(broadcastedTable.value), "key")


val joinedData = smallDataFrame.join(largeDataFrame, "key")

By removing the broadcast function, you let Spark decide the best join strategy based on the dataset sizes and other factors, which can then be influenced by the spark.sql.autoBroadcastJoinThreshold setting.


Dealing with large datasets in Spark requires careful consideration of join strategies. If you encounter a broadcast size limitation error, you have two primary options:

  1. Adjust the Broadcast Threshold: Set spark.sql.autoBroadcastJoinThreshold to 1 to prevent automatic broadcast joins.
  2. Remove Explicit Broadcast Joins: Modify your code to remove any explicit broadcast join instructions, ensuring that the configuration change will take effect.

By applying these solutions, you can manage large dataset joins more effectively and prevent your Spark jobs from failing due to broadcast size limitations.

Lastly, thank you for reading this post. For more awesome posts, you can also follow me on Twitter — iamarpandey, Github — amarlearning.

#Data #Apache #Spark #Broadcast #Join