Be it a small startup or a large corporation, data is everywhere. This data is collected from a variety of sources, such as customer logs, office bills, cost sheets, and employee databases. Companies collect and analyze these data chunks to determine patterns and trends. These patterns help them in making important decisions for the enhancement of the business.
But, this data analysis and number crunching are not possible only through excel sheets. This is where data processing software technologies come in. One of the fastest and widely used data processing frameworks is Apache Spark. Spark optimization techniques are used for tuning its performance to make the most out of it.
We will learn about the techniques in a bit. Let us wrap our heads around the basics of this software framework.
Table of Contents
What is Apache Spark?
Apache Spark is a world-famous open-source cluster computing framework that is used for processing huge data sets in companies. Processing these huge data sets and distributing these among multiple systems is easy with Apache Spark. It offers simple APIs that make the lives of programmers and developers easy.
Spark provides native bindings for programming languages, such as Python, R, Scala, and Java. It supports machine learning, graph processing, and SQL databases. Due to these amazing benefits, Spark is used in banks, tech firms, financial organizations, telecommunication departments, and government agencies. To learn more about apache spark, check out our data science courses from recognized universities.
Architecture of Apache Spark
The run-time architecture of Apache Spark consists of the following components:
Spark driver or master process
This converts programs into tasks and then schedules them for executors (slave processes). The task scheduler distributes these tasks to executors.
The Spark cluster manager is responsible for launching executors and drivers. It schedules and allocates resources across several host machines for a cluster.
Executors, also called slave processes, are entities where tasks of a job are executed. After they are launched, they run until the lifecycle of the Spark application ends. The execution of a Spark job does not stop if an executor fails.
Resilient Distributed Datasets (RDD)
This is a collection of datasets that are immutable and are distributed over the nodes of a Spark cluster. Notably, a cluster is a collection of distributed systems where Spark can be installed. RDDs are divided into multiple partitions. And, they are called resilient as they can fix the data issues in case of data failure.
The types of RDDs supported by Spark are:
- Hadoop datasets built from files on Hadoop Distributed File System
- Parallelized collections, which can be based on Scala collections
DAG (Directed Acyclic Graph)
Spark creates a graph as soon as a code is entered into the Spark console. If some action (an instruction for executing an operation) is triggered, this graph is submitted to the DAGScheduler.
This graph can be considered as a sequence of data actions. DAG consists of vertices and edges. Vertices represent an RDD and edges represent computations to be performed on that specific RDD. It is called a directed graph as there are no loops or cycles within the graph.
Spark Optimization Techniques
Spark optimization techniques are used to modify the settings and properties of Spark to ensure that the resources are utilized properly and the jobs are executed quickly. All this ultimately helps in processing data efficiently.
The most popular Spark optimization techniques are listed below:
1. Data Serialization
Here, an in-memory object is converted into another format that can be stored in a file or sent over a network. This improves the performance of distributed applications. The two ways to serialize data are:
- Java serialization – The ObjectOutputStream framework is used for serializing objects. The java.io.Externalizable can be used to control the performance of the serialization. This process offers lightweight persistence.
- Kyro serialization – Spark uses the Kryo Serialization library (v4) for serializing objects that are faster than Java serialization and is a more compact process. To improve the performance, the classes have to be registered using the registerKryoClasses method.
This is an efficient technique that is used when the data is required more often. Cache() and persist() are the methods used in this technique. These methods are used for storing the computations of an RDD, DataSet, and DataFrame. But, cache() stores it in the memory, and persist() stores it in the user-defined storage level.
These methods can help in reducing costs and saving time as repeated computations are used.
3. Data Structure Tuning
We can reduce the memory consumption while using Spark, by tweaking certain Java features that might add overhead. This is possible in the following ways:
- Use enumerated objects or numeric IDs in place of strings for keys.
- Avoid using a lot of objects and complicated nested structures.
- Set the JVM flag to xx:+UseCompressedOops if the memory size is less than 32 GB.
4. Garbage collection optimization
For optimizing garbage collectors, G1 and GC must be used for running Spark applications. The G1 collector manages growing heaps. GC tuning is essential according to the generated logs, to control the unexpected behavior of applications. But before this, you need to modify and optimize the program’s logic and code.
G1GC helps to decrease the execution time of the jobs by optimizing the pause times between the processes.
5. Memory Management
The memory used for storing computations, such as joins, shuffles, sorting, and aggregations, is called execution memory. The storage memory is used for caching and handling data stored in clusters. Both memories use a unified region M.
When the execution memory is not in use, the storage memory can use the space. Similarly, when storage memory is idle, execution memory can utilize the space. This is one of the most efficient Spark optimization techniques.
Also Read: 6 Game Changing Features of Apache Spark
From the various Spark optimization techniques, we can understand how they help in cutting down processing time and process data faster. Developers and professionals apply these techniques according to the applications and the amount of data in question.
If you are curious to learn about spark optimization, data science, check out IIIT-B & upGrad’s Executive PG Programme in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.