Video in progress
A regulator demands an answer across the whole system to "who changed which data, and when". How do you design an audit log for microservices?
An audit log is not an ordinary log: it is an immutable, complete, searchable record. Each service writes its audit event together with the business transaction (through the outbox), and they are gathered in a common format into a central append-only store. To answer "who", the user and service identifiers are propagated along the whole chain.
📖 Answer
An ordinary application log isn't suitable for audit: it can be lost, it gets sampled, any admin can delete it, and its format differs from service to service.
The requirements are usually these:
- completeness: if a change happened, an audit record definitely exists;
- immutability: nobody, not even an admin, can change a record silently;
- context: who (the user and the service that acted on their behalf), when, what (the before and after values), from where (IP, channel), why (request ID, ticket);
- search and long retention (often 5–10 years).
The architecture:
- The write happens in the service, inside the business transaction. The audit event goes into the outbox table in the same commit as the business change, so there is never a "change with no audit" case.
{"auditId": "…", "occurredAt": "2026-11-03T10:15:02Z",
"actor": {"type": "USER", "id": "u-981", "via": "admin-portal"},
"action": "CREDIT_LIMIT_CHANGED", "resource": "customer/42",
"before": {"limit": 5000000}, "after": {"limit": 8000000},
"requestId": "6f1c…", "reason": "TICKET-3312"}
- A single schema: not a shared library but a versioned event schema in Schema Registry. Each team doesn't invent its own format.
- A central store that is append-only: a separate Kafka topic → object storage in WORM mode (for example, S3 Object Lock) and a search index. The audit service has no "delete" API at all.
- Proof of integrity: link the records with a hash chain (each record holds the hash of the previous one). If one record is deleted from the middle, the chain breaks.
"Who" is the hardest part. If the user token from the gateway is lost in internal calls, the internal service writes only "order-service" to the audit. So the user identifier is propagated along the chain with token relay or token exchange, while the service's own identity is taken from mTLS or client credentials. Background jobs also get a clear actor like "SYSTEM:reminder-job".
Subtle points: the audit has PII in it too, so access to it is restricted, and the conflict between the retention requirement and the right to erasure is resolved with lawyers. DB triggers or CDC catch "what changed", but they don't know "who and why", so they are only an extra safety net.
Sign up to mark studied questions and save your progress.