The overnight run that has to finish
Claims go out in a window, remittances come in on a schedule, and statements print on a day. A batch job that fails halfway through at two in the morning is a revenue problem, not an operations one.
The nightly job builds the day's claims, writes the files, and sends them. One night it fails on claim 312 of 480 with an unhandled error.
What happens next decides what kind of system this is. In the bad version, nobody is told, the first 311 claims went out, the remaining 169 are in an indeterminate state, and the following night's run either sends them again — duplicates — or skips them because of a flag set on the first attempt. Somebody finds out in a fortnight when a claim is past filing.
Batch processing in a billing system is not a scheduling problem. It is a reliability problem with a deadline, and the deadline is a payer's submission window.
Make the unit of work a row, not a run
The single design decision that matters: the thing that succeeds or fails is the individual claim, recorded individually, and the run is only a loop over rows.
That gives you resumability for free. A run that dies after 311 claims has 311 claims recorded as done and 169 still pending; the next attempt — five minutes later, automatically — picks up the 169. Nothing is reprocessed, nothing is skipped, and the recovery needs no judgement.
It also gives you the failure granularity you want. One claim that throws should not stop the other 479. It should be marked failed with its error, left out of the file, and put in a queue — and the run should complete. A batch that aborts on the first bad row makes one claim's data problem into everybody's outage.
What remains is making the two steps that are genuinely coupled behave atomically: the file is built, and the claims in it are marked as sent. If the process dies between those, you either have a file whose claims look unsent — and will be sent again tomorrow — or claims marked sent with no file. The usual resolution is to record the intent first, with the control number allocated, then write the file, then confirm; a record in the intermediate state is then a visible, recoverable condition rather than a silent divergence.
The run has to say what it did
Every run writes a record: when it started, when it finished, how many rows it attempted, how many succeeded, how many failed, and what the failures were. That record is the thing you alert on.
The alerts that earn their keep are not "the job threw an exception". They are:
- It did not start. The scheduler did not fire, the container did not come up, the lock from last night was never released. A job that does not run produces no error, which is why this is the alert most often missing and the one that costs the most.
- It did not finish by the deadline. Not "it is slow" — it is past the time by which the file had to be with the clearinghouse.
- The volume is wrong. Four hundred and eighty claims every weekday, and tonight it was nine. No exception was thrown; the query that selects the work had a date comparison that broke. Volume anomaly detection is cruder than it sounds and catches a whole class of silent failure.
- The failure count crossed a threshold. One failed claim is a work item; sixty failed claims is a change somebody made.
Zero is the value to be most suspicious of. Zero claims to send, zero remittances to post, zero statements to print — each is normal occasionally and a broken selector often, and the system cannot tell the difference without knowing what normal looks like.
Concurrency and the second copy
Two instances of the same nightly job running at once will, between them, send some claims twice. This happens for mundane reasons: a deploy restarts a worker while the job is mid-run, a manual trigger overlaps the scheduled one, a scaling event starts a second container.
A lock with a timeout and an owner is enough, provided the timeout is longer than the longest legitimate run and the lock is released on crash rather than held until it expires. The thing to avoid is a lock whose expiry is shorter than the run it is protecting, which converts an occasional overlap into a reliable one.
Manual triggers deserve the same lock as the scheduled run, and a human who triggers a run that is already in progress should be told so rather than queued behind it.
What bites once it is live
Time zones. The practice is in one, the server is in another, the payer's cutoff is in a third, and the day a claim belongs to is a local date. Run schedules in an explicit zone, store timestamps as instants, and derive the business date deliberately rather than from whatever the server thinks today is.
Daylight saving. A job scheduled at a wall-clock time runs twice on one day a year and not at all on another. If the job is not idempotent, mark the calendar.
The clearinghouse window is not your window. A file handed over at 23:58 may be in the next day's batch at the other end, which matters for filing dates. Know which timestamp counts and record it.
Retry does not fix a data problem. A claim that fails validation will fail it again on every attempt. Separate retryable failures — a network error, a locked row — from terminal ones, and cap the attempts on both. A row retried three hundred times fills the log and hides the real failures.
Long runs block deploys, or deploys break long runs. Decide which, and make the job survive a restart. If the unit of work is a row, it does.
Nobody reads the run report. Which is fine, as long as the exceptions are pushed rather than published. A daily email that is green 364 times trains people not to look on the day it is red.
How to test it
- A run killed after half the rows, restarted, completes the remainder and sends nothing twice.
- One row that throws leaves the other rows processed and itself in a failed state with its error recorded.
- Two runs started concurrently result in one doing the work and the other declining.
- A run that processes zero rows when the previous ten weekdays averaged four hundred raises an alert.
- A job that does not start at all raises an alert. Test this by not starting it — the absence of a heartbeat is the hardest thing to notice and the easiest thing to forget to test.
- A business date derived at 23:50 and 00:10 local time gives the two different dates you expect, and the file reflects it.
- A terminal validation failure is not retried; a transient connection failure is, up to the cap.
What we do
We make the claim the unit of work rather than the run, so a failure at two in the morning is 169 rows still pending rather than an indeterminate state; allocate the control number and record the intent before the file is written; and alert on the run that did not start and the night the volume was wrong, because those are the failures that do not announce themselves.
Building something that has to talk to a payer?
If any of the above is a problem you currently have, describe it and we will tell you what we would do about it.