Spark Illuminated

Spark Illuminated

Unveiling Key Concepts for Effective Data Manipulation

Dataframes vs Resilient Distributed Datasets

In the first article, I explained Dataframes in detail, and in the second, I talked about Resilient Distributed Datasets. But what exactly sets them apart?

Here's a table that summarizes the key differences between RDDs and DataFrames in Apache Spark, along with examples for each:

Abstraction LevelLow-level, providing fine-grained control over data and operations.High-level, providing a more structured and higher-level API.
OptimizationNo automatic optimization. Operations are executed as they are defined.Uses the Catalyst optimizer for logical and physical plan optimizations.
Ease of UseRequires more detailed and complex syntax for operations.Simplified and more intuitive operations similar to SQL.
InteroperabilityPrimarily functional programming interfaces in Python, Scala, and Java.Supports SQL queries, Python, Scala, Java, and R APIs.
Typed and Untyped APIsProvides a type-safe API (especially in Scala and Java).Offers mostly untyped APIs, except when using Datasets in Scala and Java.
PerformanceMay require manual optimization, like coalescing partitions. *Generally faster due to Catalyst optimizer and Tungsten execution engine. *
Fault ToleranceAchieved through lineage information allowing lost data to be recomputed.Also fault-tolerant through Spark SQL's execution engine.
Use CasesSuitable for complex computations, fine-grained transformations, and when working with unstructured data.Best for standard data processing tasks on structured or semi-structured data, benefiting from built-in optimization.


* Will be covered in future articles

Examples in Context

  • RDD Example: Suppose you have a list of sales transactions and you want to filter out transactions with a value greater than 100.

      transactions_rdd = sc.parallelize([(1, 150), (2, 75), (3, 200)])
      filtered_rdd = transactions_rdd.filter(lambda x: x[1] > 100)
  • DataFrame Example: Achieving the same with a DataFrame:

      transactions_df = spark.createDataFrame([(1, 150), (2, 75), (3, 200)], ["id", "amount"])
      filtered_df = transactions_df.filter("amount > 100")

The choice between RDDs and DataFrames depends on the specific needs of your application, such as the need for custom, complex transformations (favouring RDDs) or for optimized, structured data processing (favoring DataFrames).

Understanding Joins in Apache Spark

Let's break down the concept of joins in Apache Spark, starting with the basics and moving towards how to optimize these operations for efficiency.

Introduction to Joins

If you've spent some time in the data domain, you might already be familiar with Joins. These allow us to merge datasets based on common attributes.

Spark supports several types of joins to cater to various needs:

  • Inner Join: Retains only the row pairs with matching IDs from both tables.

  • Left Join: Retains all rows from the left table, along with their matches from the right table.

  • Full Outer Join: Merges the outcomes of both left and right joins, keeping all rows from both tables.

  • Cross Join: Generates a Cartesian product, pairing every row from the first table with every row from the second table.

I won't be spending too much time on this topic (SQL basics) and instead will move into how Spark deals with Joins.

How Joins Work in Spark

To perform these joins, Spark may need to shuffle data. This shuffling, a mix of network and sort operations, aligns matching rows for the join but can be resource-intensive. I will go into detail on Shuffling in further articles on Spark Optimisation. But, for now, I want you to keep in mind that:

  • Shuffle Process: Spark redistributes data across its distributed system to align matching rows, ensuring all data with the same key (ID) are in the same partition for joining.

  • Impact on Performance: Shuffling involves moving data across the network, which can be a bottleneck. Hence, optimizing this process is crucial for performance.

What if, instead of optimizing data transfer between nodes (Node-to-node communication strategy), we focus on maximizing the efficiency of data processing within each individual node by bringing the data into each node (per node computation strategy)?

Broadcast Joins

A broadcast join in Apache Spark is an optimization technique for join operations that can significantly improve performance, especially when one of the datasets involved in the join is much smaller than the other. To understand broadcast joins deeply, let’s explore their mechanism, advantages, and when to use them, using an analogy and then delving into the technical details.

The Broadcast Join Explained

Imagine you’re a teacher in a large school, and you have a small list of special announcements that you need to share with every classroom. Instead of asking every classroom to come to you (which would be chaotic and inefficient), you make copies of the announcements and deliver them to each classroom. Now, every classroom has direct access to the announcements, making the whole process smoother and faster.

In Apache Spark, a similar approach is taken with broadcast joins. Instead of moving large amounts of data across the network to perform a join, the smaller dataset is sent (broadcast) to every node in the cluster where the larger dataset resides. This eliminates the need for shuffling the larger dataset across the network, significantly reducing the amount of data transfer and, consequently, the time taken to perform the join.

In-depth Mechanics of Broadcast Joins

  1. Identification: Spark automatically identifies when one side of the join operation is small enough to be broadcasted based on the spark.sql.autoBroadcastJoinThreshold configuration setting. This setting specifies the maximum size (in bytes) of a table that can be broadcast.

  2. Broadcasting: The smaller dataset is broadcasted to all executor nodes in the Spark cluster. This involves serializing the dataset and sending it over the network to each node.

  3. Local Join Operation: Once the smaller dataset is on every node, Spark performs the join operation locally, combining it with the relevant partition of the larger dataset that resides on the node. Since the data is located together, this operation is very efficient and avoids the costly shuffle operation that would otherwise be necessary.

To illustrate the implementation of a broadcast join in Apache Spark using PySpark (the Python API for Spark), let's consider a simple example. Suppose we have two datasets: a large datasetemployees with employee details, and a smaller datasetdepartments that contains department names and their IDs. Our goal is to use a broadcast join to efficiently join these two datasets on the department ID, ensuring that each employee record is enriched with the department name.

from pyspark.sql import SparkSession
from pyspark.sql.functions import broadcast

# Initialize a SparkSession
spark = SparkSession.builder \
    .appName("Broadcast Join Example") \

# Example datasets
employees_data = [
    (1, "John Doe", 2),
    (2, "Jane Doe", 1),
    (3, "Mike Brown", 2),
    # .... Assume more records

departments_data = [
    (1, "Human Resources"),
    (2, "Technology"),
    # .... Assume more departments

# Creating DataFrames
employees_df = spark.createDataFrame(employees_data, ["emp_id", "name", "dept_id"])
departments_df = spark.createDataFrame(departments_data, ["dept_id", "dept_name"])

# Performing a broadcast join (inner)
# Broadcasting the smaller DataFrame (departments_df)
joined_df = employees_df.join(broadcast(departments_df), employees_df.dept_id == departments_df.dept_id, "inner")

# Show the result of the join

# Stop the SparkSession

Advantages of Broadcast Joins

  • Performance: The primary advantage is the significant reduction in network traffic, which can greatly improve the performance of join operations.

  • Efficiency: By avoiding shuffling, broadcast joins reduce the load on the network and the cluster, making the join process more efficient.

  • Scalability: For joins with significantly disparate dataset sizes, broadcast joins allow Spark to scale more effectively, handling large datasets more efficiently by leveraging the parallel processing capabilities of the cluster.

Use Cases

Broadcast joins are particularly effective in scenarios where:

  • One dataset is much smaller than the other and can fit into the memory of each node.

  • The smaller dataset is used repeatedly in join operations with different larger datasets, making the cost of broadcasting it justified over several operations.

Guidelines for Use

  • Size Consideration: Be mindful of the spark.sql.autoBroadcastJoinThreshold setting. If the smaller dataset is close to this size limit, consider manually broadcasting it if it's beneficial.

  • Memory Management: Ensure that there is sufficient memory on each node to accommodate the broadcasted data without impacting the execution of other tasks.

In summary, broadcast joins are a powerful optimization in Apache Spark that can lead to significant performance improvements in distributed data processing tasks, particularly when the size disparity between joining datasets is large. Understanding when and how to use broadcast joins can greatly enhance the efficiency of Spark applications.

Other Advanced Joins

Advanced join techniques in Apache Spark are designed to optimize the performance of join operations beyond the basic and broadcast join strategies. These techniques can significantly reduce the computational and memory overhead associated with joins, especially when dealing with large datasets. Let’s dive into some of these advanced techniques:

Sort-Merge Join

A sort-merge join is a method that Spark may choose when both sides of the join have already been sorted on the join key. This technique involves two main steps:

  • Sorting: If the datasets are not already sorted, they are sorted by the join key.

  • Merging: The sorted datasets are then merged together. Because the data is sorted, Spark can efficiently merge the two datasets by sequentially scanning through them, significantly reducing the need for shuffling data across the network.

Use Cases: Sort-merge joins are particularly effective when dealing with large datasets that are already sorted or when the cost of sorting the data is offset by the reduced need for shuffling during the join.

# Assuming df1 and df2 are the DataFrames to join
df1_sorted = df1.sort("joinKey")
df2_sorted = df2.sort("joinKey")

# Perform the join
joined_df = df1_sorted.join(df2_sorted, df1_sorted.joinKey == df2_sorted.joinKey)

Shuffle Hash Join

The shuffle hash join is another technique that Spark might use, especially when one dataset is much larger than the other but not small enough for a broadcast join. This method works by:

  • Shuffling: The smaller dataset is shuffled (partitioned) across the cluster based on the join key.

  • Hashing: Spark then builds an in-memory hash table of the smaller dataset on each partition.

  • Joining: The larger dataset is scanned partition-wise, and for each row, Spark uses the hash table to quickly find matching rows from the smaller dataset.

Use Cases: This method is efficient when one dataset is moderately small, and its hash table can fit into the memory of each node, allowing for fast lookups.

# Setting these parametersis is more about influencing Spark's 
# choice rather than directly enforcing a Shuffle Hash Join.
spark.conf.set("spark.sql.join.preferSortMergeJoin", "false")
spark.conf.set("spark.sql.autoBroadcastJoinThreshold", "-1")

# Perform the join, Spark might use Shuffle Hash Join depending 
# on data size and other conditions
joined_df = df1.join(df2, ["joinKey"])

Bucketed Joins

Bucketed joins in Spark leverage bucketing, a technique where data is divided based on a join key:

  • Bucketing: Data is split into buckets based on a hash of the join key, with each bucket containing a subset of data. This is done during data ingestion. To understand this better, refer to my previous article where I discuss bucketing in detail.

  • Joining: When performing a join on bucketed tables, Spark can avoid shuffling by directly joining buckets with matching keys.

Use Cases: Bucketed joins are most effective when dealing with frequent joins on large datasets where the join keys are known ahead of time. By avoiding shuffling, they can significantly improve join performance.

# Bucketed Joins require your data to be bucketed and saved 
# to disk beforehand
df1.write.bucketBy(42, "joinKey").sortBy("joinKey").saveAsTable("df1_bucketed")
df2.write.bucketBy(42, "joinKey").sortBy("joinKey").saveAsTable("df2_bucketed")

# Read bucketed data
df1_bucketed = spark.table("df1_bucketed")
df2_bucketed = spark.table("df2_bucketed")

# Perform the bucketed join without shuffling
joined_bucketed_df = df1_bucketed.join(df2_bucketed, "joinKey")

Here, 42 specifies the number of buckets to create, and "joinKey" is the column used for bucketing. This means that rows with the same value in the joinKey column will end up in the same bucket.

Skew Join Optimization

Data skew occurs when the distribution of values within a dataset is uneven, leading to some tasks taking much longer than others. Spark offers optimizations for skew joins:

  • Detecting and Handling Skew: Spark can detect skewed keys and replicate the skewed partition's data to all tasks, allowing each task to process a portion of the skewed data in parallel.

  • Salting: A common technique to manually handle skew involves "salting" the join keys by adding a random value, thus distributing the skewed keys across multiple partitions to balance the load. This process creates multiple variations of each key, distributing the data more evenly across the cluster.

Use Cases: Skew join optimizations are crucial for performance when joins involve skewed datasets, helping to distribute the load more evenly across the cluster.

The following steps describe how you can perform the Skew Join:

  1. Adding Salt to Join Keys
df1 = df1.withColumn("salt", (rand()*5).cast("int"))
df2 = df2.withColumn("salt", monotonically_increasing_id() % 5)

Fordf1: A new column named salt is added. This column is populated with random integers between 0 and 4, inclusive. The rand() function generates a random floating-point number between 0 and 1, which is then multiplied by 5 to scale it. Casting it to an integer effectively creates a random salt value within the desired range.

Fordf2: Similarly, a salt column is added. Here, monotonically_increasing_id() generates a unique, monotonically increasing integer for each row. Taking this value modulo 5 ensures that the salt values are distributed between 0 and 4, much like in df1, but in a sequential and deterministic manner rather than randomly.

  1. Replicating Skewed Data indf2
from pyspark.sql.functions import concat, col
from pyspark.sql.functions import lit

# Create a DataFrame with Salt Values
salt_values = spark.range(5).withColumnRenamed("id", "salt")

# Cross-Join df2 with the Salt Values DataFrame
df1_cross_joined = df1.crossJoin(salt_values)
df2_cross_joined = df2.crossJoin(salt_values)

# Modify the Join Key in Both DataFrames to Include the Salt
df1_cross_joined = df1_cross_joined.withColumn("saltedJoinKey", concat(col("joinKey"), lit("_"), col("salt")))
df2_cross_joined = df2_cross_joined.withColumn("saltedJoinKey", concat(col("joinKey"), lit("_"), col("salt")))

This operation is intended to replicate each row of df2 five times, with each replication having a different salt value from 0 to 4. The flatMap function is used to expand each original row into multiple rows, one for each salt value. This approach ensures that when joining df1 and df2, the join operation is not bottlenecked by any single value of joinKey since the data corresponding to each joinKey in df2 is now evenly distributed across all salt values.

  1. Performing the Join with the Salted Column
joined_skewed_df = df1_cross_joined.join(df2_cross_joined, df1_cross_joined.saltedJoinKey == df2_cross_joined.saltedJoinKey)

The join operation now includes a condition which ensures that rows are matched by their salt value, effectively distributing the join operation more evenly across the cluster. But, make a note to always test and monitor the performance impact when using this technique since Cross-joins can significantly increase the volume of data being processed, and hence, should be used judiciously, especially with large datasets.

Implementing these advanced join techniques requires a good understanding of the data and the specific computational challenges it presents. Spark attempts to automatically choose the most efficient join strategy based on the data characteristics and the available cluster resources. However, for optimal performance, developers might need to manually adjust configurations, such as enabling or tuning specific join optimizations based on their understanding of the data and the nature of the join operation.

Leveraging Shared Variables in Apache Spark

Apache Spark provides two types of shared variables to support different use cases when tasks across multiple nodes need to share data: broadcast variables and accumulators. Let's explore each type in detail.

Introduction to Shared Variables

In distributed computing, operations are executed across many nodes, and sometimes these operations need to share some state or data among them. However, Spark's default behavior is to execute tasks on cluster nodes in an isolated manner. Shared variables come into play to allow these tasks to share data efficiently.

Use Cases: Shared variables are used when you need to:

  • Share a read-only variable with tasks across multiple nodes efficiently.

  • Accumulate results from tasks across multiple nodes in a central location.

Broadcast Variables

Broadcast variables allow the program to efficiently send a large, read-only variable to all worker nodes for use in one or more Spark operations.


  • Efficiency: Significantly reduces the amount of data that needs to be shipped to each node.

  • Performance: Can cache the data in memory on each node, avoiding the need to re-send the data for each action.

Example Use Case

Suppose you have a large lookup table that tasks across multiple nodes need to reference. Instead of sending this table with every task, you can broadcast it so that each node has a local copy.

from pyspark import SparkContext
sc = SparkContext()

lookup_table = {"id1": "value1", "id2": "value2"}  # Large lookup table
broadcastVar = sc.broadcast(lookup_table)

data = sc.parallelize(["id1", "id2", "id1"])
result = x: (x, broadcastVar.value.get(x))).collect()
# Output: [("id1", "value1"), ("id2", "value2"), ("id1", "value1")]


Accumulators are variables that are only "added" to through an associative and commutative operation and can therefore be efficiently supported in parallel. They can be used to implement counters or sums.


  • Central Aggregation: Provide a simple syntax for aggregating values across multiple tasks.

  • Fault Tolerance: If a task fails and is re-executed, its updates to the accumulator are not reapplied, thus maintaining accuracy.

Example Use Case

You want to count

  • how many times each key appears across all partitions of a dataset, or

  • debug by counting how many records were processed or

  • how many had missing information.

from pyspark import SparkContext
sc = SparkContext()

data = sc.parallelize([1, 2, 3, 4, 5])
counter = sc.accumulator(0)

def count_function(x):
    global counter
    counter += x

# Output will be the sum of the numbers in the dataset: 15

Both broadcast variables and accumulators provide essential capabilities for optimizing and managing state in distributed computations with Apache Spark, facilitating efficient data sharing and aggregation across the cluster.


We've covered the various abstractions in PySpark, including DataFrames and RDDs. But what about those not native to PySpark?

Datasets are a strongly-typed collection of distributed data. They extend the functionality of DataFrames by adding compile-time type safety. This is particularly useful in Scala due to its static type system, where type information is known and checked at compile time.

Note - To simplify the comparison, imagine Python as similar to JavaScript, and Scala as akin to TypeScript, especially if you're familiar with application development (Only in terms of type strictness).

Key Features

  • Type Safety: Datasets provide compile-time type safety. This means errors like accessing a non-existent column name or performing operations with incompatible data types can be caught during compilation rather than at runtime.

  • Lambda Functions: Similar to RDDs, Datasets allow you to manipulate data using functional programming constructs. However, unlike RDDs, Datasets benefit from Spark SQL's optimization strategies (Catalyst optimizer).

  • Interoperability with DataFrames: A Dataset can be considered a typed DataFrame. In fact, DataFrame is just a type alias for Dataset[Row], where Row is a generic untyped JVM object. This allows seamless conversion and operation between typed Datasets and untyped DataFrames.

Example Usage in Scala

Don't worry too much about Scala or the syntax. Just focus on the comments to understand the code's purpose.

import spark.implicits._

// Define a case class that represents the schema of your data
case class Person(name: String, age: Long)

// Creating a Dataset from a Seq collection
val peopleDS = Seq(Person("Alice", 30), Person("Bob", 25)).toDS()

// Perform transformations using lambda functions
val adultsDS = peopleDS.filter(_.age >= 18)

// Show results

// Is 25 really adulting though?


Encoders are what Spark uses under the hood to convert between Java Virtual Machine (JVM) objects and Spark SQL's internal tabular format. This conversion is necessary for Spark to perform operations on data within Datasets efficiently. I'll talk more on JVM in the future articles.

Key Features

  • Efficiency: Encoders are responsible for Spark's ability to handle serialization and deserialization cost-effectively. They allow Spark to compress data into a binary format and operate directly on this binary representation, reducing memory usage and improving processing speed.

  • Custom Objects: For custom objects, like the Person case class in the example above, Spark requires an implicit Encoder to be available. Scala's implicits and the Datasets API make working with custom types seamless.

Example Usage in Scala

Implicitly, Encoders are used in the Dataset operations shown above. However, when creating Datasets from RDDs or dealing with more complex types, you might need to provide an encoder explicitly:

import org.apache.spark.sql.Encoders

// Explicitly specifying an encoder for a Dataset of type String
val stringEncoder = Encoders.STRING

val namesDS = spark.sparkContext
  .parallelize(Seq("Alice", "Bob"))
  .toDS()(stringEncoder) // Applying the encoder explicitly

For a PySpark user, understanding Datasets and Encoders can offer insights into how Spark manages type safety, serialization, and optimization at a deeper level. While PySpark leverages dynamic typing and does not directly expose Datasets, the principles of efficient data representation, processing, and the ability to perform strongly-typed operations are universally beneficial across Spark's API ecosystem.