# 回填

回填是指在历史时间范围内重新运行数据管道，目的要么是填充一张此前不存在的表，要么是在转换逻辑变更后重新计算历史产出。在数据工程中，该术语涵盖通过一条为当前数据或流式数据设计的管道对历史数据进行追溯处理<sup>[1](https://docs.databricks.com/gcp/en/ldp/flows-backfill)</sup>。回填使用与常规管道相同的业务逻辑，因此历史数据与未来数据的形态完全一致，而不是通过临时路径导入<sup>[2](https://datapanda.eu/en/dictionary/backfill)</sup>。

| 关键事实 | 详情 |
|---|---|
| 定义 | 在历史范围内重新运行管道，以填充新表、修复缺陷或将变更后的逻辑应用于历史时段<sup>[3](https://motherduck.com/glossary/backfill/)</sup> |
| 触发场景 | 在既有历史数据上运行新管道、缺陷修复、新增列或指标、业务逻辑变更、上游数据质量修正、导致数据缺口的中断<sup>[1](https://docs.databricks.com/gcp/en/ldp/flows-backfill)</sup><sup> • </sup><sup>[2](https://datapanda.eu/en/dictionary/backfill)</sup> |
| 典型耗时 | 数小时到数天，长于重试（数秒）或流范围重放（数分钟到数小时）<sup>[4](https://www.thedataengineering.com/mechanisms/data-movement/how-it-works/delivery-semantics/replay)</sup> |
| 核心前提 | 幂等性：以相同输入多次运行管道产生相同结果，不出现重复或不一致的数据<sup>[5](https://www.ibm.com/think/insights/etl-best-practices)</sup> |
| 成本计算示例 | 两年的每日数据为730个区间；每个区间耗时十分钟，则顺序回填约需122小时，即连续运行约五天<sup>[6](https://www.thedataengineering.com/mechanisms/orchestration/how-it-works/safe-re-execution)</sup> |
| 主要策略 | 从源端重算、从消息代理或事件存储重放事件、并行双写并比对<sup>[7](https://amirulislamalmamun.com/practice/data-engineering/concepts/026-backfill-strategies/)</sup> |

## 与相关操作的区别

**重试、重放、重处理、回填。** 重试覆盖瞬时故障后的单条记录或单个批次，在数秒内完成。重放覆盖消费者缺陷或状态损坏后的一段流范围，耗时数分钟到数小时。重处理在逻辑变更或新增列之后用新代码运行历史数据，耗时数小时到数天。回填在新的下游系统出现后，将历史数据送入新的消费者，同样耗时数小时到数天<sup>[4](https://www.thedataengineering.com/mechanisms/data-movement/how-it-works/delivery-semantics/replay)</sup>。重试是一种流控机制；其余三者共享同一种机制，即从保留的输入重新推导产出，区别在于范围和意图<sup>[4](https://www.thedataengineering.com/mechanisms/data-movement/how-it-works/delivery-semantics/replay)</sup>。

**回填与重处理。** 重处理是在已经流经某管道的数据上再次运行该管道，通常是为了修复逻辑错误或应用更新后的规则。回填处理的则是从未被处理过的时段，例如二月份上线的一块仪表盘必须用一月份的数据来填充<sup>[8](https://datacraft.sh/blog/reprocessing-vs-backfilling)</sup>。两者常常同时出现：一条管道因逻辑错误停机三天，先对缺失的天数进行回填，再用修正后的逻辑在更大的窗口上重处理<sup>[8](https://datacraft.sh/blog/reprocessing-vs-backfilling)</sup>。

## 回填为何发生

Databricks列出了若干场景：处理来自遗留系统的历史数据以训练机器学习模型或构建趋势仪表盘；在上游数据质量问题之后重处理一部分数据；覆盖初始管道未包含的时段；以及在业务逻辑变更后重处理历史数据和当前数据<sup>[1](https://docs.databricks.com/gcp/en/ldp/flows-backfill)</sup>。当指标定义发生变化时，例如“活跃用户”的计算方式改变，回填会在每一个历史日期上重新运行该计算，使整个序列一致地反映新定义，而不是在代码变更处出现断点<sup>[3](https://motherduck.com/glossary/backfill/)</sup>。当数据源不可用时，数据也会变得需要回填；一旦缺失的文件或API响应到达，被跳过的窗口就需要填补<sup>[2](https://datapanda.eu/en/dictionary/backfill)</sup>。安全管道在初次搭建时通常会回填数天或数周的数据，这一步称为历史注水<sup>[9](https://www.query.ai/wp-content/uploads/2025/08/2025-08-14_Best-Practices-for-Security-Data-Pipelines.pdf)</sup>。

## 策略

三种策略覆盖了大多数情况<sup>[7](https://amirulislamalmamun.com/practice/data-engineering/concepts/026-backfill-strategies/)</sup>：

- **重算。** 源数据仍然可用，因此当前转换在历史分区上运行。这是数仓原生管道的默认做法：源行存放在原始表中，每个历史分区被覆写。它成本低、幂等，且无需源端协调。当源数据此后已被修改（例如原地更新）时，应避免使用<sup>[7](https://amirulislamalmamun.com/practice/data-engineering/concepts/026-backfill-strategies/)</sup>。
- **重放。** 转换消费Kafka、Kinesis或Debezium馈送等流，历史事件从消息代理或事件存储中重新发出。这适用于代理保留数据的时间长于回填窗口的情况，保留期短时则不适用<sup>[7](https://amirulislamalmamun.com/practice/data-engineering/concepts/026-backfill-strategies/)</sup>。
- **双写。** 新管道与旧管道并行运行，两者写入不同的目标表，比对输出一到两周，然后切换读取方。这适用于旧路径纠缠过深、无法对其重算的迁移场景，代价是要为两条管道同时付费<sup>[7](https://amirulislamalmamun.com/practice/data-engineering/concepts/026-backfill-strategies/)</sup>。

大多数批处理数仓工作是重算；重放出现在事件溯源系统和CDC管道中；双写是迁移工具而非日常工具<sup>[7](https://amirulislamalmamun.com/practice/data-engineering/concepts/026-backfill-strategies/)</sup>。

## 前提条件与正确性

即使任务跨区间依赖先前的任务实例，只要这些依赖得到遵守，回填仍然可以被良好定义<sup>[17](https://airflow.apache.org/docs/apache-airflow/2.3.2/tutorial.html)</sup>。如果任务读取`now()`、查询可变维度表（随时间变化的查找表），或者追加而非覆写，那么这次运行就不是对过去计算的重放，而是一次碰巧运行在旧日期上的不同计算<sup>[6](https://www.thedataengineering.com/mechanisms/orchestration/how-it-works/safe-re-execution)</sup>。同样的挂钟时间问题也出现在“最近7天”窗口或随机UUID等日期过滤器中，它们会使针对过去日期的运行产生错误数据或非确定性键<sup>[10](https://solutiongigs.in/blog/backfilling-data-pipelines)</sup>。

**幂等性**是这样一种保证：以相同输入多次运行管道产生相同结果，不产生重复或不一致的数据。当重试逻辑自动重新运行失败的任务时，非幂等的管道可能载入重复记录并破坏下游分析。工程师通过upsert操作（插入新记录的同时更新已有记录）和检查点（记录进度，使重启的管道从已知的安全状态恢复）来构建幂等性<sup>[5](https://www.ibm.com/think/insights/etl-best-practices)</sup>。

## 顺序、规模与爆炸半径

**顺序与并行。** 顺序运行回填可保持累积语义的完整性，例如累计余额、[缓慢变化维度](https://www.edgepedia.org/zh/slowly-changing-dimension)的链条，以及任何区间N依赖N−1的情况；实际耗时随区间数量线性增长。并行回填受容量约束，但仅在区间真正独立时才是正确的。顺序的代价恰好是顺序回填的时长，可以在决策之前计算出来<sup>[6](https://www.thedataengineering.com/mechanisms/orchestration/how-it-works/safe-re-execution)</sup>。

并行回填是一场蓄意的惊群——众多客户端同时冲击一个资源——而目标数仓的容量是按一次只运行一个任务来配置的。因此回填需要自己的并发上限，最好还有独立于生产运行的资源池，既保护其他工作负载，也防止饥饿<sup>[6](https://www.thedataengineering.com/mechanisms/orchestration/how-it-works/safe-re-execution)</sup>。无论采用哪种策略，标准操作形态都相同：一次一个分区，通常为一天，以有上限的并行任务数排队<sup>[7](https://amirulislamalmamun.com/practice/data-engineering/concepts/026-backfill-strategies/)</sup>。一个具体的故障模式来自追加：回填追加了已存在的行，导致指标重复计数，因此安全序列是幂等写入（如分区覆写或按键MERGE）、参数化日期范围、限流，以及完成后逐分区核对行数<sup>[10](https://solutiongigs.in/blog/backfilling-data-pipelines)</sup>。

## 平台支持

编排器通过为既有管道指定历史日期范围来提供回填功能。在Airflow和Dagster中，触发回填会针对该范围内的每个调度区间重新执行管道<sup>[11](https://blog.pmunhoz.com/glossary/backfill/)</sup>。在dbt中，对增量模型进行回填通常意味着全量刷新<sup>[11](https://blog.pmunhoz.com/glossary/backfill/)</sup>，而dbt的微批策略可以将大型时间序列工作拆分为基于时间的批次<sup>[2](https://datapanda.eu/en/dictionary/backfill)</sup>。Databricks管道将回填实现为写入既有表的独立流：面向具有权威快照的SCD 1型目标的`AUTO CDC FROM SNAPSHOT`流、面向重放历史变更的SCD迁移的一次性`AUTO CDC`流，以及面向仅追加流式表的带`ONCE`选项的追加流。需要考虑的事项包括：优雅地处理重复数据、确保历史模式与当前模式兼容，以及根据数据量和处理SLA调整集群与批次的大小<sup>[1](https://docs.databricks.com/gcp/en/ldp/flows-backfill)</sup>。

一些系统现在将回填作为一等摄取功能。Elasticsearch 9.5通过批量API、OTLP或[Prometheus](https://www.edgepedia.org/zh/prometheus)远程写入，接受带有过去时间戳的文档进入时间序列数据流，随着文档到达创建过去的后备索引；该能力默认关闭，通过一个集群设置即可启用<sup>[12](https://www.elastic.co/search-labs/blog/time-series-data-backfill)</sup>。在自动化程度的另一个极端，微软描述了Azure上一个由LLM驱动的回填代理，它检测缺口、确定恢复窗口，并通过参数化的Synapse管道触发重处理，在治理控制下以自动化决策取代人工识别缺失分区<sup>[13](https://techcommunity.microsoft.com/blog/analyticsonazure/from-manual-backfills-to-autonomous-pipelines-building-an-llm-powered-backfill-a/4516777/replies/4523316)</sup>。

## 设计实践

一个反复出现的原则是：凡是做过一次的事情都应当可重复。如果一条管道在假想的数据丢失后无法重建其数据，那么它日后添加一列或补上额外日期时也会同样吃力<sup>[14](https://thedatapedia.com/wp-content/uploads/2025/01/understanding_etl_data_pipelines_1735417709-1.pdf)</sup>。由于源端保留期可能到期，输入保留设置同时也是最大恢复窗口<sup>[4](https://www.thedataengineering.com/mechanisms/data-movement/how-it-works/delivery-semantics/replay)</sup>。从业者还强调，在存储系统支持的情况下保存先前的分区位置或表快照，使回滚成为一次元数据操作，而不是又一次漫长的计算<sup>[15](https://learn.programmingline.com/learn/data-engineering/data-engineering-backfills-reprocessing-and-late-arriving-data)</sup>；并在回填产出取代消费者所读内容之前对其进行验证，记录重处理了什么、为什么重处理、使用了哪个代码版本<sup>[16](https://datadank.com/data-engineering/system-design/backfill-late-data-handling-system/)</sup>。

## References

1. [Backfilling historical data with pipelines, Databricks on Google Cloud](https://docs.databricks.com/gcp/en/ldp/flows-backfill)
2. [Backfill, Data Dictionary, Data Panda](https://datapanda.eu/en/dictionary/backfill)
3. [Backfill, MotherDuck glossary](https://motherduck.com/glossary/backfill/)
4. [Replay, The Data Engineering](https://www.thedataengineering.com/mechanisms/data-movement/how-it-works/delivery-semantics/replay)
5. [ETL Best Practices: 14 Ways to Build Modern Data Pipelines, IBM](https://www.ibm.com/think/insights/etl-best-practices)
6. [Backfills, catchup and versioning, The Data Engineering](https://www.thedataengineering.com/mechanisms/orchestration/how-it-works/safe-re-execution)
7. [Backfill strategies, Amirul Islam](https://amirulislamalmamun.com/practice/data-engineering/concepts/026-backfill-strategies/)
8. [Reprocessing vs. Backfilling in Data Engineering, DataCraft Academy](https://datacraft.sh/blog/reprocessing-vs-backfilling)
9. [Best Practices for Security Data Pipelines, Query.AI](https://www.query.ai/wp-content/uploads/2025/08/2025-08-14_Best-Practices-for-Security-Data-Pipelines.pdf)
10. [How to Backfill a Data Pipeline Safely, SolutionGigs](https://solutiongigs.in/blog/backfilling-data-pipelines)
11. [Backfill, Glossary, blog.pmunhoz.com](https://blog.pmunhoz.com/glossary/backfill/)
12. [Backfill time series data in Elasticsearch with the bulk API, Elasticsearch Labs](https://www.elastic.co/search-labs/blog/time-series-data-backfill)
13. [From Manual Backfills to Autonomous Pipelines: Building an LLM-Powered Backfill Agent on Azure, Microsoft Community Hub](https://techcommunity.microsoft.com/blog/analyticsonazure/from-manual-backfills-to-autonomous-pipelines-building-an-llm-powered-backfill-a/4516777/replies/4523316)
14. [Understanding ETL Data Pipelines Technical Guide, The DataPedia](https://thedatapedia.com/wp-content/uploads/2025/01/understanding_etl_data_pipelines_1735417709-1.pdf)
15. [Backfills, Reprocessing and Late-Arriving Data, Programming Line](https://learn.programmingline.com/learn/data-engineering/data-engineering-backfills-reprocessing-and-late-arriving-data)
16. [Design a Backfill and Late-Data System, DataDank](https://datadank.com/data-engineering/system-design/backfill-late-data-handling-system/)
17. [Tutorial, Airflow Documentation](https://airflow.apache.org/docs/apache-airflow/2.3.2/tutorial.html)

---
*Initially written Oct 26, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Databases and data systems › Data mining, warehousing, and big data

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/backfill

English original: https://www.edgepedia.org/backfill
