AWS Glue PySpark Job Cost Optimization
Problem Statement
The client was experiencing unusually high AWS Glue costs for multiple PySpark jobs. Despite processing datasets of less than one million records, several jobs required G.8X workers to complete successfully, significantly increasing operational expenses.
Business Context
- Sector: Banking
- Scale: < 1 million records per job
- Goal: Reduce AWS Glue execution costs without compromising job reliability or SLAs.
Architecture
The data processing pipeline used AWS Glue for ETL workloads:
- Ingestion: Source data loaded from S3 into Glue PySpark jobs.
- Processing: Multiple transformations and validations implemented in PySpark.
- Execution: Glue jobs configured with high-capacity
G.8Xworkers to avoid out-of-memory failures.
Challenges Faced
- Driver Memory Pressure: Several transformations (e.g., frequent
count()operations) triggered actions that executed on the driver node. - Inefficient Patterns: Use of
head()after full dataset scans and unnecessarybroadcast()operations increased memory usage. - Misleading Scaling: Increasing worker size masked code inefficiencies rather than addressing the root cause.
Solution & Results
A code-level optimization approach was implemented instead of scaling infrastructure.
- Action Reduction: Removed frequent
count()calls and replaced them with conditional checks and cached metadata where possible. - Driver Optimization: Refactored logic to avoid driver-heavy operations and ensured transformations executed on executors.
- Broadcast Control: Eliminated unnecessary broadcast joins and optimized join strategies.
- Execution Tuning: Validated memory usage through Spark UI and Glue metrics.
Results:
- Worker Size: Jobs successfully migrated from
G.8XtoG.1Xworkers. - Cost: Significant reduction in Glue job execution cost per run.
- Performance: Improved job stability with no driver OOM errors.
- Scalability: Jobs now scale predictably with data growth instead of requiring oversized workers.