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.

Second write, or byproduct When the application writes the service database and the audit store separately, the two writes drift apart. When the audit is derived from the database's own commit log via CDC or the outbox pattern, it cannot disagree with the state. FIGURE 23 SECOND WRITE, OR BYPRODUCT Second Write Application two writes Service DB the truth Audit Store drifts apart two writes, two failure modes: drift is certain Byproduct Application one write Service DB the outbox lives here Audit Copy derived CDC or outbox the audit is derived: it cannot disagree one write, derived audit: the ledger agrees with the state by construction

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

Follow-ups to expect

Red flags in an answer

Sources