DEV Community

Cover image for Don't Delete the Ledger Row When the Key Expires. Keep It Marked Expired
AheadKit
AheadKit

Posted on

Don't Delete the Ledger Row When the Key Expires. Keep It Marked Expired

The retry arrived at 01:14 on a Tuesday, twenty-six hours after the first charge. A customer had paid $29 for a book, order ORD-1042, and the charge had gone through on the first try with key evt-1042. Then the network dropped, the confirmation never reached her, and the order went in again on its own. By then the provider had pruned the key. It keeps keys for 24 hours and then throws them away, and this retry landed two hours past that line. The provider looked at the key, found nothing, and shrugged. My own ledger was supposed to answer instead, but a cleanup job had deleted the row the night before, because its key was gone and the job treats a gone key as a done story. So at 01:14 there were two systems and one question, did this money move, and neither system had anything to say. The provider had forgotten. My ledger had been emptied on purpose. The customer was asleep, the retry was sitting unanswered, and the only honest thing the code could have said at that moment was that it did not know.

That is the situation robertpaschal asked about in the comments on my last payment piece, and he asked it in exactly the right shape. A key that outlives its window turns a retry into a false already_charged, he said, and then he put the real question to me: after the provider prunes the key, do you delete the ledger row, or keep it marked expired so the replay still has an answer? One correction to the record before I go further: the 24 hours was my window from the earlier piece, not one he measured. He agrees with the shape of the answer, that marked-expired keeps the answer around and deleted leaves nothing to ask, but agreeing is cheap and I wanted the count. So I ran it.

The setup is the same rig shape as the earlier payment pieces. A small stand-in for the provider, built by me, one that stores charges by key and drops the key when I tell it to prune, the way a real provider drops it after its window. A ledger with one row per order. The order is ORD-1042, $29, key evt-1042. The charge lands first, the key gets pruned, and then the same order is replayed ten times with the row deleted and ten times with the row kept and marked expired. Ten replays before the prune, as a control, so I know what a healthy answer looks like.

The control came back clean, ten out of ten. Before the prune, every replay asked the provider by key and got back already_charged, ten times, no second charge, no hesitation. That is the world the key is built for, and in that world everything works. The trouble starts the moment the key is gone, and the count after that is the whole piece.

Delete the row, and the replay has nothing to ask. Ten replays, ten times the same empty answer: the ledger has no row, the provider has no key, and the handler can only say it does not know. Not charged, not already charged. Unknown, no record. Zero out of ten got a correct answer, because there was no answer left anywhere in the two places the code looks. The money moved, $29 of it, and twenty-six hours later the system that took it could not confirm it had done so.

That empty answer is not neutral. It forces a guess, and the guess people actually make in production is the one that costs money. The common default is to treat "unknown" as "not charged" and charge again, and I measured what that default does here. Ten retries with a charge-again default took the $29 a second time, exactly once. The first retry re-charged, because the key was gone and the provider saw a new request. The second retry found the fresh key the first retry had just created, and answered already_charged, and so did the eight after it. So the damage is one double charge per order, $29 taken twice, and then the new key hides the evidence by making every later retry look correct. The customer sees $58 for a $29 book, and the retry log after the first one looks perfectly clean. The one place that could still tell the truth at that point is the settlement file, the provider's own record of money that actually moved, and it does hold the line: one matching line for ORD-1042, $29, correct. But the file lands the next day. For a full day after the prune, the honest answer in the deleted arm is "I don't know," and the file is the only thing left that knows, and it has not arrived yet.

Keep the row and mark it expired, and the replay never has to ask the provider at all. Ten replays, ten correct answers: already_charged, every time, zero second charges. The handler reads the ledger first, finds the row with its amount, its charge id and its status, and stops there. The provider never gets asked, because there is nothing left to ask it. The key is gone and the answer is not.

The word on the row matters, and it is worth saying why I mark it expired instead of leaving it as charged. A row that still says charged, with a key the provider no longer holds, reads as if the key is still good, and the next person to look at it will try the key first and get the same shrug the retry got. A row that says expired tells a different story in one word: we charged this, and the proof at the provider is gone, and this row is now the record instead of the key. It says the key is dead without deleting what the key used to prove. A cleanup job that deletes expired rows is doing the same damage as the first job, just later. The row is small. A few dozen bytes, an order number, an amount, a charge id, a word. Keeping it costs nothing measurable, and deleting it costs the only answer you have for every replay that arrives after the window closes.

There is a limit to what this run can claim, and I would rather state it plainly. The provider here is a stand-in I built, not a real payment company, and its prune happens the instant I call it, not after a real 24 hours. A real provider's window might be longer or shorter, its prune might be lazy, and its settlement file might arrive late or mangled. I measured none of that. What the run does prove is narrower and still worth keeping: once the key is gone, the ledger row is the only instant answer left, and whether it exists decides whether a replay gets a correct answer ten times out of ten or no answer ten times out of ten. The file can still settle it the next day, but next day is not an answer at 01:14, when the retry is sitting there and the customer is asleep and the code has to decide.

So keep the row. Mark it expired, never delete it, and let the replay read it first. The key is a good tool for the day it lives. The row is the memory for every day after that, and memory is the part you do not throw away just because the thing it remembers has expired.

What does your cleanup job delete? Do your ledger rows carry an expiry mark, or does a pruned key take the row with it, and when did you last replay an order whose key was already gone? Tell me what your replay answered, and whether it answered at all.


P.S. Everything I write stays free — that part doesn't change. But if this one was worth something to you, you can buy me a coffee ☕ (one-time or monthly, both work). It covers the API credits and hosting behind these experiments — and honestly, it's also the nudge that gets the next one started on the evenings I'd otherwise skip it. — Salam

Buy me a coffee

AheadKit on Gumroad — Beginner-Friendly Books & Packs

Top comments (0)