§Case study
← All projectsprocure-flow – Procurement Approvals Without a Web Framework
A compact procurement approval service with an explicit state machine, RBAC, append-only audit trail, idempotent writes, and an async event pipeline — all from the Go standard library.
- Shipped
- Reading
- 4 min read
- Stack
- Go
- PostgreSQL
- Redis
- RabbitMQ
- Prometheus
- Docker
“Every transition is explicit. Every write is auditable. Every retry is safe.”
In plain English
procure-flow is a procurement approval system: someone drafts a purchase request, submits it, and the right person approves or rejects it. Every action is recorded, every concurrent edit is caught, and the system keeps working even when Redis goes down.
The point isn’t the business logic — it’s how the code handles the things that go wrong: lost updates, self-approval, stale caches, message delivery failures, and rate limit evasion.
📦 GitHub Repository: https://github.com/amirfaisalz/procure-flow
The problem
Procurement workflows are deceptively simple on a whiteboard: draft → submit → approve. In production:
- Lost updates — two people edit the same request at the same time, and one silently overwrites the other.
- Self-approval — a requester approves their own purchase. It’s not just a UX bug; it’s a compliance failure.
- Audit tampering — someone deletes an audit row to hide a rejected request that was later approved.
- Retry storms — a webhook fails, the relay retries aggressively, and the downstream service gets hammered.
- Rate limit bypass — an unauthenticated attacker probes credentials without being throttled.
What I built
Explicit state machine
The procurement lifecycle is draft → submitted → approved | rejected | cancelled, enforced by the aggregate root — not by scattered if statements across handlers. Invalid transitions return a domain error, not a 500.
Optimistic concurrency
Every write carries an expected Version. If the version in the database doesn’t match, the write fails with 409 Conflict. Lost updates are structurally impossible, not just unlikely.
Real authorization
Four roles: admin, approver, requester, viewer. Approvals are department-scoped — you can only approve requests in departments you’re assigned to. Non-admin users cannot approve their own requests. The system fails closed: missing or invalid credentials → deny.
Auditable by construction
An immutable audit row is written in the same transaction as the state change. A PostgreSQL trigger rejects UPDATE and DELETE on the audit table. If the audit write fails, the state change rolls back. There is no window where state changes without a record.
Idempotent writes
Idempotency-Key is scoped per actor. Same key + same body → replay with Idempotent-Replayed: true header. Same key + different body → 422 Unprocessable Entity. This follows the IETF draft semantics, with RFC 9457 problem documents for all errors.
Distributed rate limiting with graceful degradation
Token-bucket rate limiting shared across replicas via a Redis atomic Lua script. The degradation chain:
- Redis available → shared rate limit across all replicas
- Redis down → fall back to per-process in-memory bucket
- Everything fails → allow the request and log the failure
Failed authentication attempts consume the IP-level bucket before the 401 response — so brute-force probing is throttled even without valid credentials.
Transactional outbox + RabbitMQ
Side effects (notifications, ERP sync) are delivered via a transactional outbox:
- Outbox rows are written in the same transaction as domain changes
- A relay polls with
FOR UPDATE SKIP LOCKEDand publishes to RabbitMQ in confirm mode - Backoff from 1 second to 5 minutes on failure
- At-least-once delivery with
event_idas a deduplication handle - Notification and ERP-sync consumers with their own dead-letter queues
Cache-aside with versioned keys
Read-heavy endpoints use a cache-aside pattern with versioned keys (procurement:v1:<id>) and generation markers to prevent stale data from being written back after a cache miss that races with a write. Cache errors are treated as misses — the system is slower, not broken.
Boring operations
- Liveness and readiness probes with a
degradedstate (Redis down → degraded, not dead) - RED metrics with
ServeMuxpattern labels (no high-cardinality blowup from path params) X-Request-IDand W3Ctraceparentpropagation- Graceful shutdown with in-flight request draining
- Read-only container with
no-new-privilegessecurity policy
Test discipline
A shared repository contract test suite runs against the real PostgreSQL adapter — not mocks. Handler tests use test-only doubles that are never linked into cmd/. The test binary and the production binary share no test infrastructure.
Technology choices
Every dependency earns its place:
| Dependency | Why |
|---|---|
Go net/http stdlib |
No framework overhead. The standard ServeMux handles routing, middleware is just function composition. |
| PostgreSQL | Transactions, triggers, FOR UPDATE SKIP LOCKED, and embedded reversible migrations. |
| Redis | Shared rate limiting and read cache. Explicitly optional — the system degrades without it. |
| RabbitMQ | Reliable event delivery with confirms and DLQs. The outbox pattern decouples domain writes from delivery. |
Docs
User journeys, architecture diagrams (layers, transaction boundaries, failure modes), technology rationale, full configuration reference, OpenAPI 3.1 contract, and .http request collection for manual testing.
Next case study ↓
04 / 2026
ShopStudio
AI Shopify Theme Builder SaaS
A SaaS that turns natural-language prompts into production-ready Shopify Online Store 2.0 themes. Native Liquid sections, JSON templates, and downloadable ZIP — no build step needed on Shopify.
- Next.js
- React
- TypeScript
- Tailwind CSS
- Stripe
- Gemini