Sub topic 15
A decision framework
This chapter assumes you have read the earlier chapters on each tool. You should know the difference between eager and lazy execution, roughly how much memory a DataFrame uses, and what a shuffle is. Here you will turn that knowledge into a repeatable way to choose a tool and defend the choice.
Why a framework helps
Tool choice often follows habit or whatever a blog post praised last month. Both are weak reasons. A framework forces you to write down the facts that matter: how big the data is, what the work looks like, who will maintain it, and where it runs. Then the choice often becomes obvious, and the cases that remain close become visible.
The framework below is a sequence of questions. Answer them in order. Earlier questions rule out tools, and later ones break ties.
Question 1: How large is the data, really?
Measure the data in the form the tool will hold it, not the size of the file on disk. A compressed Parquet file can expand several times when loaded. A rough rule is that a pandas DataFrame needs two to five times the raw data size once you count intermediate copies during joins and group-bys. Polars and DuckDB usually need less, and both can process data in chunks.
Use three size bands as a starting point. Treat the boundaries as soft, because they depend on your hardware and your queries.
| Working data size | Fits in RAM on one machine? | Usual starting points |
|---|---|---|
| Up to a few GB | Yes, with room to spare | pandas, Polars, or DuckDB |
| Tens of GB up to a few hundred GB | Not as a pandas DataFrame, but often yes for streaming engines | Polars (lazy) or DuckDB |
| Many hundreds of GB and above, or data already spread across a cluster | No | Spark, or a larger single machine with DuckDB or Polars |
Check the size with a quick script before you decide. This one estimates the in-memory footprint of a sample and extrapolates.
import pandas as pd
sample = pd.read_parquet("sales.parquet").head(100_000)
bytes_per_row = sample.memory_usage(deep=True).sum() / len(sample)
total_rows = 250_000_000 # take this from the file metadata or a COUNT(*)
estimate_gb = bytes_per_row * total_rows / 1e9
print(f"Estimated in-memory size: {estimate_gb:.1f} GB")If the estimate is below roughly a third of your RAM, pandas is safe. If it is between a third and the full amount, expect trouble with joins. If it exceeds RAM, you need an engine that can stream or spill to disk, or a cluster.
Question 2: What shape is the workload?
Size alone is not enough. Consider what the job does.
- Interactive exploration. You run many small queries and look at results. pandas, Polars, and DuckDB all work well. pandas has the largest ecosystem of plotting and notebook helpers.
- Scan-heavy analytics. You filter and aggregate a few columns of a wide table. Columnar engines with lazy optimization (Polars, DuckDB, Spark) read only the columns and row groups they need, so they gain the most here.
- Join-heavy transformations. Large joins and sorts are memory hungry. DuckDB and Polars can handle them on one machine up to a point. Past that point, Spark distributes the join through a shuffle.
- Row-by-row or custom logic. Arbitrary Python functions per row run slowly in every tool. pandas handles this most naturally. In Spark, Python user-defined functions add serialization costs.
- Repeated pipelines on a schedule. Reproducibility and testability matter more than interactive comfort. Any tool works, but pick one your orchestration already supports.
- Streaming or continuously arriving data. Spark has a structured streaming API. The other three are batch-oriented, so you would run them on micro-batches yourself.
Question 3: Is distribution justified?
Spark pays a fixed cost in startup time, serialization, shuffles, and cluster operations. Distribution is justified when at least one of these holds:
- The data does not fit on any single machine you can reasonably rent, even with disk spilling.
- The job must finish within a deadline that one machine cannot meet, and the work parallelizes well.
- Your organization already runs Spark and the data lives in a lakehouse that Spark manages.
If none holds, a single large machine is usually cheaper and simpler. Renting one machine with 256 GB of RAM and running DuckDB or Polars is often faster than a small cluster, because it avoids network transfer between nodes. Always weigh engineer time against compute cost. A cluster you must debug and tune costs more than the invoice shows.
Question 4: What does the team know and what does the platform support?
Skills and deployment constraints are real technical inputs.
- A team fluent in SQL gets value from DuckDB quickly, because the interface is the language they already use.
- A team with years of pandas code has a migration cost for any other tool. Count that cost.
- Polars uses an expression API that differs from pandas. Expect a learning period and different habits, such as avoiding row-wise loops.
- Spark needs people who understand partitioning, shuffles, and cluster configuration, or a managed platform that hides them.
- Deployment matters too. DuckDB and Polars install as ordinary Python packages and run inside a container or serverless function. Spark needs a cluster or a managed service.
Putting the questions together
The flowchart below encodes the sequence. It is a starting point for a shortlist, not a verdict.
Worked examples
A 2 GB CSV export, one analyst, a weekly report. The data fits easily. The analyst knows pandas, and the job runs once a week. Choose pandas. Switching tools would cost more than the seconds saved.
A 60 GB Parquet dataset, a nightly aggregation on one server with 64 GB of RAM. This exceeds what pandas can hold, but the query is a filter and a group-by over a few columns. Choose DuckDB or Polars with lazy scanning. Both read only the needed columns and stream the rest. Try both on a sample and keep the faster one.
A 5 TB event log in object storage, shared by many teams, with an existing cluster. One machine would struggle with the volume and the concurrency. Choose Spark, because the platform and the skills already exist.
A 300 GB dataset, a small team, no cluster. This is the contested case. A large single machine with DuckDB may handle it. Benchmark it before you commit to a cluster.
Common mistakes
- Choosing by published benchmarks. Those numbers measure someone else's data, hardware, and queries. Run your own, as the benchmarking chapter describes.
- Reaching for Spark too early. Teams often adopt a cluster for data that fits on one machine and then spend months on operations.
- Staying on pandas too long. Repeated out-of-memory errors and long waits signal that the workload has outgrown it.
- Ignoring the cost of change. Rewriting working code carries bug risk. A tool that is 3x faster on a job that takes 20 seconds saves little.
- Treating the choice as exclusive. You can use more than one tool. The next chapter covers mixing tools through Parquet and Arrow.
A short checklist
- Estimate the in-memory size, including copies, and compare it with available RAM.
- Classify the workload: exploratory, scan-heavy, join-heavy, custom logic, scheduled, or streaming.
- Decide whether a single large machine is enough before considering a cluster.
- Check team skills and the deployment environment.
- Shortlist two tools and benchmark them on real data.
- Record the decision and the conditions that would change it.
Exercise
A team has a 120 GB Parquet dataset on a server with 64 GB of RAM. The nightly job filters rows, selects four columns out of forty, and groups by customer. The team knows SQL well and has no cluster. Which tool would you shortlist first, and why not pandas and Spark?
Show answer
Shortlist DuckDB, with Polars lazy as the alternative. The job is scan-heavy and touches few columns, so a columnar engine reads only the needed data and can stream or spill to disk. The team's SQL skill fits DuckDB. pandas would need to hold the data in memory, plus copies, which exceeds 64 GB. Spark is unjustified because the data fits on one machine's disk, no cluster exists, and the job is a simple aggregation. Confirm the choice with a benchmark on real data.
Exercise
Name two situations where Spark is the right choice even though a single powerful machine could technically hold the data.
Show answer
First, the organization already runs Spark and the data lives in a lakehouse that Spark manages, so the platform and skills exist and adding another tool costs more than it saves. Second, the job is a streaming workload or needs many teams to share the same compute with scheduling and access control, which Spark platforms provide and single-node tools do not.