Monday morning, I opened the daily report and found the same 398-yuan order listed three times, while two other orders that should have appeared were missing entirely. I checked the logs: the previous night’s pipeline had indeed “succeeded,” exit code 0, with “done” on the last line.
The pipeline isn’t complicated: every night it pulls data from three sources—the payment backend, the ticketing system, and the CRM—aligns them by order number, writes them into a summary table, and pushes a digest to the group chat at 8:30 the next morning. Four steps, about two hundred lines of Python. It took me roughly two hours from idea to working; getting it to run two weeks straight without incident took nearly three weeks. Not a single pitfall was in the business logic itself.
The day it ran, I thought I was done
The first version was just an extended script: pull three sources, merge by order number, write the summary table, push the notification, straight through top to bottom, with any exception thrown directly out. Cron was set for 23:30—if it failed, it failed, deal with it the next day.
In the demo it worked flawlessly. A demo runs once, and the data and network cooperate. In the real environment, it depended on three premises I hadn’t initially realized: every call succeeds; every successful call happens exactly once; every failure can be retried by rerunning the whole thing. None of the three held.
200 doesn’t mean success, and retry doesn’t mean safe
When the token expired, the payment backend returned HTTP 200 with {'code': 40101, 'msg': 'token expired'} in the body and an empty array for data. I only checked status_code == 200, so I took the empty list and kept going, eventually producing a “no orders today” daily report—and gave that success log a thumbs up.
After that, all external calls went through a wrapper: first raise_for_status(), then read code from the body, and throw a custom upstream error if it isn’t 0. The point isn’t the wrapper itself, but that the business-code check must be centralized at the entry point, not scattered across every call site. When scattered, there’s always a new call site that forgets the check—and forgetting doesn’t raise an error, it just silently drops a batch of data. This version was patched in after the fact; before the patch there were already two missed checks, one of which ran for three days before being discovered.
Retry was the second pitfall. I added “retry three times on failure” to the calls, and the next day the table was full of duplicate rows. The write step had failed once, and on retry the whole pipeline reran from step one, writing the export again. The decorator wasn’t wrong; the mistake was choosing the retry granularity at the whole-pipeline level. The rule now: timeouts, connection resets, 429, and 5xx get backoff retries on the individual call, up to three times; other 4xx are thrown out directly—if the parameters are wrong, retrying a hundred times is still wrong.
And duplicate writes can’t be prevented by retry policy—only by the write side:
1 | insert into daily_orders (order_id, amount, paid_at, synced_at) |
The business key must be something that genuinely exists upstream, not “the Nth row of this batch.” My first version used an auto-increment id, which offered zero resistance to duplicate data—green light all the way.
Cross-system pipelines have no transactions, only progress records
Step three writes to the database successfully, step four fails to send the message: the database already has new data, but the group chat still has the old daily report. Across three external systems, there’s no such thing as “rollback.” I once considered wrapping the first three steps in try/except for compensation, but gave up halfway through—the compensation logic was longer than the forward logic, and every new source meant rewriting it, effectively maintaining a reverse pipeline for every pipeline.
Then I switched to a very crude approach: record progress. Add a flow_run table, with the primary key being run_id (date plus pipeline name) and step, one row per step’s status running/done/failed, plus a jsonb field for intermediate results. Write running before each step starts, change to done on success. On rerun, start from the first non-done step—“rerun the whole thing” becomes “resume from the breakpoint,” and the duplicate-write problem shrinks by more than half.
stateDiagram-v2 [*] --> 待运行 待运行 --> 执行中: cron 触发 执行中 --> 步骤完成: 单步成功 步骤完成 --> 执行中: 还有下一步 执行中 --> 待重试: 可重试错误 待重试 --> 执行中: 退避后重试 执行中 --> 失败: 不可重试或重试耗尽 步骤完成 --> 已完成: 最后一步 done 已完成 --> [*] 失败 --> 告警 告警 --> [*]
The cost is that steps must be serializable. For the summary section I just store the result in jsonb—a few thousand rows a day, good enough. If intermediate data grows to tens of megabytes, this approach would need to switch to writing files plus path references; I haven’t actually verified that yet.
There’s something even cruder but more useful: single-step execution. Run steps individually like python -m flow run --step=export --date=2026-03-01, paired with --dry-run to only print the number of rows to be written and a five-row sample, without writing to the database. With this, when something goes wrong I don’t have to wait until the next night to verify, nor copy-paste half a pipeline into a notebook.
All of this together isn’t elegant. I once considered abstracting it into a “general workflow engine”—config-driven, pluggable steps. After staring at my own diagram for a while, I realized the real requirement was one pipeline with four steps, so I dropped it. Wait until there’s actually a second pipeline.
Alerts are either too noisy or too quiet
The first version of alerts was “send to the group on any exception.” The first week after launch, the group got flooded with over forty messages; the second week I muted the notifications; the third week the pipeline silently died for three days with no one noticing, because it didn’t even throw an exception: the cron machine’s disk was full, and the task never started.
After that I changed two things.
First, tiering. The only failures that genuinely need someone that night are those that “affect the 8:30 daily report”; everything else gets logged first and summarized into one line the next morning. The criteria are written as config inside the pipeline, not left to whoever is on duty to judge on the fly. For the convergence window on same-type errors I set one send per 30 minutes; the cost is occasionally missing a second, different failure—a loss I accept.
Second, heartbeat. Exception alerts only cover “ran and failed,” not “never ran at all.” So every day at 09:30, a separate check:
1 | select count(*) from flow_run |
If the result is 0, alert. This check runs on a different machine, to avoid dying together with the main pipeline. The 26-hour figure leaves two hours of margin; any tighter and it would false-alarm on weekends.
The logging also got reworked once. Early on I logged the full payload of every request for easy debugging. One day I found user phone numbers and shipping addresses in the logs—and those logs were synced to a shared drive. The rule now: logs contain only ids, status codes, row counts, elapsed time, and the first eight characters of the payload’s sha256. To reproduce an issue, take the id and look it up at the source—don’t move data around.
Write the infrastructure first, the business logic last
Now every time I start a new pipeline, I write three things first: a flow_run table, a --dry-run switch, and a heartbeat query. The business code comes last. The order sounds awkward, but what it saves is weeks of debugging later—and that self-doubt of “but it clearly ran successfully.”
If you also have an automated pipeline running right now, here’s a small thing you can do tonight: confirm when it last succeeded, and, if it didn’t run today, how long it would take you to find out. If you can’t answer the second question, add that heartbeat query first—don’t rush to add features.
Comments