# An invoice's reliability

> From number collisions to dual-write, from duplicate delivery to determinism: how an invoice moves on without being lost, issued twice or changing outcome.

- Kind: Presentation
- Published: 2026-10-03
- Slides: 15
- Based on these articles: https://muhammetsafak.com/blog/resolving-invoice-number-collision-in-e-invoice-integration/ · https://muhammetsafak.com/blog/from-dual-write-to-outbox-idempotent-consumption-and-field-encryption/ · https://muhammetsafak.com/blog/same-message-different-outcome-determinism-in-event-driven-architecture/ · https://muhammetsafak.com/engineering/systems/race-conditions-and-gaps-in-sequential-numbering/ · https://muhammetsafak.com/engineering/systems/transactional-outbox-dual-write-and-idempotent-consumption/
- Source: https://muhammetsafak.com/visuals/d/an-invoices-reliability/
- Language: en-US
- Author: Muhammet Şafak

---
## Slide 1 — An invoice's reliability

Muhammet Şafak — Visuals

Number, write, repeat and outcome: an invoice can go wrong four times on its way.

Muhammet Şafak October 3, 2026

## Slide 2 — Number

Section 01

In e-invoicing, a number must be both free of duplicates and free of gaps.

## Slide 3

> Generate too late and you get a race; generate too early and you get a gap.

Muhammet Şafak Number collision in an e-invoice integration

## Slide 4 — Four approaches, two constraints

Two constraints

The database's own sequence (SEQUENCE, AUTO\_INCREMENT) does not give back a value consumed on rollback; it produces gaps.

| Approach | No duplicates | No gaps | Failure mode |
| --- | --- | --- | --- |
| MAX()+1 at send time | No | Yes | Collision with parallel workers |
| Generate at enqueue | Yes | No | Failed or cancelled → gap |
| DB sequence | Yes | No | Rollback doesn't return the value → gap |
| JIT reservation | Yes | Yes | Cost: an early commit is mandatory |

## Slide 5 — The number, right before sending

JIT reservation

1.   Step 1: Lock `lockForUpdate`
2.   Step 2: Reserve `if reserved_no is empty, counter++`
3.   Step 3: Early commit `status = SENDING`
4.   Step 4: Integrator `outside the lock`
5.   Step 5: Retry `same reserved_no`

## Slide 6

> A lock is held for DB state, not for external I/O.

Muhammet Şafak The integrator call takes 200 ms to 2 seconds · Engineering

## Slide 7 — Delivery

Section 02

The invoice goes to the database, the event to RabbitMQ: two systems, no single atomic step.

## Slide 8 — Writing to two systems

Dual-write

Dual-write save() and publish() are separate

-   Publish fails → lost event
-   Rollback happens → phantom event
-   DB::transaction does not cover RabbitMQ

Transactional outbox The write drops to a single system

-   Invoice and outbox row in the same commit
-   A separate process publishes to the broker
-   Dual-write shrinks to a single write

## Slide 9 — Repeat delivery must be harmless

Repeat

|  | At-least-once only | Idempotent consumer |
| --- | --- | --- |
| Delivery | At least once, sometimes twice | At least once, sometimes twice |
| Record | Processed ids are not kept | processed\_messages, unique constraint |
| Transaction | Work and stamp are separate | Work and stamp in the same transaction |
| Outcome | A repeat repeats the effect | Effectively-once |

## Slide 10

> Write the data to a single system, suppress repeats at the consumer, seal the sensitive field before it leaves.

Muhammet Şafak Outbox, idempotent consumption and field encryption

## Slide 11 — Outcome

Section 03

The outbox guaranteed delivery; it did not guarantee the same outcome.

## Slide 12

The real equation

outcome = f(message, hidden inputs )

Between production and consumption there can be milliseconds, twenty minutes or three days.

## Slide 13 — Six hidden inputs

Hidden inputs

Six hidden inputs encountered in an event-driven invoice flow.

| Input | In practice |
| --- | --- |
| Time | Reading the current clock for the document date |
| Database | Prefix X at creation, Y at send time |
| Configuration | The same allow-list in two separate services |
| External system | "This already exists", but what exists is unclear |
| Environment | One framework swallows the warning, another turns it into an exception |
| Worker | Parallel replicas, locks, repeat delivery |

## Slide 14 — Four moves

A noisy failure beats a silent wrong outcome

1.   Step 1: Freeze the context `at production time`
2.   Step 2: Time is an input `a retry does not change it`
3.   Step 3: Name the fork `write the reason to the log`
4.   Step 4: Don't resolve silently `stop on ambiguity`

## Slide 15 — Thank you

[muhammetsafak.com](https://muhammetsafak.com)

What enters the decision is frozen; what doesn't is carried by reference.
