"Simplicity under pressure = mastery"
A production-quality ISO 17025 calibration management platform for Beamex Oy Ab, written in Go with PostgreSQL. Provides full calibration lifecycle management (draft -> completed -> certified -> expired), an ISO compliance engine that validates ambient conditions and measurement uncertainty, sequential certificate numbering, and an append-only event-sourced audit log.
The API and compliance engine are a single Go binary. Active calibrations in progress (status: draft or running) are not affected in the database — state is in Postgres, not in-process memory. However, any HTTP requests in-flight are dropped without a response. The auto-refreshing web dashboard stops updating. Because calibration is an offline/batch workflow (not a real-time stream), a brief outage is recoverable: operators can resume after restart without data loss.
All reads and writes go through pgxpool. Slow Postgres blocks every API request (no async path exists). The connection pool will exhaust under sustained latency, causing cascading 500 errors to all clients. There is no circuit breaker or degraded-mode response (e.g., serving cached dashboard stats). The append-only event log (calibration_events) is also written synchronously in the same request — a slow event insert blocks the primary write.
The platform is a single-region, single-node architecture (Docker Compose). Network partition between the API container and Postgres drops all operations. There is no read replica, cache, or offline capability. Certificate generation and compliance checks fail immediately. The dashboard shows a connection error. This is acceptable for the stated single-deployment target but must be addressed before multi-region deployment.
The Certify method is explicitly idempotent: it checks rec.Status == StatusCertified and returns the existing certificate rather than creating a new one. Certificate numbers use sequential DB-generated IDs, preventing gaps or duplicates. However, the CompleteRecord + CreateCertificate sequence is not wrapped in a single database transaction — a crash between completion and certificate creation leaves the record as completed without a certificate, requiring manual intervention.
| Check | Status | Notes |
|---|---|---|
| Invariants defined | ✅ | ISO 17025 rules are explicitly coded: temperature 18-28C, humidity 30-75%RH, deviation <= 2u per measurement point |
| Idempotency | ✅ | Certify() checks for existing certificate and returns it — correctly idempotent |
| Race conditions handled | No row-level locking on calibration records; concurrent certification attempts could both pass the status check before either commits | |
| State consistency | CompleteRecord + CreateCertificate are separate DB calls without a transaction; partial write possible | |
| Structured logging | Not visible from code review — no zap or slog usage found in calibration service; logging strategy unclear | |
| Metrics (not just logs) | ❌ | No Prometheus metrics endpoint; no counters for certifications issued, compliance failures, or request rates |
| Distributed tracing | ❌ | No OpenTelemetry instrumentation; cannot trace a calibration request end-to-end through API -> service -> store |
| Rollback strategy | Migrations exist but no down-migration strategy; rollback requires manual SQL | |
| Safe migrations | Migration execution strategy not confirmed from README — no golang-migrate version table visible | |
| 10x traffic plan | Single Postgres instance; at 10x concurrent calibration events the append-only event log becomes a write hotspot | |
| Bottleneck identified | Synchronous event append on every write operation adds latency to every mutation; could be async | |
| Simplicity test passed | ✅ | Removing the event log, the system still manages calibrations correctly — event log is additive, not load-bearing for core flow |
- Missing transaction for CompleteRecord + CreateCertificate: these two operations must be atomic. A crash between them leaves a
completedrecord without a certificate — an orphaned state that the compliance engine and dashboard will handle inconsistently. - Race condition on concurrent certification: two simultaneous
Certifycalls for the same record can both readstatus != certified, both pass the compliance check, and both callCreateCertificate. UseSELECT ... FOR UPDATEor a DB-level unique constraint on(record_id)in the certificates table. - No metrics: ISO 17025 audit trails require operational traceability. At minimum expose:
calibrations_certified_total,compliance_failures_total{reason},certificates_expired_total, and API request latency. - No structured logging in service layer: the calibration service (
service.go) has no logging — compliance failures and certifications are silent at the application log level, making audit reviews impossible without DB queries. - Certificate expiry not enforced: the data model tracks
expires_atbut there is no background job or API check that transitions certificates fromcertifiedtoexpired— operators must manually track expiry.
- ISO 17025 compliance engine: the compliance checks are specific, correct, and code-documented. Temperature range (18-28C), humidity range (30-75%RH), and per-point deviation validation (|dev| <= 2u) are all explicitly enforced with descriptive violation messages.
- Idempotent certification:
Certify()correctly returns the existing certificate on re-call instead of creating a duplicate — a rare but important case for ISO audit workflows. - Append-only event sourcing: every state transition is recorded in
calibration_eventsas an immutable append — this is the correct pattern for audit-regulated systems. - Sequential certificate numbering:
CAL-YYYY-NNNNNNformat with DB-generated sequential IDs prevents gaps or duplicates in the certificate series. - Distroless runtime image: the multi-stage Docker build produces an ~8MB image, reducing attack surface significantly.
- pgx/v5 connection pooling:
pgxpoolis used throughout — connections are reused correctly, not opened per-request.
- Wrap CompleteRecord + CreateCertificate in a DB transaction: use
pgxpool.BeginTxso the state machine transition is atomic. - Add SELECT FOR UPDATE on certification: prevent race conditions by locking the calibration record row before reading status in
Certify(). - Certificate expiry job: add a background goroutine that runs daily and transitions
certifiedrecords past theirdue_datetoexpired, emitting acertificate.expiredevent. - Add structured logging: instrument
service.gowith zap or slog, logging compliance check outcomes (pass/fail + violations) and certification events with record ID and technician. - Prometheus /metrics endpoint: expose
calibrations_total{status},compliance_checks_total{result}, and request latency histograms — required for any production deployment in a regulated context.