Loading
Code Deb

Designing Invoice Number Sequences for Concurrent Retail Systems

Design fiscal-year invoice numbering with tenant-scoped atomic counters, idempotent issuance, audit trails and Nepal billing integration boundaries.

Invoice sheets from multiple retail lanes converge into one ordered reference sequence.

A gapless invoice series and a unique invoice series are not interchangeable. In PostgreSQL, a sequence can allocate unique values even when an invoice transaction fails. Whether a missing number is acceptable depends on the applicable series and accounting policy.

The aborted-transaction model

We modeled 1,000 invoice issuance attempts, aborting every seventeenth attempt starting with index zero. In one policy, a sequence number is consumed before the transaction outcome. In the other, the counter increment is committed only with an issued invoice.

ResultCount
Aborted attempts59
Issued invoices941
Sequence high-water mark1000
Transactional counter high-water mark941

The first policy leaves 59 unissued numbers in the model. PostgreSQL explicitly documents that nextval allocations are not reclaimed on abort. The second policy avoids those abort-induced gaps but serializes access to a fiscal counter row. This is a deterministic failure model, not a live database latency result.

What a safe fiscal namespace looks like

-- Within the same transaction as authoritative invoice creation:
UPDATE invoice_counters
SET last_number = last_number + 1
WHERE tenant_id = $1 AND series = $2 AND fiscal_year = $3
RETURNING last_number;

Pre-create the correct scoped counter or initialize it transactionally. Add a unique constraint on the issued invoice's fiscal series and number. The authoritative invoice and its counter update must commit or roll back together. For a retry, look up the existing issuance idempotency record within the same transaction. Invoice References formats references and requires an atomic sequence adapter; it does not certify the issuance system.

The cost of stronger ordering is contention

A counter row is a serial resource. Holding its lock while contacting a payment gateway or printing a receipt extends the critical section unnecessarily. Prepare the invoice data before locking, then perform the minimum issuance work and commit. A per-outlet series can reduce contention only if the business and applicable accounting rules permit it; technical convenience alone does not authorize a new legal series.

Gaps, voids and reprints have different meanings

An aborted attempt is not an issued invoice. A canceled issued document remains historical evidence and may require a reversal document or void record. Reprinting a document does not allocate another number. Preserve the original reference, relevant fiscal-year label and an audit trail. Offline terminals can hold provisional order IDs but must not improvise authoritative fiscal numbers outside an approved allocation protocol. See idempotent network delivery and BS calendar coverage.

Reconcile issuance against the accountant's approved policy

For each series, reconcile allocated references, issued documents, voids, failed attempts and the counter value. Record explained and unexplained gaps separately. Confirm the applicable Nepal billing requirements and any authorized provider with the Inland Revenue Department. Retail POS must preserve these business rules in its integration. The model establishes a distinction between two allocation strategies; it does not prescribe a universal gapless requirement or claim IRD certification.

Method and reproducibility: Controlled synthetic model. See Code Deb's recorded experiment results and reproduction script in the repository. Figures are reproducible from declared inputs, not undisclosed client outcomes. The cited external material is independent of Code Deb's experiment.

More from Code Deb

Read about our agency, explore available services, or contact the team for a project discussion.