Generated banner for Spark Optimization
Generated logo for Spark Optimization

Spark Optimization

Seth HobsonCommunityCreated by Seth Hobson

  • Skill
  • Tool integration
  • Data and AI
  • MIT
  • No issues found

From the author

Optimize Apache Spark jobs with partitioning, caching, shuffle optimization, and memory tuning. Use when improving Spark performance, debugging slow jobs, or scaling data processing pipelines.
— the description by Seth Hobson

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

This skill provides a comprehensive guide to optimizing Apache Spark applications. It covers essential performance factors including shuffle management, data skew mitigation, memory tuning, and effective partitioning strategies. The content includes both high-level architectural best practices and specific code patterns for common Spark challenges like join optimization, caching, and serialization.

What it does

  • Provides configuration patterns for Adaptive Query Execution (AQE).
  • Explains techniques for mitigating data skew through salting and broadcast joins.
  • Offers guidance on right-sizing partitions to balance parallelism and overhead.
  • Details strategies for effective memory management and persistence.
  • Outlines best practices for using columnar file formats like Parquet.
  • Demonstrates how to optimize joins using bucketed tables and broadcast hints.

When to use it

  • When debugging slow or failing Spark jobs.
  • When scaling data pipelines to handle larger datasets.
  • When trying to reduce network and disk I/O caused by excessive shuffles.
  • When optimizing resource usage and reducing executor memory pressure.
  • When preparing Spark jobs for production deployment.

Advantages

  • Reduces job execution time by minimizing expensive shuffle operations.
  • Improves cluster resource utilization through better memory and partition management.
  • Provides actionable patterns for handling common performance bottlenecks like data skew.
  • Encourages the use of modern Spark features like AQE for automatic optimization.

Usage examples

“How can I optimize a join between a large table and a small lookup table in Spark?”
The skill provides a pattern using the broadcast join hint to avoid a full shuffle, significantly speeding up the join operation.
“My Spark job is failing due to data skew on a specific key. What should I do?”
The skill suggests enabling Adaptive Query Execution (AQE) skew join optimization or implementing manual salting to distribute the skewed keys evenly.
“What is the recommended way to manage partitions for a large dataset?”
The skill explains how to calculate optimal partition counts based on data size and how to use repartitioning or coalescing to maintain efficiency.

Install

  1. Download the skill as a ZIP:

    https://mp-staging.autogineer.com/agent-skills/wshobson/spark-optimization/download
  2. Unzip it into the cross-client skills folder of your project:

    mkdir -p .agents/skills && unzip spark-optimization.zip -d .agents/skills

Documentation by Seth Hobson

Apache Spark Optimization

Production patterns for optimizing Apache Spark jobs including partitioning strategies, memory management, shuffle optimization, and performance tuning.

When to Use This Skill

  • Optimizing slow Spark jobs

  • Tuning memory and executor configuration

  • Implementing efficient partitioning strategies

  • Debugging Spark performance issues

  • Scaling Spark pipelines for large datasets

  • Reducing shuffle and data skew

Core Concepts

1. Spark Execution Model
Driver Program
    ↓
Job (triggered by action)
    ↓
Stages (separated by shuffles)
    ↓
Tasks (one per partition)
2. Key Performance Factors
FactorImpactSolution
ShuffleNetwork I/O, disk I/OMinimize wide transformations
Data SkewUneven task durationSalting, broadcast joins
SerializationCPU overheadUse Kryo, columnar formats
MemoryGC pressure, spillsTune executor memory
PartitionsParallelismRight-size partitions

Quick Start

from pyspark.sql import SparkSession
from pyspark.sql import functions as F

# Create optimized Spark session
spark = (SparkSession.builder
    .appName("OptimizedJob")
    .config("spark.sql.adaptive.enabled", "true")
    .config("spark.sql.adaptive.coalescePartitions.enabled", "true")
    .config("spark.sql.adaptive.skewJoin.enabled", "true")
    .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
    .config("spark.sql.shuffle.partitions", "200")
    .getOrCreate())

# Read with optimized settings
df = (spark.read
    .format("parquet")
    .option("mergeSchema", "false")
    .load("s3://bucket/data/"))

# Efficient transformations
result = (df
    .filter(F.col("date") >= "2024-01-01")
    .select("id", "amount", "category")
    .groupBy("category")
    .agg(F.sum("amount").alias("total")))

result.write.mode("overwrite").parquet("s3://bucket/output/")

Detailed patterns and worked examples

Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.

Best Practices

Do's
  • Enable AQE - Adaptive query execution handles many issues

  • Use Parquet/Delta - Columnar formats with compression

  • Broadcast small tables - Avoid shuffle for small joins

  • Monitor Spark UI - Check for skew, spills, GC

  • Right-size partitions - 128MB - 256MB per partition

Don'ts
  • Don't collect large data - Keep data distributed

  • Don't use UDFs unnecessarily - Use built-in functions

  • Don't over-cache - Memory is limited

  • Don't ignore data skew - It dominates job time

  • Don't use .count() for existence - Use .take(1) or .isEmpty()

Files

Requirements and compatibility

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Needs: Needs Python

Security

No issues found by our static scan· No issues found

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Created by
Seth Hobson
Publisher
Seth Hobson (Community)
Source
wshobson/agents · plugins/data-engineering/skills/spark-optimization · commit 4bde316
Licence
MIT License (redistribution allowed)
  • LICENSE (added by AutoGineer from LICENSE)
  • ATTRIBUTION.md (written by AutoGineer)
Synced

AutoGineer is not affiliated with or endorsed by Seth Hobson.

Download ZIP

The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download spark-optimization.zip