Sub topic 3
Memory Limits and Larger-than-Memory Data
Assume you know basic Python, tabular data, and SQL. The key question is whether the working data fits in RAM, not whether the source file fits on disk.
Memory Limits
A compressed Parquet file can expand several times when decoded. DataFrame columns, string objects, joins, sorts, and intermediate results need additional memory. Leave room for the operating system and other processes.
| Situation | Practical first choice |
|---|---|
| Data and intermediates fit comfortably in RAM | pandas, Polars, or DuckDB |
| Data is larger than RAM but queryable from files | DuckDB or Polars lazy execution |
| Data needs parallel processing across machines | Spark |
A single-node engine can process larger-than-memory data by reading only needed columns, filtering early, and processing batches. This is still limited by operations such as large joins and sorts.
Distribution Costs
Spark divides work among executors, which are processes running on cluster machines. This helps with large workloads, but distribution adds costs:
- A shuffle moves rows between executors for operations such as
groupByand joins. - Cluster startup and task scheduling add latency.
- Clusters require configuration, monitoring, permissions, and maintenance.
- Small jobs may run faster on one machine because they avoid these costs.
A distributed design is worthwhile when the workload is too large or too slow for one machine, or when an existing Spark platform reduces operational effort.
Moving Data Between Tools
Use Parquet for durable files and Arrow-compatible interfaces for in-process transfers. Conversions can copy data, so measure them when performance matters.
import duckdb
import pandas as pd
import polars as pl
pdf = pd.DataFrame({"region": ["eu", "us"], "revenue": [10, 20]})
pldf = pl.from_pandas(pdf)
back = pldf.to_pandas()
summary = duckdb.sql(
"SELECT region, sum(revenue) AS revenue FROM pdf GROUP BY region"
).df()
print(back)
print(summary)Benchmarking and Decisions
Benchmark your own representative files, filters, joins, group-bys, output formats, and cold and warm runs. Record wall time, peak memory, data read, and total cost. Published benchmarks often use different hardware or queries.
Consider team Python and SQL skills, deployment targets, existing cluster expertise, observability, and maintenance. Choose the simplest tool that meets the measured requirements:
- Start with pandas for familiar, comfortably in-memory analysis.
- Choose Polars for fast single-machine DataFrame work and lazy optimization.
- Choose DuckDB for SQL over local files and embedded analytics.
- Choose Spark for sustained, distributed workloads.
Migration and Mixing
Migration is worth its cost when repeated failures, runtime limits, or infrastructure requirements justify testing and retraining. Otherwise, mix tools at clear boundaries: store results in Parquet, query them with DuckDB, transform selected data with Polars, and use pandas for small reporting steps.
Exercise
A 30 GB Parquet dataset contains a simple aggregation, and a 64 GB machine has enough free RAM for only 10 GB. What should you test first?
Show answer
Test column pruning and filtered aggregation with DuckDB or lazy Polars. If the operation still exceeds one machine's resources, evaluate Spark and include shuffle and cluster costs in the benchmark.