Challenge - 5 Problems
Skew Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of join with skewed key using salting
Consider two Spark DataFrames
df1 and df2 where df1 has a highly skewed key 'user_id'. The code below applies salting to handle skew during join. What is the count of rows in the joined DataFrame joined_df?Apache Spark
from pyspark.sql import functions as F # Sample data skewed_data = [(1, 'A')] * 1000 + [(2, 'B')] * 10 other_data = [(1, 'X')] * 1000 + [(3, 'Y')] * 5 df1 = spark.createDataFrame(skewed_data, ['user_id', 'val1']) df2 = spark.createDataFrame(other_data, ['user_id', 'val2']) # Add salt column to df1 salt_range = 10 salted_df1 = df1.withColumn('salt', (F.rand() * salt_range).cast('int')) # Add salt column to df2 by exploding salt values salt_values = spark.range(salt_range).withColumnRenamed('id', 'salt') salted_df2 = df2.crossJoin(salt_values) # Join on user_id and salt joined_df = salted_df1.join(salted_df2, ['user_id', 'salt']) result_count = joined_df.count()
Attempts:
2 left
💡 Hint
Think about how salting multiplies the number of matching rows for the skewed key.
✗ Incorrect
The skewed key 'user_id' = 1 appears 1000 times in df1 and 1000 times in df2. After salting with 10 salt values, each row in df2 is duplicated 10 times with different salt values. The join matches on both user_id and salt, so for user_id=1, the join produces approximately 1000 rows in df1 distributed across 10 salt values, and 1000 rows in df2 duplicated 10 times with salt values. The expected total join rows for user_id=1 is about 10000. For other keys, the join count is smaller but does not affect the total significantly.
🧠 Conceptual
intermediateIdentifying data skew in Spark jobs
Which of the following is the most reliable sign of data skew causing performance issues in a Spark job?
Attempts:
2 left
💡 Hint
Think about how skew affects task durations during shuffle.
✗ Incorrect
Data skew causes some partitions to be much larger, making tasks processing them take longer. This results in uneven task durations visible in shuffle stages in Spark UI.
🔧 Debug
advancedDebugging skew handling with broadcast join
The code below attempts to fix skew by broadcasting the smaller DataFrame. However, it still runs slowly. What is the main reason?
Apache Spark
from pyspark.sql import functions as F large_df = spark.range(1000000).withColumn('key', (F.rand() * 10).cast('int')) small_df = spark.range(10).withColumnRenamed('id', 'key') # Attempt broadcast join joined = large_df.join(F.broadcast(small_df), 'key') result = joined.count()
Attempts:
2 left
💡 Hint
Consider what causes skew and how broadcast join works.
✗ Incorrect
Broadcast join avoids shuffle on the small DataFrame, but if the large DataFrame has skewed keys, some partitions remain large and cause slow tasks.
🚀 Application
advancedChoosing the best skew mitigation technique
You have a Spark job joining two large DataFrames on a key with heavy skew. The skewed key appears in 50% of the data. Which approach is best to reduce job runtime?
Attempts:
2 left
💡 Hint
Think about isolating skewed keys to handle them differently.
✗ Incorrect
Filtering out skewed keys and processing them separately allows tailored handling, reducing skew impact. Salting helps but is less effective when skewed keys dominate half the data.
❓ visualization
expertInterpreting Spark UI task duration histogram for skew detection
You see the following histogram of task durations for a shuffle stage in Spark UI: most tasks finish in under 10 seconds, but a few tasks take over 5 minutes. What does this indicate?
Attempts:
2 left
💡 Hint
Look at the distribution of task durations and what skew causes.
✗ Incorrect
A few very long tasks among many short ones is a classic sign of data skew, where some partitions have much more data.
