Idempotency and reconciliation in automated invoicing
Written by
Akash Kurup
Published
August 11, 2026
Any system that sends money-related documents will eventually send one twice. Designing for that from the start is far cheaper than discovering it from a customer.
The retry that costs a relationship
Invoice generation is a distributed system whether or not it was designed as one. A job triggers, a document is produced, a mail service is called, a ledger entry is written, a webhook fires. Any of those can time out after succeeding, and the safe-looking response to a timeout is a retry.
Retry a read and nothing happens. Retry an invoice send and a customer receives the same demand twice, with two numbers, and phones somebody. The technical fault is trivial. The consequence is a credibility problem in the one channel where a business cannot afford one.
Idempotency keys derived from the work
The key must come from the business meaning of the operation, not from the attempt.
idempotency_key = sha256(
tenant_id, customer_id, billing_period,
sorted(line_item_ids), rate_card_version
)
Because the key is derived from the inputs, a retry of identical work produces the identical key and the second attempt returns the first result instead of doing the work again. A genuine change to the inputs produces a different key, which is correct: that is a different invoice.
The keyed record must be written in the same transaction as the effect it guards. Writing the invoice and then recording the key is not idempotency, it is a smaller window.
Numbering under concurrency
Invoice numbers are frequently required to be gapless and sequential per financial year and per entity, which is a real constraint rather than a preference. That rules out the two convenient options: application-side counters race, and database sequences leave gaps on rollback.
A row-level lock on a per-series counter, taken inside the same transaction that writes the invoice, gives correctness at the cost of serialising issuance within a series. That cost is acceptable because the series is naturally partitioned by entity and year, and because gapless numbering is not a place to be clever.
Outbox, not direct send
Calling the mail provider inside the transaction that creates the invoice couples two failure domains. If the send succeeds and the transaction then rolls back, a customer holds an invoice the system has no record of, which is the worst available outcome.
The transactional outbox pattern fixes this. The invoice and an outbox row are written atomically, and a separate dispatcher reads the outbox and calls the provider, marking rows as it goes. Delivery becomes at-least-once, and the provider-side idempotency key makes duplicate delivery harmless. The invariant that matters is preserved: the system never has an unrecorded document in the world.
Reconciliation is a first-class job
Automation removes the manual check that used to catch drift, so the check has to be rebuilt deliberately. A scheduled reconciliation compares three things that should agree and rarely disagree quietly:
- Invoices in the application ledger
- Documents the delivery provider reports as sent
- Receipts recorded against those invoices
Every mismatch is classified rather than counted. Sent but not recorded is an outbox or dispatcher fault. Recorded but not sent is a stuck outbox row. A receipt against an unknown invoice usually means a customer paid against a superseded document, which is a business question rather than a bug.
Payment matching, and knowing when to stop
Bank references are entered by humans and arrive mangled. Exact reference matching handles most of it. A scored fallback on amount, date proximity and fuzzy customer name handles more. Beyond that, the correct engineering decision is to stop and queue the item for a person.
Aggressive automatic matching produces a small number of confidently wrong allocations, and an incorrectly allocated payment is far more expensive to unwind than an unmatched one is to resolve. Precision is worth more than recall here, and it is worth a great deal more.
The rule of thumb
Anything that leaves the building carrying a number should be idempotent, recorded before it is sent, and reconciled on a schedule. The cost of all three is a few days of design. The cost of skipping them is measured in customer conversations.
Frequently asked questions
What is idempotency in automated invoicing?
Idempotency ensures that sending an invoice multiple times has the same effect as sending it once. This prevents customers from receiving duplicate demands if a system retry occurs after a timeout. It is achieved by deriving an idempotency key from the invoice's business inputs, allowing subsequent identical requests to return the original result without re-processing.
Why is a transactional outbox used for sending invoices?
The transactional outbox pattern prevents inconsistencies where an invoice is sent but not recorded due to a transaction rollback. It ensures the invoice and a record of its intended send are written atomically. A separate dispatcher then handles the actual sending, guaranteeing the system never has an unrecorded document in the world.
How is gapless invoice numbering managed in automated systems?
Gapless and sequential invoice numbering is achieved by using a row-level lock on a per-series counter. This lock is taken within the same database transaction that writes the invoice. While it serialises issuance within a series, this cost is acceptable because series are naturally partitioned by entity and year, ensuring correctness.
What is the purpose of reconciliation in automated invoicing?
Reconciliation in automated invoicing deliberately compares the application ledger, documents reported as sent by the delivery provider, and recorded receipts. This process identifies mismatches that automation might otherwise miss, such as invoices sent but not recorded or recorded but not sent, ensuring data consistency and addressing discrepancies proactively.
Sources and further reading
- RFC 9110 HTTP Semantics, section 9.2.2: Idempotent methods. IETF, 2022. Defines an idempotent request: one whose intended effect is the same whether it is sent once or several times, which is what makes a retry safe.
- The Idempotency-Key HTTP header field. IETF HTTPAPI working group, Internet-Draft. A proposed standard header that lets a client retry a non-idempotent request, such as creating an invoice, without creating it twice.
- Council Directive 2006/112/EC on the common system of VAT, Article 226. Council of the European Union. Requires every VAT invoice to carry a sequential number that identifies it uniquely, the rule behind gapless invoice numbering.
Want this applied to your own operation?
A working session on your own estate, with a prioritised view of what to build, what to buy and what to leave alone.
0 comments