Skip to content

Performance and scaling ​

Where the time goes ​

Issuing a B2C invoice in queue mode does: store the JSON, validate, lock the device counter, build and sign the XML, store it, commit, push a queue job. ZATCA is not called in the request.

Measured on a developer laptop (Windows, SQLite, PHP 8.4, one process, warm): signing about 4 ms, complete issuance p50 about 89 ms, most of it database commits. On a server with MySQL/MariaDB on fast disks and Redis queues, expect a lower figure; measure on your own hardware before committing to a target.

The one rule: one device = one document at a time ​

Each document contains the hash of the device's previous document, so documents of one device are created strictly one after another. Concurrent requests for the same device wait for the counter row lock (up to counter_lock_wait_ms, default 2000 ms).

Throughput therefore scales with the number of devices:

max invoices/second ≈ number of devices × (1000 ms ÷ issuance time per document in ms)
SetupRecommendation
POS terminalsOne device per terminal (natural fit)
Web shop / APIA pool of devices; spread requests over them (for example by order ID modulo pool size)
Nightly ERP batchb2b()->submit() (no signing in the request); workers sign per device in parallel

Very high volume

Workloads of many thousands of invoices per second need many devices, several app servers behind a load balancer, a database sized for the write rate, Redis for queue and cache, and enough queue workers to keep up with ZATCA's own limits. The load-shedding middleware answers 429/503 with Retry-After instead of letting requests pile up into gateway errors (502/504). Benchmark your full stack first; the package alone cannot guarantee a given rate.

Tuning knobs ​

SettingDefaultEffect
performance.signing_key_memory_ttl300 sKeep decrypted signing keys in worker memory (Octane/long-running workers)
performance.max_in_flight_issuance200HTTP API: concurrent issuance requests before 429
performance.counter_lock_wait_ms2000How long a request waits for a busy device
performance.advisory_chain_lockfalseExtra cache lock around issuance (rarely needed)
queues.high_water_mark5000Pending submissions before the API answers 503
http.max_rps0 (off)Optional ceiling on requests per second to ZATCA, shared by all workers. Off: throughput is set by the number of submission workers; ZATCA's 429 answers open the circuit breaker
http.max_in_flight_per_device2Parallel ZATCA calls per device

Infrastructure checklist ​

  • Database: InnoDB (MySQL/MariaDB); enough connections for web + workers.
  • Queue and cache: Redis. Not the sync driver, not the file cache with several servers.
  • Cache: set ZATCA_CACHE_STORE (or the default store) to Redis so the circuit breaker and rate ceiling are shared.
  • Signed XML: stored in the database by default (nothing to share between servers). With ZATCA_ARTIFACT_DRIVER=filesystem, use a shared disk (S3) when several servers issue invoices.
  • PHP: OPcache on; Octane or long-running workers benefit from the in-memory key cache.
  • Workers: start with 4 per server; add more if zatca_outbox lag grows.

Not affiliated with or endorsed by ZATCA. Verify regulatory requirements with ZATCA and your tax advisor.