Symptoms
- The
xcomtable is one of the largest in the metadata database, and it keeps growing. - The scheduler and UI slow down. Grid and task instance pages that render XComs time out.
- Tasks spend seconds pushing or pulling return values. On Airflow 3, large XComs also travel through the API server, so it uses more memory and its requests get slower.
- Backups and upgrades of the metadata database take much longer than the DAG count suggests.
The root cause is almost always a task that returns data (a DataFrame converted to records, a list of thousands of file names, an API response) instead of a reference to data.
Step 1: measure the problem
PostgreSQL:
SELECT pg_size_pretty(pg_total_relation_size('xcom')) AS xcom_total,
(SELECT count(*) FROM xcom) AS rows;
-- Biggest individual values
SELECT dag_id, task_id, run_id, key, pg_column_size(value) AS bytes
FROM xcom
ORDER BY bytes DESC
LIMIT 20;
-- Which tasks push the most bytes in total
SELECT dag_id, task_id,
count(*) AS rows,
pg_size_pretty(sum(pg_column_size(value))) AS total
FROM xcom
GROUP BY dag_id, task_id
ORDER BY sum(pg_column_size(value)) DESC
LIMIT 20;
MySQL:
SELECT dag_id, task_id, COUNT(*) AS rows_, SUM(LENGTH(value)) AS bytes
FROM xcom GROUP BY dag_id, task_id ORDER BY bytes DESC LIMIT 20;
Column names in the xcom table have changed between Airflow 2 and 3. If a query fails, run \d xcom (PostgreSQL) or DESCRIBE xcom (MySQL) and adjust.
Record the top offenders. The backend change below is a safety net. Fixing those tasks to return references is the real fix.
Step 2: decide the threshold
The object storage backend from apache-airflow-providers-common-io keeps small values in the database and writes values above a threshold to object storage. The database row then holds only a reference.
xcom_objectstorage_threshold | Behaviour |
|---|---|
-1 (default) | Everything stays in the database. The backend is installed but does nothing |
0 | Everything goes to object storage, including tiny values. Every pull is a storage read |
Positive bytes, e.g. 1048576 | Values larger than this go to object storage |
A threshold of 64 KB to 1 MB is a sensible start. Small values such as IDs, counts and paths stay fast, and large payloads move out.
Step 3: prepare storage and the connection
- Create a bucket or container (or a prefix in an existing one) for XComs only, so lifecycle rules don't touch anything else.
- Create an Airflow connection with read, write and delete rights on that prefix only.
- Install the provider and the provider for your storage scheme on every Airflow image:
pip install "apache-airflow-providers-common-io" "apache-airflow-providers-amazon[s3fs]"
# Azure: apache-airflow-providers-microsoft-azure ; GCS: apache-airflow-providers-google
pip show apache-airflow-providers-common-io | grep Version
Check the provider's compatibility table against your Airflow version before you pin it.
Step 4: configure the backend
Use environment variables so that every component gets the same values:
AIRFLOW__CORE__XCOM_BACKEND=airflow.providers.common.io.xcom.backend.XComObjectStorageBackend
AIRFLOW__COMMON_IO__XCOM_OBJECTSTORAGE_PATH=s3://aws_xcom@company-airflow-xcom/prod
AIRFLOW__COMMON_IO__XCOM_OBJECTSTORAGE_THRESHOLD=1048576
AIRFLOW__COMMON_IO__XCOM_OBJECTSTORAGE_COMPRESSION=gzip
The path is a URL. The part before @ is the Airflow connection ID (aws_xcom here). For Azure, use an abfs://<conn_id>@<container>/<path> form. Supported compression values depend on the provider version. gzip works without extra packages, but snappy needs python-snappy.
Warning
Set the backend on every component that reads or writes XComs: scheduler, workers (Celery or Kubernetes pod template), triggerer, DAG processor and webserver or API server. A worker without the setting returns the raw reference string to the next task instead of the value. The UI shows references instead of values.
On Kubernetes with the official Helm chart, put these under env: (or extraEnv) so all pods inherit them, then confirm:
kubectl -n airflow exec deploy/airflow-scheduler -- airflow config get-value core xcom_backend
kubectl -n airflow exec deploy/airflow-scheduler -- airflow config get-value common.io xcom_objectstorage_threshold
Step 5: test before broad rollout
Deploy to a non-production environment first and run a test DAG that pushes one value below and one above the threshold:
from airflow.sdk import dag, task # Airflow 3; on 2.x use: from airflow.decorators import dag, task
import pendulum
@dag(schedule=None, start_date=pendulum.datetime(2026, 11, 1, tz="UTC"), catchup=False)
def xcom_backend_smoke():
@task
def small():
return {"rows": 42}
@task
def large():
return ["x" * 1000] * 5000 # about 5 MB
@task
def check(s, l):
assert s["rows"] == 42
assert len(l) == 5000
check(small(), large())
xcom_backend_smoke()
airflow dags test xcom_backend_smoke
aws s3 ls s3://company-airflow-xcom/prod/ --recursive | tail -5
You should see one object for the large value and none for the small one.
Step 6: roll out to production
- Deploy the image and configuration to all components at once, during a quiet period.
- Existing XCom rows stay in the database and remain readable. Only new pushes above the threshold go to storage.
- Watch task failures for the first hour, especially tasks that pull XComs pushed before the change by long-running DAG runs.
Caution
Rolling back is the dangerous direction. After you remove the backend, XComs already written to object storage are references the default backend cannot resolve. Downstream tasks in in-flight runs then get the reference string, not the data. To roll back, pause the affected DAGs, let running DAG runs finish (or fail and clear them after rollback), then remove the setting.
Step 7: clean up the database
With new large values going to storage, remove old rows and reclaim space. Do a dry run first:
airflow db clean --tables xcom --clean-before-timestamp '2026-10-15T00:00:00+00:00' --dry-run
airflow db clean --tables xcom --clean-before-timestamp '2026-10-15T00:00:00+00:00' --skip-archive --yes
Without --skip-archive, deleted rows are copied to an archive table in the same database. That doesn't free space until you drop it.
Deleted rows don't shrink a PostgreSQL table on disk. Plain VACUUM makes the space reusable. VACUUM FULL returns it to the OS, but it takes an exclusive lock on xcom and blocks tasks while it runs.
VACUUM (VERBOSE, ANALYZE) xcom; -- safe while running
-- Maintenance window only:
-- VACUUM FULL xcom;
Then add a storage lifecycle rule that expires objects under the XCom prefix after your XCom retention period. Make it the same period you use for airflow db clean, so the database and storage don't drift apart.
aws s3api put-bucket-lifecycle-configuration --bucket company-airflow-xcom \
--lifecycle-configuration '{"Rules":[{"ID":"expire-xcom","Status":"Enabled","Filter":{"Prefix":"prod/"},"Expiration":{"Days":45}}]}'
Clearing a task normally removes its XCom row. Don't rely on that to delete storage objects. Let the lifecycle rule handle it.
Fix the tasks that caused it
| Pattern | Replace with |
|---|---|
| Returning a DataFrame or list of records | Write Parquet to the lake and return its path |
| Returning thousands of file names for dynamic mapping | Return a manifest path, or batch into chunks so mapped task count stays sensible |
| Returning a raw API response | Store it in a raw landing zone and return the object key plus a count |
do_xcom_push=True by default on operators that print large output | Set do_xcom_push=False where nothing downstream uses it |
Prevent it next time
- Add a CI check or DAG policy that flags tasks whose XComs exceed the threshold in test runs.
- Track
pg_total_relation_size('xcom')as a metric and alert on growth week over week. - Keep the XCom backend settings in the shared base image or Helm values, never in one deployment's overrides.
- Run
airflow db cleanon a schedule with a defined retention, and keep the storage lifecycle rule aligned with it.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get