Apache Spark Optimization
Unverified●30/40Claude Code◐PartialHas SKILL.md but declares no allowed-tools — Claude Code will ask for permission each time
Cursor◐PartialPlain prose you can paste in — but no Cursor rules file
Codex◐PartialPlain prose you can paste in — but no AGENTS.md
Gemini CLI◐PartialPlain prose you can paste in
Copilot◐PartialPlain prose you can paste in — but no Copilot instructions file
npx agentalley add spark-optimizationWho 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
Frontmatter — 2 properties
| 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. |
| 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 | ---A5 — No allowed-tools declared — no way to tell what this skill may touch |
| 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 | ```python |
| 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 |
Reviews
Installed this one?Write the first review and take the Trailblazer badge.
Alternatives
Task Coordination StrategiesDecompose complex tasks, design dependency graphs, and coordinate multi-agent work with proper task descriptions and workload balancing. Use this skill when breaking down work for agent teams, managing task dependencies, or monitoring team progress.◐◐◐◐◐●35/40Ebay Seller Tools·····●34/40Tough Decision Advisor: Every Angle ConsideredHand in a decision you're stuck on. Get back a clear breakdown of every angle — the trade-offs, the risks, the blind spot, and a recommended path.●····●32/40DHDNA Profiler — Cognitive Pattern ExtractionPaste any email, proposal, or note someone wrote, and get back a plain-language read on how they think, what drives their decisions, and how they communicate.●····●32/40