Lädt...


🔧 Why Is Spark Slow??


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Why Is Spark Slow??

Starting with an eye-catching title, "Why is Spark slow??," it's important to note that calling Spark "slow" can mean various things. Is it slow at aggregations? Data loading? Different cases exist. Also, "Spark" is a broad term, and its performance depends on factors like the programming language and usage context. So, let's refine the title to be more precise before diving in.

Since I primarily use Spark with Python on Databricks, I'll narrow the scope further.

The refined title will be:

"First Impressions of Spark: 'I Heard It Was Fast, But Why Does It Feel Slow?' A Beginner's Perspective"

Motivation for Writing (Casual Thoughts)

As someone who works extensively with pandas, NumPy, and machine learning libraries, I admired the allure of Spark's ability to handle big data with parallel and distributed processing. When I finally got to use Spark for work, I was puzzled by scenarios where it seemed slower than pandas. Unsure of what was wrong, I discovered several insights and would like to share them.

When Does Your Spark Become Slow?

Before Getting to the Main Topic

Let's briefly cover Spark's basic architecture.

Image description

(Cluster Mode Overview)

A Spark cluster consists of Worker Nodes, which perform the actual processing, and a Driver Node, which coordinates and plans the execution. This architecture influences everything discussed below, so keep it in mind.

Now, onto the main points.

1. The Dataset Isn’t Large Enough

Spark is optimized for large-scale data processing, though it can handle small datasets as well. However, take a look at this benchmark:

Image description

(Benchmarking Apache Spark on a Single Node Machine)

The results show that for datasets under 15GB, pandas outperforms Spark in aggregation tasks. Why? In a nutshell, the overhead of Spark's optimizations outweighs the benefits for small datasets.

The link shows cases where Spark isn't slower, but these are often in a local cluster mode. For standalone setups, smaller datasets can be a disadvantage due to network communication overhead between nodes.

  • pandas: Processes everything in-memory on a single machine, with no network or storage I/O.
  • Spark: Uses RDDs (Resilient Distributed Datasets), involves network communication between Workers (if distributed), and incurs overhead in organizing data for parallel processing.

2. Understanding Lazy Evaluation

Spark employs lazy evaluation, meaning transformations are not executed immediately but deferred until an action (e.g., collect, count, show) triggers computation.

Example (pandas):

df = spark.read.table("tpch.lineitem").limit(1000).toPandas()
df["l_tax_percentage"] = df["l_tax"] * 100
for l_orderkey, group_df in df.groupby("l_orderkey"):
    print(l_orderkey, group_df["l_tax_percentage"].mean())

Execution time: 3.04 seconds

Equivalent in Spark:

from pyspark.sql import functions as F
sdf = spark.read.table("tpch.lineitem").limit(1000)
sdf = sdf.withColumn("l_tax_percentage", F.col("l_tax") * 100)

for row in sdf.select("l_orderkey").distinct().collect():
    grouped_sdf = sdf.filter(F.col("l_orderkey") == row.l_orderkey).groupBy("l_orderkey").agg(
        F.mean("l_tax_percentage").alias("avg_l_tax_percentage")
    )
    print(grouped_sdf.show())

Execution time: Still running after 3 minutes.

Why?

  1. Lazy Evaluation: All transformations are queued and only executed during an action like show.
  2. Driver-to-Worker Communication: Operations like collect and show involve data transfer from Workers to the Driver, causing delays.

The Spark code effectively does this in pandas:

for l_orderkey, group_df in df.groupby("l_orderkey"):
    df["l_tax_percentage"] = df["l_tax"] * 100
    print(l_orderkey, group_df["l_tax_percentage"].mean())

Avoid such patterns by using Spark's cache or restructuring the logic to minimize repeated calculations.

3. Watch Out for Shuffles

https://spark.apache.org/docs/latest/rdd-programming-guide.html#shuffle-operations

Shuffles occur when data is redistributed across Workers, typically during operations like groupByKey, join, or repartition. Shuffles can be slow due to:

  • Network Communication between nodes.
  • Global Sorting and Aggregation of data across partitions.

For example, having more Workers doesn't always improve performance during a shuffle.

  • 32GB x 8 Workers can be slower than 64GB x 4 Workers, as fewer Workers reduce inter-node communication.

Conclusion

Did you find this helpful? Spark is an excellent tool when used effectively. Beyond speeding up large-scale data processing, Spark shines with its scalable resource management, especially in the cloud.

Try Spark to optimize your data operations and management!

...

🔧 Why Is Spark Slow??


📈 27.91 Punkte
🔧 Programmierung

🔧 Is Spark Still Relevant: Spark vs Dask vs RAPIDS


📈 25.7 Punkte
🔧 Programmierung

🪟 Cisco präsentiert Spark 2.0 und Spark Whiteboard


📈 25.7 Punkte
🪟 Windows Tipps

🪟 Cisco präsentiert Spark 2.0 und Spark Whiteboard


📈 25.7 Punkte
🪟 Windows Tipps

📰 Slow iPhone: Check How Slow Your Device Is


📈 20.96 Punkte
🖥️ Betriebssysteme

🔧 Why Apache Spark RDD is immutable?


📈 17.43 Punkte
🔧 Programmierung

🍏 Why is macOS Mojave slow and how to fix It


📈 15.05 Punkte
🍏 iOS / Mac OS

🔧 Why Large Tables Slow Down Your Database


📈 15.05 Punkte
🔧 Programmierung

🐧 OnlyOffice why is it so slow? ( OnlyOffice, not OpenOffice)


📈 15.05 Punkte
🐧 Linux Tipps

🔧 Why MySQL Could Be Slow With Large Tables


📈 15.05 Punkte
🔧 Programmierung

🐧 Why is OpenOffice so slow


📈 15.05 Punkte
🐧 Linux Tipps

🔧 Why is it Slow? Strategies for Solving Performance Issues


📈 15.05 Punkte
🔧 Programmierung

🔧 Why Your Flutter App Feels Slow and How to Fix It?


📈 15.05 Punkte
🔧 Programmierung

🐧 Why is Apache Traffic Server so slow?!


📈 15.05 Punkte
🐧 Linux Tipps

🔧 Why are you writing slow code?


📈 15.05 Punkte
🔧 Programmierung

🐧 Why are KDE apps so slow to start ?


📈 15.05 Punkte
🐧 Linux Tipps

🎥 Why SLOW WiFi ISN'T Going Away?


📈 15.05 Punkte
🎥 Videos

🔧 Why is your website slow? Common causes and how to fix them


📈 15.05 Punkte
🔧 Programmierung

🔧 Why Bitbucket Pipelines is Slow and How to Optimize


📈 15.05 Punkte
🔧 Programmierung

🎥 Why are upload speeds so SLOW?


📈 15.05 Punkte
🎥 Videos

🔧 Why Distributed Transactions can be slow?


📈 15.05 Punkte
🔧 Programmierung

📰 Microsoft Explains Why Signed PowerShell Cmdlets May Run Slow


📈 15.05 Punkte
📰 IT Security Nachrichten

📰 Why your phone is slow – and how to speed it up


📈 15.05 Punkte
📰 IT Security Nachrichten

🐧 Why is librewolf so slow


📈 15.05 Punkte
🐧 Linux Tipps

📰 Why is my VPN connection so slow? And what to do?


📈 15.05 Punkte
🖥️ Betriebssysteme

📰 Why is my Wi‑Fi slow and how do I make it faster?


📈 15.05 Punkte
📰 IT Security Nachrichten

🐧 Why is Ubuntu on Virtual Box slow and laggy?


📈 15.05 Punkte
🐧 Linux Tipps

📰 Why is my internet so slow? 11 ways to speed up your connection


📈 15.05 Punkte
📰 IT Nachrichten

🪟 Chrome extensions might be the reason why your browser is slow


📈 15.05 Punkte
🪟 Windows Tipps

📰 Why Do Some Torrents Download Slow?


📈 15.05 Punkte
🖥️ Betriebssysteme

📰 Why organizations are keen on zero trust but are slow to adopt it


📈 15.05 Punkte
📰 IT Security Nachrichten

🎥 Here's why Chrome is SLOW (It's NOT your RAM)


📈 15.05 Punkte
🎥 Video | Youtube

matomo