Skip to content
Back to skills

Spark Best Practices

ASecurity

Apache Spark 4.0.2 best practices for PySpark and Scala distributed data processing

  • 43 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 9, 2026
businesspythonsqlperformance

Works with

  • cli

Security analysis

A100/100

Scanned September 9, 2026

npx -y skills add baekenough/oh-my-customcode --skill spark-best-practices --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Spark Best Practices?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Spark Best Practices
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/baekenough-spark-best-practices/badge)](https://www.skillsdirectory.com/skills/baekenough-spark-best-practices)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: spark-best-practices
description: Apache Spark 4.0.2 best practices for PySpark and Scala distributed data processing
scope: core
user-invocable: false
---

# Apache Spark Best Practices

> **Version**: Spark 4.x. Key changes from Spark 3.x: ANSI mode is now **default** (stricter SQL type coercion and overflow checks), and Spark Connect provides a decoupled client-server protocol for remote Spark access.

## Performance Optimization

### Broadcast Joins (CRITICAL)
- Use `broadcast(small_df)` for small-large table joins
- Default broadcast threshold: 10MB (`spark.sql.autoBroadcastJoinThreshold`)
- Avoid broadcast for tables > 100MB

### Shuffles (CRITICAL)
- Minimize shuffles: expensive operations
- Use `coalesce()` to reduce partitions without shuffle
- Use `repartition()` only when necessary (causes shuffle)
- Predicate pushdown: filter before joins

### Caching
- Cache DataFrames used multiple times: `df.cache()` or `df.persist()`
- Choose storage level: MEMORY_ONLY, MEMORY_AND_DISK, DISK_ONLY
- Unpersist when done: `df.unpersist()`

## Resource Management

### Executor Configuration
- Executor memory: 80% of available memory per executor
- Executor cores: 4-5 cores per executor (optimal)
- Dynamic allocation: enable for varying workloads

### Partitioning
- Optimal partition size: 100-200MB
- Too few partitions: underutilized cluster
- Too many partitions: task overhead

## Data Processing

### UDFs
- Prefer built-in functions over UDFs
- Use Pandas UDF for vectorized operations
- Avoid Python UDFs (serialization overhead)

### Storage Formats
- Parquet: default for analytics (columnar, compression)
- ORC: alternative to Parquet
- Delta/Iceberg: ACID transactions, time travel

## References
- [Spark Performance Tuning](https://spark.apache.org/docs/latest/tuning.html)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.