airflow icon indicating copy to clipboard operation
airflow copied to clipboard

Large nested DagRun.conf payloads cause memory amplification across the task-startup boundary

Open ChahatKumar opened this issue 2 months ago • 5 comments

Under which category would you file this issue?

Airflow Core

Apache Airflow version

3.2.2

What happened and how to reproduce it?

Issue description

Large nested JSON values in DagRun.conf are materialized as Python object graphs and carried through multiple task-startup boundaries, including database deserialization and task workload serialization. This can use several times the wire payload size in memory and creates avoidable worker/supervisor memory pressure when multiple tasks start concurrently.

An illustrative 12,122,014-byte compact JSON payload containing 11,000 nested records produced:

Representation Size
Compact JSON 12,122,014 bytes
Parsed Python object graph 55,294,806 bytes
Peak allocation while running json.loads 55,296,230 bytes

These Python figures are a local materialization baseline, not an Airflow pod RSS measurement. Airflow may hold additional copies while reading DagRun.conf, constructing the workload, encoding it, and decoding it in the task process.

Difference from Airflow 2

Airflow 2's CeleryExecutor sent a small task command and launched the task through airflow tasks run. It did not send the fully materialized task-startup context from the supervisor to a child process through Task SDK MessagePack.

Airflow 2 still had to read and decode DagRun.conf, so its memory use was not zero. Airflow 3 adds another boundary where the context is materialized, MessagePack-encoded, transferred, and decoded again before the task body starts.

Steps to reproduce

  1. Run Airflow 3.2.2 with PostgreSQL and CeleryExecutor.
  2. Create a DAG whose task reads dag_run.conf.
  3. Trigger the DAG with a nested DagRun.conf payload close to 12 MB.
  4. Start several runs or tasks concurrently.
  5. Measure worker/supervisor RSS while Airflow reads the JSONB value, builds the task workload, serializes it, starts the task process, and decodes it.
  6. Compare Airflow 3 with the Airflow 2 Celery task-launch path using the same payload and concurrency.

The object-graph amplification can be reproduced independently with:

import json
import tracemalloc

payload = open("large-conf.json", encoding="utf-8").read()
tracemalloc.start()
value = json.loads(payload)
current, peak = tracemalloc.get_traced_memory()
print(len(payload.encode()), peak)

What you think should happen instead?

Airflow should minimize memory amplification when materializing and transporting large nested DagRun.conf values during task startup.

Possible approaches include transporting persisted JSON as opaque serialized bytes, delaying decoding until the task accesses the value, or otherwise avoiding serialization and deserialization of the full Python object graph at each boundary.

Operating System

No response

Deployment

Official Apache Airflow Helm Chart

Apache Airflow Provider(s)

No response

Versions of Apache Airflow Providers

apache-airflow-providers-celery

Official Helm Chart version

Not Applicable

Kubernetes Version

No response

Helm Chart configuration

Not Applicable

Docker Image customizations

Not Applicable

Anything else?

Related issues:

  • #74023 tracks number representation and Python-type changes when PostgreSQL JSONB-backed DagRun.conf is materialized. This issue covers memory amplification and applies even when all values remain within normal numeric ranges.
  • #73712 tracks MessagePack integer overflow during the same task-startup transport. This issue is specifically about memory amplification for large nested payloads.

Are you willing to submit PR?

  • [ ] Yes I am willing to submit a PR!

Code of Conduct

ChahatKumar avatar Oct 01 '26 12:10 ChahatKumar

Looked into this. Valid issue. working on a fix targeting the supervisor to task pipe boundary PR coming.

Subham-KRLX avatar Oct 01 '26 14:10 Subham-KRLX

It's not just the msgpack serialization/deserialization of executioncontext that contains user input. It's also storing the json in postgres and reading it back and postgres does its own serialization/deserialization as well, which also transforms some values - https://github.com/apache/airflow/issues/74023

Overall, the user input json provided in executionContext should go as-is to the DAG without any transformation. Maybe the executioncontext/user input should be base64 encoded or so, and then stored or passed around, instead of trying to parse JSON which will need a lot of memory. Encoding can also prevent those two other issues linked in t he original issue.

sabz90 avatar Oct 02 '26 04:10 sabz90

I traced the task-start path before anyone writes more code here. Short version:

  • The task process always needs one full dict (process_params merges the whole conf, templates and user code expect a dict). So #74392 / #75935 can only save the API server and supervisor copies, not the task one.
  • Changing what the task sees by default would break existing Dags (isinstance(conf, dict), json.dumps(conf), {{ dag_run.conf }}, params overrides).
  • Passing conf around as an opaque string only works while no layer reads it. Non-Python SDKs and older Execution API clients need a dict, so the saving disappears for them, and any future feature that wants to look at conf in the server or supervisor hits the same wall.
  • #74048 showed the task side paying about what the supervisor saved, so the net win is unclear.

One idea that doesn't break anything: an opt-in conf_keys on the Dag. The server only loads those keys (plus the ones named in params) in SQL, and any other key is fetched on demand, so a missing declaration is slower, not broken. Dags without it behave as today. It needs serialization, a subset query per database, and an on-demand fetch endpoint.

Is that worth a PR, or would you rather just document "keep conf small, pass references" and leave the startup path alone?


Drafted-by: Claude Code (Sonnet 5.5)

xchwan avatar Nov 03 '26 08:11 xchwan

After thinking about this more, I think DagRun.conf should only be used for small metadata, like a path, a table name or an ID. The task then uses that metadata to read the actual data itself. Done that way, the size of the data never goes through the API server, the supervisor or the msgpack hop, so there shouldn't be a size problem in the first place.


Drafted-by: Claude Code (Sonnet 5.5)

xchwan avatar Nov 04 '26 04:11 xchwan

I agree. The field was designed for metadata of small size, let’s keep it that way. It can be used to store a pointer to real data, but storing and transferring the data does not need to be a part of Airflow. Let’s just add a section in the documentation describing the intention and expectation.

uranusjr avatar Nov 04 '26 04:11 uranusjr