Business processes / 12 / 12
Gapless document numbering
In many countries issued invoices must carry continuous numbers: per series and per year, no number skipped and none used twice, and an auditor checks the register. A database sequence leaves gaps, MAX()+1 gives duplicates. A counter row taken in the finalize transaction gives both guarantees, at the price of making writers wait in line, so the lesson also measures that price and keeps it small.
- 14 min
- Intermediate
- 12 steps
- Missing numbers
- 0 /yr
- Numbers used twice
- 0 /yr
- Finalizes per series
- 900 /s
- p99 finalize wait
- 4 ms
- request
- reply
- redelivery
- duplicate
- lost
- call: the reply returns on the same line
Numbers are a model scenario, not measurements
The real request
Invoice service → Invoices DBRequest
INSERT INTO invoices (id, number, status, buyer_id)1
VALUES (gen_random_uuid(),
'INV-2026-' || lpad(nextval('invoice_no')::text, 6, '0'),2
'draft', 'b_310');3
-- the number is taken the moment a draft is created
Response
INSERT 0 1 number = INV-2026-0004814
number— The legal identity of the document, printed on it and reported.nextval— A database sequence: fast, never blocks, never goes back.draft— The document is not issued yet, but it already holds a number.INV-2026-000481— Series INV, year 2026, the 481st document of the year.
Build it yourselfCheat sheetCompare lessons
Course: 74 lessons
01 Foundations
02 Scaling
03 Data & storage
04 Communication
05 Reliability
06 Architecture
07 Security
08 Shipping
09 Whole systems
10 Business processes
11 Case studies