Thursday, September 8, 2022

How to Decide on Spark Cluster Configuration?


Spark Optimization:

Spark Optimization is the most Important concepts. It is the responsibility of developer to design the spark application in a optimize manner so we can take full advantage of spark execution engine. When Application are not optimized, simple code takes longer to execute, resulting in performance lags and downtime, and it takes effect on the other Application which is using the same cluster. 

There are two ways to optimize Spark Application.

1). Resource/Cluster level Optimization

2). Code Level / Application Level Optimization

If developer write the optimized code but if application don’t have enough resources to run it then there is no use to write optimize code, as it will take lot of time due to lack of resources.

Spark Architecture:

Before going to deep down into spark Resource Level Optimization let’s first try to understand spark architecture, so it will be easy to understand the process of Resource Level Optimization.

Apache Spark follows a master/slave architecture with two main daemons and a cluster manager –

a).Master Daemon — (Master/Driver Process)

b).Worker Daemon –(Slave Process)

c).Cluster Manager

A spark cluster has Master Node and of Slaves/Workers Node. The driver and the executors run their individual Java processes and users can run them on the same horizontal spark cluster or on separate machines.

Master Node or driver process : The master is the driver that runs the main() program where the spark context is created. It then interacts with the cluster manager to schedule the job execution and perform the tasks.

Slaves/Workers Node: The worker consists of processes that can run in parallel to perform the tasks scheduled by the driver program. These processes are called Executors/Containers.

The role of worker nodes/executors:

1. Perform the data processing for the application code

2. Read from and write the data to the external sources

3. Store the computation results in memory, or disk.

Executors/Containers contains cores and RAM. Executors/Containers launch once in the beginning of Spark Application and then they run for the entire lifetime of an application. All the cores and total memory divides among the number of executors. One node can hold one or more then one executor. The individual task in the given Spark job runs in the Spark executors/Containers.

Executors/Containers are the combination of memory(RAM) and Cores.

Number Of Cores decides Number of Parallel Process in each executors. So let say if we have 5 cores in one executor then max 5 parallel task can execute in executor.

Tasks: Task is the smallest execution unit in Spark. A task in spark executes a series of instructions. For eg. reading data, filtering and applying map() on data can be combined into a task. Tasks are executed inside an executor. Based one HDFS block size (128 mb) spark will create partitions, and assign one task per partition. So if we have 1GB(1024 MB)data , spark will create (128mb+128mb+128mb+128mb) 4 partitions.

Stage : Group of tasks creates a Stage. Number of Stages depends on Data Shuffling(Narrow and wide Transformation). Spark encounters a function that requires a shuffle it creates a new stage. Transformation functions like reduceByKey(), Join() etc will trigger a shuffle and will result in a new stage

Job : Group of Stages Creates a Job.

How to Decide Cluster Configuration

Let’s Assume we have 10 node cluster.

16 CPU Cores Each Node

64 GB RAM Each Node

Now we can decide the no of Executers based on no of CPU cores. Each Executors can hold Minimum 1 Core and Max is Number of CPU cores available.

If we consider the minimum value. i.e. one core per executor.

In that case we will have 16 Executor and 1 core per executor, and each executor will have 64Gb/16=4GB RAM.

so as we discussed earlier, number of cores decided the parallel task, so in this case we can not execute more then one task at a time.

If we create any broadcast or accumulator variable then it will create copy on each executor.

so it is not good idea to assign one core per executor. This Process is also called tiny executor.

Let’s Discuss 2nd approach.

where we can create 1 single executor and assign all the 16 cores to one executor. so we will have one executor,64 core and 64 gb RAM.

Again In This case we will have 16 parallel process

It is observed and that if we execute more then 5 parallel process or have more then 5 cores per executor then the HDFS throughput suffers.

If executor holds very huge amount of memory (64gb ) then garbage collection takes lot of time.

So this is also not a good cluster configuration. This Process is also called as FAT executor.

So above tiny and fat executor is not a good cluster Configuration.

So we should always have a balanced approach , so can effectively use our cluster.

Let’s try to configure optimized Cluster.

Resources

1. Total Number Of Nodes : 10

2. Cores Per Node : 16

3. RAM Per Node : 64 GB

So, from the above Resources we need to give 1 core and 1 gb ram for other OS Activities.

So , Now we left with -

1. Total Number Of Nodes : 102. Cores Per Node : 153. RAM Per Node : 63 GB

As per the study ,5 cores per Executors is the best and preferred Choice.

15 cores → 63 GB RAM → each machineSo here we can have 15/5=3 executors per machine.Memory : 63/3=21 GB Per Executor

1 Node — → 3 Executors →5 Cores per Executors → 21 GB RAM Per Executor.

Out Of this 21GB RAM , some of Will go as part of overhead memory(off heap).

overhead memory= max(384 MB,7% Of Executor Memory)

i.e max(384 MB, 7% Of 21 GB)~1.5 GB

So the Remaining Memory Will be 21–1.5 ~ 19 GB

SO the Calculation Will be -

10 Node Cluster

Each node has 3 Executor: 10*3= 30 Executor

So 30 Executor With Each Executor will Hold. : 5 CPU Core and 19 GB Memory.

Now Out of These 30 Executors 1 executor will we given for Yarn Applications Manager.

So Now Our Final Cluster Configuration Will Look like below:

Number Of Nodes : 10Total Number Of Executors : 29Core Per Executor : 5RAM Per Executor :19 GB





Sunday, September 4, 2022

Data Engineering Blogs

How to get Latest news of Big data/Data Engineering?


Often data enthusiasts are asking that where can I get latest news on technology, components, architecture and trending design stuffs which related to big data/Data engineering?

Here I am going to post few important websites which I am referring frequently for the same.


8.    LinkedIn Blog




Thursday, September 1, 2022

NoSQL Databases Series

 

NoSQL Databases(Store):

NoSQL databases differ significantly from SQL databases. For example, they all use a data model with a different structure from relational database management systems' typical row-and-column table architecture (RDBMSs). There are 4 types of NoSQL databases. In this post, we are going to see each type with advantage ,disadvantages, use cases and example.

1. Key-Value Store

The key-value store DB system depends on the map-storage data. It helps to speed up query execution as the data is less/not intensive. It does query execution based on primary key values. The best examples are Key/ID columns.
They are highly scalable and able to scale their nodes when no of concurrent users and traffic grows. It prefers distributed deployment. At peak periods, amazon website uses 10000 different DynamoDB servers to service close to 10 million consumers across several locations.

Advantages:
1. The major advantage is its High Availability, High Scalable, and reliable quality.

2.No exact query language is required. So you can move the DB to any Language.

3. It does not require placeholders for optional data, it will have lower storage requirements and grow virtually linearly with the number of nodes.

Disadvantages:
1. Querying a specific value is not possible, you must rely on the person who saved it, trusting that all needed fields are present.

2. Complex queries, such as indexed searches, significantly impact performance.

3. Not optimized for lookup. Lookup requires scanning the whole collection or creating separate index values.

Example:
Redis, level Db, Riak, DynamoDB

Use cases: 
For storing user session data, maintaining schema-less user profiles, Storing user preferences, Storing shopping cart data.

2. Document store:


It stores data as independent documents with self-contained schemas. The sorting key should be a key that is present in all documents, such as item name or price, to sort the data in this context. Otherwise, if the key isn't included in all papers, the results may be unexpected. In MongoDB, for example, if a value is not represented, it is treated as null and returned at the end of the order. Filtering the results is also possible with the document store database systems.

Advantages:

1. It can store any data structure.
2. Ability to perform complicated queries with sorting and filtering in single operations.
3. As it works as schema-less DB, run faster.

Disadvantage:
1. When the document grows, there will be a security concern
2. Need attention to web app vulnerabilities, which may affect read-write performance.

Example:

MongoDB, CouchDB, RavenDB, CouchBase

Use cases:
E-commerce platforms, Content management systems, Analytics platforms, and Blogging platforms.

3. Column- Family Store database:

The column-family databases are designed to keep a huge volume of data comprising little data pieces. The column-family database helps to access a huge amount of data at once since the data is stored under several row keys that may be utilized to collect data with low latency. Due to this, providing real-time data would not be a difficulty.
The column family database can also help for fast writes because the data model makes data storage a less intensive activity. Google had a similar issue because they had petabytes of data spread across multiple domains and wanted to query it quickly and efficiently.

Advantages:
1. The column-based is a proven and highly effective storage option for event-driven data. For example, a separate column for publishing events or error logs might be created for a given application, with rows identified by unique keys, including event timestamps.
2. Column family database can handle small data elements for large data sets. So We can store and retrieve relevant data together in our selected domain.

Disadvantages:
When you access the data in different rows, performance will be affected.

Use cases:
Content management systems, Blogging platforms, Systems that maintain counters, Services that have expiring usage, and Systems that require heavy write requests (like log aggregators)

Examples:·        
Cassandra, HBase, Hyper table, Accumulo, Google’s BigTable.

4. Graph Database

This database type allows storing data nodes that are participants in the relationships to be constructed to create the connections. Graph databases allow you to define relationships with directional importance, allowing you to store meaningful connections. Furthermore, graph databases will enable you to query the database for different patterns based on your needs. Finally, using various traversal methods, graph databases provide speed and dependability.

Advantages:
The ability to store data in a linked format while retaining the links between distinct entities is one of the main benefits. Relationships can be depicted in a variety of ways. Scalable, high-availability installations are also possible with graph databases.

Disadvantage:
Data Sharding is very difficult in this graph database. Because there is no generalization in the query language for graph databases, sharding data is exceedingly challenging and usually necessitates learning a new query language like CIPHER.

Use cases:
·        Fraud detection Graph-based search, Network and IT operations, Social networks, etc
Neo4j,OrientDB,Titan,InfiniteGraph,ApacheGraph





Spark- Window Function

  Window functions in Spark ================================================ -> Spark Window functions operate on a group of rows like pa...