Optimize Apache Spark jobs with partitioning, caching
Apache Spark Optimization

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.
--- name: spark-optimization description: 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. --- # 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
Overview
Production patterns for optimizing Apache Spark jobs including partitioning strategies, memory management, shuffle optimization, and performance tuning.
Manual install (npx, ZIP)
Claude Code
npx skills add wshobson/agents --skill spark-optimization -g -a claude-codeClaude Code (manual)
mkdir -p ~/.claude/skills && curl -sL https://codeload.github.com/wshobson/agents/tar.gz/4236bb91f839 \
| tar -xz -C ~/.claude/skills --strip-components=4 agents-4236bb91f839/plugins/data-engineering/skills/spark-optimizationWindows (PowerShell)
New-Item -ItemType Directory -Force "$HOME\.claude\skills" | Out-Null; Invoke-WebRequest "https://m.agentalley.io/zip/0ade870cf31857798392/spark-optimization.zip" -OutFile "$env:TEMP\spark-optimization.zip"; Expand-Archive "$env:TEMP\spark-optimization.zip" -DestinationPath "$HOME\.claude\skills" -ForceClaude.ai / desktop
Codex, Cursor, other agents
Connects with Agent Alley MCP · agentalley.io/mcp
Files included
SKILL.md3.1 KBreferences/1 file · 9.6 KB
Read the files here
SKILL.md
Show the full text96 lines
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
| Factor | Impact | Solution |
|---|---|---|
| Shuffle | Network I/O, disk I/O | Minimize wide transformations |
| Data Skew | Uneven task duration | Salting, broadcast joins |
| Serialization | CPU overhead | Use Kryo, columnar formats |
| Memory | GC pressure, spills | Tune executor memory |
| Partitions | Parallelism | Right-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()
| 1 | |
| 2 | name spark-optimization |
| 3 | description 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. |
| 4 | |
| 5 | |
| 6 | # Apache Spark Optimization |
| 7 | |
| 8 | Production patterns for optimizing Apache Spark jobs including partitioning strategies, memory management, shuffle optimization, and performance tuning. |
| 9 | |
| 10 | ## When to Use This Skill |
| 11 | |
| 12 | Optimizing slow Spark jobs |
| 13 | Tuning memory and executor configuration |
| 14 | Implementing efficient partitioning strategies |
| 15 | Debugging Spark performance issues |
| 16 | Scaling Spark pipelines for large datasets |
| 17 | Reducing shuffle and data skew |
| 18 | |
| 19 | ## Core Concepts |
| 20 | |
| 21 | ### 1. Spark Execution Model |
| 22 | |
| 23 | |
| 24 | Driver Program |
| 25 | ↓ |
| 26 | Job (triggered by action) |
| 27 | ↓ |
| 28 | Stages (separated by shuffles) |
| 29 | ↓ |
| 30 | Tasks (one per partition) |
| 31 | |
| 32 | |
| 33 | ### 2. Key Performance Factors |
| 34 | |
| 35 | | Factor | Impact | Solution | |
| 36 | | ----------------- | --------------------- | ----------------------------- | |
| 37 | | **Shuffle** | Network I/O, disk I/O | Minimize wide transformations | |
| 38 | | **Data Skew** | Uneven task duration | Salting, broadcast joins | |
| 39 | | **Serialization** | CPU overhead | Use Kryo, columnar formats | |
| 40 | | **Memory** | GC pressure, spills | Tune executor memory | |
| 41 | | **Partitions** | Parallelism | Right-size partitions | |
| 42 | |
| 43 | ## Quick Start |
| 44 | |
| 45 | |
| 46 | from pyspark.sql import SparkSession |
| 47 | from pyspark.sql import functions as F |
| 48 | |
| 49 | # Create optimized Spark session |
| 50 | spark = (SparkSession.builder |
| 51 | .appName("OptimizedJob") |
| 52 | .config("spark.sql.adaptive.enabled", "true") |
| 53 | .config("spark.sql.adaptive.coalescePartitions.enabled", "true") |
| 54 | .config("spark.sql.adaptive.skewJoin.enabled", "true") |
| 55 | .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer") |
| 56 | .config("spark.sql.shuffle.partitions", "200") |
| 57 | .getOrCreate()) |
| 58 | |
| 59 | # Read with optimized settings |
| 60 | df = (spark.read |
| 61 | .format("parquet") |
| 62 | .option("mergeSchema", "false") |
| 63 | .load("s3://bucket/data/")) |
| 64 | |
| 65 | # Efficient transformations |
| 66 | result = (df |
| 67 | .filter(F.col("date") >= "2024-01-01") |
| 68 | .select("id", "amount", "category") |
| 69 | .groupBy("category") |
| 70 | .agg(F.sum("amount").alias("total"))) |
| 71 | |
| 72 | result.write.mode("overwrite").parquet("s3://bucket/output/") |
| 73 | |
| 74 | |
| 75 | ## Detailed patterns and worked examples |
| 76 | |
| 77 | Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient. |
| 78 | |
| 79 | ## Best Practices |
| 80 | |
| 81 | ### Do's |
| 82 | |
| 83 | **Enable AQE** - Adaptive query execution handles many issues |
| 84 | **Use Parquet/Delta** - Columnar formats with compression |
| 85 | **Broadcast small tables** - Avoid shuffle for small joins |
| 86 | **Monitor Spark UI** - Check for skew, spills, GC |
| 87 | **Right-size partitions** - 128MB - 256MB per partition |
| 88 | |
| 89 | ### Don'ts |
| 90 | |
| 91 | **Don't collect large data** - Keep data distributed |
| 92 | **Don't use UDFs unnecessarily** - Use built-in functions |
| 93 | **Don't over-cache** - Memory is limited |
| 94 | **Don't ignore data skew** - It dominates job time |
| 95 | **Don't use `.count()` for existence** - Use `.take(1)` or `.isEmpty()` |
| 96 |
Details
- Updated
- Sep 21, 2026
- License
- MIT
- Stars on the repo
- ★ 39,857
- Checked
- Source
- wshobson/agents ↗
- Creator
- wshobson · 179 listings
Similar skills
See all →
SkillTranslates Snowflake dbt SQL models to Standardized BigQuerydbt Snowflake to BigQuery Translator
SkillAnswering business analytics, RevOps, GTM metric, pipelineDeepline analytics
SkillBuild production Apache Airflow DAGs with best practicesApache Airflow DAG Patterns
SkillImplement data quality validation with Great ExpectationsData quality frameworks
SkillMaster dbt for analytics engineering with model organizationDbt transformation patterns
SkillAudit datasets for completeness, consistency, accuracyData quality auditorMore from wshobson
SkillMaster C/.NET backend development patterns for building
SkillImplement WCAG 2.2 compliant interfaces with mobileAccessibility compliance
SkillMaster Material Design 3 and Jetpack Compose patternsAndroid Mobile Design
SkillMigrate from AngularJS to Angular using hybrid modeAngular migration
SkillUnderstand anti-reversing, obfuscationAnti reversing techniques
SkillBuild production Apache Airflow DAGs with best practicesApache Airflow DAG Patterns