agentleFS
karim-bhalwanikarim-bhalwani/agent-skills-collection/skills/spark-optimization/SKILL.md

spark-optimization

Skill0 starsChanged 10 months agoMITView on GitHub

Specialist in Apache Spark performance optimization—partitioning strategies, memory tuning, shuffle reduction, and job profiling for production systems. Use when optimizing Spark jobs, tuning performance, reducing costs, profiling applications, or scaling Spark workloads.

8 other projects use a version of this, 7 of them edited.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

5 rules. 0 are common, 5 are distinctive, and 0 are very specific to this project.

  • 0

    Common

    Most of the 7 edited versions we read keep these.

  • 5

    Distinctive

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use this skill when:

    line 23
  2. Distinctive:

    Performance Profiling: Read Spark UI, identify bottlenecks (shuffle, GC, serialization, I/O)

    line 33
  3. Distinctive:

    Check for data skew (uneven task durations)

    line 52
  4. Distinctive:

    Test with production data volume and characteristics

    line 65
  5. Distinctive:

    Document optimization in runbook for future engineers

    line 67

“Common” is measured: kept by at least 6 in 10 of the 7 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md
  • performance
  • distributed-computing

Spark Optimization - Performance Tuning

Overview

The Spark Optimization skill focuses on diagnosing and fixing performance bottlenecks in Apache Spark jobs. This is a specialized, deep-focus skill for when you have a Spark job that's slow, consuming too much memory, or not scaling efficiently.

Use this skill when:

  • A Spark job exceeds its SLA or budget
  • You need to debug why a job is slower than expected
  • You're optimizing for large-scale data processing
  • Shuffles, skew, or memory pressure are causing failures
  • You need to choose between partitioning strategies or join algorithms

Core Capabilities

  • Performance Profiling: Read Spark UI, identify bottlenecks (shuffle, GC, serialization, I/O)
  • Partitioning Optimization: Right-size partitions, implement skew handling, partition pruning
  • Join Optimization: Choose between broadcast, sort-merge, and bucket joins based on data characteristics
  • Memory Tuning: Optimize executor memory, cache strategy, and GC behavior
  • Serialization: Configure Kryo, choose columnar formats for large data
  • Caching Strategy: Identify what to cache, when to cache, and when to spill

When to Use

Workflow / Process

Phase 1: Diagnosis

  1. Enable Spark UI and run job to completion
  2. Analyze executor usage, task durations, and shuffle size
  3. Identify bottleneck: CPU, memory, I/O, or scheduling

Phase 2: Root Cause Analysis

  1. Check for data skew (uneven task durations)
  2. Review join strategy (broadcast vs sort-merge vs bucket)
  3. Analyze partition count and size
  4. Evaluate serialization and format choices

Phase 3: Optimization

  1. Apply targeted fix (e.g., repartition, broadcast join, caching)
  2. Measure impact: runtime, memory, cost
  3. Iterate if improvements insufficient

Phase 4: Validation

  1. Test with production data volume and characteristics
  2. Monitor side effects (new bottlenecks, memory pressure)
  3. Document optimization in runbook for future engineers

Constraints

Technical Constraints:

  • Cannot fundamentally change data or algorithm (that's architect/pipeline-engineer role)
  • Optimizations must not sacrifice correctness or data consistency

Scope Constraints:

  • In Scope: Spark configuration tuning, query optimization, serialization, caching strategy
  • Out of Scope: Infrastructure provisioning, algorithm redesign, non-Spark systems

Reference Examples

See examples/ directory for:

  • Partitioning strategies and skew handling
  • Join algorithm comparisons (broadcast, sort-merge, bucket)
  • Memory tuning and caching patterns
  • Serialization and format optimization
  • Detailed Spark UI analysis walkthroughs

Version History:

  • 1.0 (2026-01-24): Spark-focused optimization skill

    skew_ratio = stats["max"] / stats["avg"] print(f"Skew ratio: {skew_ratio:.2f}x (>2x indicates skew)")


## Configuration Cheat Sheet

```python
# Production configuration template
spark_configs = {
    # Adaptive Query Execution (AQE)
    "spark.sql.adaptive.enabled": "true",
    "spark.sql.adaptive.coalescePartitions.enabled": "true",
    "spark.sql.adaptive.skewJoin.enabled": "true",

    # Memory
    "spark.executor.memory": "8g",
    "spark.executor.memoryOverhead": "2g",
    "spark.memory.fraction": "0.6",
    "spark.memory.storageFraction": "0.5",

    # Parallelism
    "spark.sql.shuffle.partitions": "200",
    "spark.default.parallelism": "200",

    # Serialization
    "spark.serializer": "org.apache.spark.serializer.KryoSerializer",
    "spark.sql.execution.arrow.pyspark.enabled": "true",

    # Compression
    "spark.io.compression.codec": "lz4",
    "spark.shuffle.compress": "true",

    # Broadcast
    "spark.sql.autoBroadcastJoinThreshold": "50MB",

    # File handling
    "spark.sql.files.maxPartitionBytes": "128MB",
    "spark.sql.files.openCostInBytes": "4MB",
}

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()

Resources

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.