Skills · Data & AI

Apache Spark Optimization

Unverified30/40

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.

Originally by wshobson · MIT

Claude CodePartialHas SKILL.md but declares no allowed-tools — Claude Code will ask for permission each time
CursorPartialPlain prose you can paste in — but no Cursor rules file
CodexPartialPlain prose you can paste in — but no AGENTS.md
Gemini CLIPartialPlain prose you can paste in
CopilotPartialPlain prose you can paste in — but no Copilot instructions file
npx agentalley add spark-optimization

This command does not work yet — the CLI is still being built. Until then, use Raw in the reader below to take the file.

Who is stuck, and on what

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 whole source

No sign-in, no blur, nothing truncated
spark-optimization/SKILL.md96 lines3.1 KBRawView on GitHub
Frontmatter — 2 properties
namespark-optimization
descriptionOptimize Apache Spark jobs with partitioning, caching, shuffle optimization, and memory tuning. Use when improving Spark performance, debugging slow jobs, or scaling data processing pipelines.
1---
2name: spark-optimization
3description: 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---A5No allowed-tools declared — no way to tell what this skill may touch
5 
6# Apache Spark Optimization
7 
8Production 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```
24Driver Program
25
26Job (triggered by action)
27
28Stages (separated by shuffles)
29
30Tasks (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```python
46from pyspark.sql import SparkSession
47from pyspark.sql import functions as F
48 
49# Create optimized Spark session
50spark = (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
60df = (spark.read
61 .format("parquet")
62 .option("mergeSchema", "false")
63 .load("s3://bucket/data/"))
64 
65# Efficient transformations
66result = (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 
72result.write.mode("overwrite").parquet("s3://bucket/output/")
73```
74 
75## Detailed patterns and worked examples
76 
77Detailed 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 

Reviews

Installed this one?Write the first review and take the Trailblazer badge.

Reviews only open after a real install, so this is empty — and we leave it empty rather than invent one.

Alternatives

Also in Data & AI