Q23 · How do you design an audit-friendly system without killing performance?
Back to 24 Architecture Questions.
The question is not “log more”. The axis it turns on: whether the audit is a byproduct of the write or a second write. The standard failure: the application writes the entity, then writes the audit log, in a second call. Two writes, two failure modes, and a drift between the ledger and the truth that is discovered exactly when the audit is needed.
What the answer should contain
The byproduct is the design goal. The audit must be produced by the same event that changes the state, so disagreement becomes structurally impossible. Three mechanisms, in order of app-code cost: the outbox (the business row and the audit event commit in one local transaction, a relay publishes them); CDC (the database’s own log becomes the audit source, zero application code, and it catches rogue writes including manual SQL); temporal tables or triggers inside the database (history commits with the row).
A second write is the anti-pattern. The entity write succeeds, the audit write fails or lags, and the ledger now disagrees with the truth. The drift is invisible until the audit is opened, which is the moment agreement is the entire point of having one.
Append-only, always. Audit records are written once and never updated: no UPDATE path exists, and if tamper-evidence matters, the storage is WORM or hash-chained. An audit log that can be edited is a suggestion, not an audit.
The audit never sits on the business write path’s critical section. The commit carries the event (cheap); the pipeline carries the audit elsewhere. Reads of the audit are served from its own store, indexed by actor, time, and correlation ID, never by scanning production tables.
Record intent, not just diffs. Who acted, what action, the state before and after, when, and the correlation ID that ties the whole workflow together. The “why” usually lives in the command that started the workflow; capture it at the entry, or it is gone.
Performance answer in one line: one commit, derived everything. The business write pays only for its own commit; the audit pays for storage and a pipeline, off the request path, batched at leisure.
Ideas worth mentioning out loud
- “An audit trail that can disagree with the state is a liability with a timestamp. The byproduct patterns make disagreement impossible rather than unlikely.”
- CDC is the strongest story in the room: it audits what actually hit the database, including the 2 a.m. manual UPDATE no application event would ever have recorded.
- Ask what the audit is for before designing it: regulatory retention shapes storage; internal forensics shapes indexing; insider-threat concern shapes tamper-evidence.
Follow-ups to expect
- Does the audit need to be synchronous? The commit is; the query path does not need to be. Regulators want the event to exist, not the event to be searchable within milliseconds.
- Triggers or events? Triggers catch everything that touches the tables; application events carry intent. Many systems want both, feeding the same store.
- What is the retention cost? Compliance windows are long; tier the storage (hot for recent, cheap and immutable for the rest) before the volume decides for you.
Red flags in an answer
- The audit written by the same request handler as the entity, in a second call.
- An audit table with an UPDATE endpoint.
- “We turned audit off in production for performance.”
- Audit queries running as full scans on the primary database.
Sources
- Event Sourcing, Martin Fowler: the audit as the event sequence itself, the strongest form of the byproduct argument, with Fowler’s own caveat about the pattern’s demands.
- Debezium: change data capture as the zero-application-code byproduct mechanism, auditing what actually hit the database, including the writes no application event would ever have recorded.
- Temporal tables, Microsoft SQL Server documentation: system-versioned history committing with the row, the byproduct implemented as a database feature.