FDE Times dataFDE jobs open 932▼14 Change in open roles over 7 days, like-for-like: roles from companies we started tracking this week are left outNew in 7 days 62Companies hiring 202Remote-friendly 24%Median US pay $201kTop hirer Databricks 92
中文VI

The newspaper of the Forward Deployed Engineer

Guides

Your nightly CSV job will die at 2am. Make the rerun harmless

If you hash file contents instead of names, keep a ledger table and quarantine broken CSVs, resent or half-loaded files never get loaded twice.

ShareLinkedInFacebookX

In brief

  • Do not use the file name as the deduplication key. Use the SHA-256 hash of the file's contents.
  • Every file must have exactly one recorded outcome: processed, quarantined with a reason, or skipped as a duplicate.
  • Quarantine data errors, delete the record on infrastructure errors, and reclaim records stuck in progress for too long.
Decision tree for each CSV file, with three questions along a main path. First: is the hash already in the ledger? If so, skip the file and log it. Second: does every record have the full set of fields? If not, the file is quarantined to the quarantine/ folder. Third: did the database load succeed? On a timeout, the partial records are deleted and a dashed arrow sends the file back to the start to rerun from scratch. On success, the file is done.
Every file a customer sends has exactly one outcome, and that outcome is recorded in the ledger. A malformed file stays malformed however often it is rerun, so it is quarantined. On an infrastructure failure such as a timeout, the job deletes the partial records so the next run can load the file cleanly. Source: Powertools for AWS Lambda documentation, Appsilon's data-ingestion design, RFC 4180.

On the third night of a deployment, the client drops orders_1108.csv onto the SFTP server. Your job loads half of it before the database connection times out. In the morning you rerun it, and the first half of the file is loaded a second time. A week later the client sends the same file again as orders_1108_final.csv, and the whole thing goes in once more.

None of those steps involved a logic bug. The problem is that the job has no memory. A nightly batch will need rerunning sooner or later, so safe reruns have to be designed in from day one rather than patched in after an incident.

This guide builds a small Python job that does four things: hashes each file, checks a ledger, validates the CSV structure, then either processes or quarantines the file. The code is a simplified sketch that uses only the standard library and SQLite. You need Python 3, an inbox/ folder standing in for the SFTP drop location, and a quarantine/ folder.

What does a safe rerun mean?

The Powertools for AWS Lambda documentation defines idempotency as an operation producing no additional side effects when it is called more than once with the same input parameters.

For a nightly batch, “the same input” means the same file contents. The file name does not count. The same documentation allows a hash of the entire payload to serve as the idempotency key, and that is the approach this job takes.

Step 1: compute SHA-256 in chunks

import hashlib

def sha256_of(path):
    h = hashlib.sha256()
    with open(path, "rb") as f:
        for chunk in iter(lambda: f.read(1 << 20), b""):
            h.update(chunk)
    return h.hexdigest()

Reading in 1 MB chunks means a file of several gigabytes will not exhaust memory. Check: copy a file under a different name and hash both. The two hex strings must be identical, and if you change one character in the file the string must change completely.

Step 2: one ledger table for every file

Appsilon has described an event-driven design for ingesting clinical data in which each file gets a single JSON record holding its SHA-256 checksum, size and processing status, and the record is written atomically. Here SQLite replaces JSON, but the principle is the same.

CREATE TABLE IF NOT EXISTS ledger (
  sha256     TEXT PRIMARY KEY,
  filename   TEXT,
  size       INTEGER,
  status     TEXT,   -- in_progress | done | quarantined
  reason     TEXT,
  updated_at TEXT
);

Putting the PRIMARY KEY on the hash column gives you two things at once: deduplication and a lock. Powertools uses an “in progress” record to stop two concurrent invocations working on the same payload, and a rejected INSERT has the same effect.

import sqlite3

def claim(db, digest, name, size):
    try:
        with db:  # one transaction
            db.execute(
                "INSERT INTO ledger VALUES (?,?,?,'in_progress',NULL,datetime('now'))",
                (digest, name, size))
        return True
    except sqlite3.IntegrityError:
        return False  # already seen: done, quarantined or running

Check: call claim twice with the same hash. The first call must return True and the second False.

What does a valid CSV file look like?

RFC 4180 offers the easiest structural rule to check: every line in the file should contain the same number of fields. The same RFC recommends enclosing in double quotes any field that contains line breaks, double quotes or commas.

Exports from legacy systems trip over exactly this point, for instance an address column containing a comma that was never quoted.

Step 3: check field counts against the header

import csv

def validate(path):
    try:
        with open(path, newline="", encoding="utf-8") as f:
            rows = csv.reader(f)
            header = next(rows, None)
            if not header:
                return "empty file"
            for n, row in enumerate(rows, start=2):
                if len(row) != len(header):
                    return f"record {n}: {len(row)} fields, header has {len(header)}"
    except (csv.Error, UnicodeDecodeError) as e:
        return f"parse error: {e}"
    return None

The csv module understands quoted fields, so a correctly written address such as "12 Le Loi, Q1" still counts as one field.

The code counts records, not physical lines, because a field containing a line break spans several lines. Check: create a file with an 8-column header and one 7-column row. The function must return a reason that includes the record number.

This is only a structural check. Data types, dates and whether a customer ID exists belong to a separate validation layer, but if a file’s structure is broken the later layers need not run.

Data errors and infrastructure errors take different paths

This is the single most important idea in the job. In Appsilon’s design, failed files are moved to a quarantine path and get their own audit record explaining what happened, instead of being retried. The reasoning is simple: a file with mismatched field counts will still be mismatched after a hundred retries.

Infrastructure errors such as timeouts or dropped connections are the opposite case. Powertools handles them by catching the exception, deleting the idempotency record to avoid inconsistent data, and re-raising the exception. The next attempt then actually runs instead of being treated as finished.

Step 4: put it all together

import shutil, logging

def handle(db, path, quarantine_dir, load):
    digest = sha256_of(path)
    if not claim(db, digest, path.name, path.stat().st_size):
        logging.info("skip %s (sha256 %s already in ledger)", path.name, digest)
        return "skipped"
    try:
        err = validate(path)
        if err:
            shutil.move(path, quarantine_dir / path.name)
            mark(db, digest, "quarantined", err)
            return "quarantined"
        load(path)                      # your import into the customer system
        mark(db, digest, "done", None)
        return "done"
    except Exception:
        with db:
            db.execute("DELETE FROM ledger WHERE sha256=?", (digest,))
        raise

The mark function is just an UPDATE ledger SET status=?, reason=?, updated_at=datetime('now') WHERE sha256=? inside a transaction. The log line for skipped files is mandatory. Appsilon sums up the audit principle in one sentence: nothing is silently dropped, and every outcome, success or failure, is recorded.

Rerunning the third night, this time with a ledger

Go back to the original scenario, except that night there are three files. a.csv is clean and loads fully. b.csv has a record with 7 fields against an 8-field header, so it is moved to quarantine/ with a reason. c.csv is halfway through loading when the database times out, and its record is deleted.

In the morning you rerun the job. a.csv has a hash already marked done, so it is skipped and leaves a log line. b.csv is already in quarantine. c.csv has no record, so it is loaded again from the start.

The following week the client sends a_final.csv, whose contents match a.csv: same hash, so it is skipped. The client sends a corrected b.csv: different contents mean a different hash, so it is processed as a new file.

Four common mistakes when the job goes to production

The first mistake is trusting that the except block always runs. If the process is hard-killed, say by running out of memory, receiving SIGKILL or a machine reboot, no exception is caught. The in_progress record stays forever, and every night the file is skipped as though another worker were handling it.

The fix is to treat any in_progress record older than N hours as dead and allow it to be reclaimed. The sketch below runs at the start of each job:

def reclaim_stale(db, hours=6):
    with db:
        db.execute(
            "DELETE FROM ledger WHERE status='in_progress' "
            "AND updated_at < datetime('now', ?)",
            (f"-{hours} hours",))

N must exceed the longest real processing time for a single file, or you will snatch a file that a live worker is still loading. Check: manually insert an in_progress record with updated_at set 7 hours back, run the job, then confirm the file is processed again.

The second mistake is letting the ledger expire too soon. By default, Powertools expires idempotency records after one hour (3600 seconds). That default suits API requests, but not files a client might resend three weeks later. A file ledger should be kept at least as long as the client could still resend old data.

The third mistake is having load insert rows straight into the live table. If the job dies at row 5,000, deleting the ledger record only lets the file run again; the 5,000 rows already loaded stay put. Load into a staging table inside a transaction and only then move the data to the live table, or upsert on a business key, so the load step itself is idempotent.

The fourth mistake is reading a file while SFTP is still writing it. This sketch does not handle that. In a real environment, agree a convention with the client to signal that a file is complete, such as uploading under a temporary name and then renaming, or sending a checksum file to compare against the hash you compute.

When the client asks “where is my file?”

The ledger earns its keep whenever a client asks that question. With it, you can answer in a minute: which file, when it arrived, its hash, and whether it loaded or was quarantined because of which bad record. Without it, you are digging through logs and guessing.

If you are preparing to move into an FDE role, put this job on GitHub with tests for five scenarios: a clean file, a structurally broken file, a mid-run timeout, a killed process and a duplicate file under a different name. On your CV, do not just write “built ETL”; state specifically that the pipeline reruns without duplicates and audits every file.

When reading job descriptions, if a role involves receiving recurring data from clients, have the story of this job ready. The question worth asking is what happens when the job dies at 2am, and your answer should be “just rerun it.”

Was this article useful?

Use with your AI assistantAsk Claude ↗Ask ChatGPT ↗
3 sources
Read next on the roadmap · Stage 2: Broad engineeringA contract summary with no errors can still miss a clauseLong-context models tend to lose the middle of a document, so split contracts with map-reduce or refine and check the output against a clause list built in code.