Performance tuning with Apache Spark – Introduction


Welcome back to our ongoing series on Data transformation with Apache Spark! In our previous posts, we’ve covered essential topics like setting up Apache Spark on Ubuntu, integrating data with Spark, and querying datasets using Apache Drill. Now, we’re stepping into the realm of performance tuning—a critical aspect of maximizing the efficiency and throughput of your Spark applications. In this installment, we’ll look at the intricacies of fine-tuning Apache Spark for optimal performance, specifically focusing on optimizing the transformation of data from a Postgres table to Amazon S3.


Apache Spark stands as a powerhouse for processing vast volumes of data. However, harnessing its full potential requires careful calibration of various parameters. In this blog post, we’ll lead you through the glance steps of performance optimization techniques tailored specifically to the task of seamlessly transferring data from a Postgres database to the cloud-based storage solution, Amazon S3.

Data Serialization:

Data serialization is the process of converting objects into a byte stream and vice versa. This is an optimization technique that allows data to be efficiently passed between different nodes in a cluster and stored in a variety of formats.  Spark uses different methods of serialization, we’ll focus on Kryo serialization:

Kryo Serialization:

Kryo serialization is a technique used by Spark to serialize objects more quickly. It’s designed to be fast and efficient

  • Kryo is faster and more compact than Java serialization, often up to 10 times faster. It also has a smaller memory footprint than Java serialization, which is important when shuffling and caching large amounts of data.
  • Kryo is used by default in Spark, but it requires custom registration and is not natively supported to serialize to the disk.
  • You must set a configuration property i.e. org.apache.spark.serializer.KryoSerializer if you want to set the Kryo serializer as part of a Spark job.

Real-Time Example: Optimizing with Kryo Serialization

Consider a Spark application that processes large datasets from a Postgres table, transforming and storing the results in S3. By switching from Java serialization to Kryo serialization, the application can achieve better performance. Here’s how you can enable Kryo serialization in your Spark configuration:

val sparkConf = new SparkConf() sparkConf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer") sparkConf.registerKryoClasses(Array(classOf[YourClass])) val spark = SparkSession.builder().config(sparkConf).getOrCreate()

This configuration change can lead to faster processing times, especially when dealing with large datasets.


Spark caching is a feature that helps improve the performance of Spark applications. It stores intermediate results in an accessible place that can be quickly recalled.

  • cache() and persist() are two methods used in this technique
  • cache() stores it in the memory, and persist() stores it in the user-defined storage level.
  •  RDD.Cache()would always store the data in memory. RDD.Persist() allows the storage of some part of data in the memory and some part on the disk.

Real-Time Example: Strategic Caching in Data Transformation

Imagine a scenario where your Spark job involves multiple transformations on data retrieved from a Postgres table before storing it in S3. If certain transformations are reused, caching intermediate datasets can significantly reduce the total execution time. Here’s an example of using caching effectively:

val dataset =, "tableName", connectionProperties) val intermediateDataset = dataset.filter(...).cache()

This intermediate dataset can then be used in multiple actions without the need to recompute it from the original source, thus optimizing the overall performance.

Data Structure Tuning:

Data structure tuning in Apache Spark is a crucial optimization technique that significantly impacts the performance, efficiency, and scalability of Spark applications. The choice of data structure—RDDs (Resilient Distributed Datasets), DataFrames, and Datasets—plays a vital role in how Spark manages and processes data. Each data structure has its own set of features and optimizations that can either leverage Spark’s capabilities to the fullest or become a bottleneck if not used appropriately. Understanding the characteristics and optimal use cases for each data structure allows developers to design Spark applications that are both fast and resource-efficient.

RDDs (Resilient Distributed Datasets) are the fundamental data structure of Spark, providing low-level functionality and fine-grained control over data. They are immutable, distributed collections of objects that enable fault-tolerant, parallel data processing. RDDs are best suited for scenarios where you need full control over data processing and optimization, such as custom partitioning schemes or when performing operations that are not easily expressible with DataFrames or Datasets API.

DataFrames are a distributed collection of data organized into named columns, conceptually similar to tables in a relational database. Built on top of RDDs, DataFrames provide a higher-level API that leverages Spark’s Catalyst optimizer for improved performance and efficiency. DataFrames are ideal for handling structured and semi-structured data, allowing for efficient storage and processing of large datasets. They are particularly well-suited for SQL queries, data aggregation, and columnar operations.

Datasets are a type-safe version of DataFrames that provide the benefits of RDDs (type safety and object-oriented programming) with the optimization advantages of DataFrames. Datasets are particularly useful when you require type safety for compile-time checks or prefer working with strongly typed data structures in Scala or Java. They are also beneficial for custom transformations that are not easily expressed with DataFrame operations.

Practical Example: Data Structure Optimization

Imagine a Spark application that processes large volumes of structured log data stored in a distributed file system. The goal is to perform data cleansing, transformation, and aggregation to generate summary reports.

Initial Approach using RDDs:

val logsRDD = sc.textFile("path/to/logs") val cleanedLogsRDD = logsRDD.filter(_.contains("INFO")).map(transformLogEntry) // Perform aggregation val summaryRDD = cleanedLogsRDD.mapToPair(...).reduceByKey(...)

Optimized Approach using DataFrames:

val logsDF ="path/to/logs") val cleanedLogsDF = logsDF.filter($"level" === "INFO").selectExpr("transformLogEntry(columns)") // Perform aggregation using DataFrame API for better optimization val summaryDF = cleanedLogsDF.groupBy("date").agg(count("message"))

In this example, switching from RDDs to DataFrames allows the application to leverage Spark’s Catalyst optimizer for better performance during filtering, transformation, and aggregation operations. The declarative nature of the DataFrame API not only makes the code more concise but also enables Spark to optimize the execution plan more effectively.

Garbage collection tuning:

Garbage collection is an essential component of any Java-based application, and this is especially true for Apache Spark, which heavily relies on the Java Virtual Machine (JVM) to execute its code. GC is the process of reclaiming memory used by objects that are no longer needed by the application, thus preventing memory leaks and ensuring the efficient use of memory resources. However, aggressive or poorly optimized garbage collection can lead to increased pause times, affecting the overall throughput and latency of Spark applications.

In Java Virtual Machine (JVM)-based systems like Spark, garbage collection can sometimes become a bottleneck. This is particularly true for Spark applications that create and destroy a large number of objects rapidly, such as those involving iterative algorithms, large shuffles, or extensive data transformations. Excessive garbage collection can lead to:

  • Increased Pause Times: GC events can cause the application to pause, waiting for memory to be freed up. These pauses can degrade the performance of Spark applications, especially if they occur frequently or last for long durations.
  • Reduced Throughput: Frequent garbage collection can significantly reduce the throughput of Spark applications, as more CPU cycles are spent collecting garbage rather than executing application logic.
  • Jitter and Latency Variability: Inconsistent GC behaviour can introduce variability in application latency, making performance unpredictable and complicating the tuning of Spark applications.

Tuning Garbage Collection

Consider a Spark application experiencing high latency due to frequent garbage collection pauses. To address this, you might start by switching to the G1 garbage collector and adjusting the heap size based on the application’s memory usage patterns.

val sparkConf = new SparkConf() sparkConf.set("spark.driver.extraJavaOptions", "-XX:+UseG1GC -Xms4g -Xmx4g") sparkConf.set("spark.executor.extraJavaOptions", "-XX:+UseG1GC -Xms4g -Xmx4g") val spark = SparkSession.builder().config(sparkConf).appName("GCTunedApp").getOrCreate()

In this configuration:

  • -XX:+UseG1GC enables the G1 Garbage Collector, which is designed to offer a good balance between throughput and pause times, especially suitable for applications with large heaps.
  • -Xms4g and -Xmx4g set the initial and maximum heap size to 4GB, respectively, providing sufficient memory for the application’s needs while limiting the heap size to avoid excessive GC pauses.

Memory Management:

Apache Spark divides its memory between two main categories: execution memory and storage memory. This division is crucial for balancing the memory usage between computational needs and data storage within an application.

  • Execution Memory: Utilized for computational processes such as joins, shuffles, aggregations, and sorting. This region is dynamic; it can borrow space from storage memory when the demand for execution memory exceeds its initial allocation, provided the storage memory is not being used at its full capacity.
  • Storage Memory: Allocated for caching and persisting RDDs, DataFrames, and Datasets. Unlike execution memory, storage memory cannot borrow space from execution memory. This ensures that cached data remains in memory as long as possible, improving the performance of Spark applications by reducing the need to recompute or fetch data from disk.

Within the total memory allocated to Spark tasks, there’s a shared region where execution and storage memory can coexist. However, there’s a protected sub-region within the storage memory (often referred to as R), designed to safeguard a portion of the cached data from being evicted by execution tasks.

Consider a Spark application that performs complex data transformations requiring extensive shuffling and sorting, alongside caching significant datasets for iterative processing. To optimize the performance of this application, you might adjust both the spark.memory.fraction and spark.executor.memory settings to ensure sufficient memory is allocated to both execution and storage tasks.

val sparkConf = new SparkConf()
// Increase total executor memory to 4GB
sparkConf.set("spark.executor.memory", "4g")
// Increase the memory fraction to 75% of the executor memory
sparkConf.set("spark.memory.fraction", "0.75")
// Apply the configuration to SparkSession
val spark = SparkSession.builder().config(sparkConf).appName("MemoryOptimizedApp").getOrCreate()

In this configuration, increasing the executor memory to 4GB and the memory fraction to 75% ensures that more memory is available for both execution and storage, reducing the need for spilling data to disk and improving overall application performance.


Effective memory management is a cornerstone of optimizing Apache Spark applications for better performance. By carefully configuring memory allocation and leveraging dynamic allocation, you can significantly enhance the efficiency of data transformation processes, such as moving data from a Postgres table to Amazon S3. These strategies ensure that your Spark applications utilize available memory resources optimally, minimizing disk I/O and accelerating processing times for large-scale data analytics tasks.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>