The Horizon Inquiry Wrote Your Audit Trail Spec. You Should Read It.
Photo by Photo by Tyler on Unsplash on Unsplash
I have sat through enough incident reviews to know the sentence. Someone asks what happened, and a senior engineer says, with total confidence, 'we can check the database'. Everyone nods. The meeting moves on.
The Post Office inquiry spent years taking that sentence apart in public. Whatever else you conclude from it, one engineering lesson sits there in plain sight: a system that cannot say who did what, and when, cannot defend itself — and neither can the people using it.
What the evidence actually kept circling back to
The inquiry heard, repeatedly, from subpostmasters who said the figures were wrong. And repeatedly the answer was that the branch records were the truth, because the system said so. Discrepancies, shortfalls, transactions that appeared or vanished: the argument was never really about arithmetic. It was about whether there existed a trustworthy record against which a human's account could be tested.
Add to that the questions about remote access from support staff into branch terminals, and about the completeness of the system's own audit data, and you get the shape of the problem. Not 'the database was wrong'. Rather: the record was treated as authoritative without anyone being able to demonstrate why it should be.
You do not work on Horizon. Your app probably schedules dog groomers. The principle transfers anyway.
An audit trail is not a table with an updated_at column
Here is the difference, stated bluntly. A normal table tells you the current state. An audit trail tells you the sequence of events that produced it, each one attributable and immutable.
If your invoices table has updated_at and updated_by, you have a bookmark, not a history. Overwrite a row three times and the first two versions are gone. Change the amount, change it back, and the record shows a tidy, unremarkable row — with no evidence that anything ever happened.
What you want instead is an append-only stream of facts:
create table audit_event (
id bigserial primary key,
occurred_at timestamptz not null,
recorded_at timestamptz not null default now(),
actor_id text not null,
actor_type text not null, -- 'user' | 'service' | 'support'
action text not null, -- 'invoice.amount_changed'
subject_type text not null,
subject_id text not null,
before jsonb,
after jsonb,
request_id text,
prev_hash text,
hash text
);
No updates. No deletes. Permissions on this table for the application user should be insert-only, and that should be enforced by the database, not by a code review promise. If your ORM can quietly UPDATE audit_event, you have not built an audit trail — you have built a suggestion.
The clock is the bit everyone gets wrong
'When' is where audit trails quietly rot. Ask yourself three questions:
- Whose time is it? If
occurred_atcomes from a laptop, a phone or a till with a clock that anyone can change, then your timeline is a claim, not a fact. Record both: the time the actor said it happened, and the time your server received it. Label them differently and never conflate them. - Is it UTC? Store UTC, render local. Always. Twice a year the UK gives you a free opportunity to corrupt a timeline, and daylight saving has ruined better systems than yours.
- Can you prove the order? Timestamps collide, clocks drift, replicas lag. A monotonic sequence number per subject, plus a hash of the previous event, gives you a chain you can verify later. If an event is deleted or altered in the middle, the chain breaks visibly — which is the entire point.
Retention versus the ICO, or: how long do we keep this?
Audit records are personal data when they identify a person, so GDPR applies. That does not mean shred them on day thirty. You have lawful bases available — legal obligation, and the establishment or defence of legal claims — but you have to actually think about it rather than defaulting to whatever your log shipper does.
The practical split most small teams should make:
- Debug logs: thirty days, rotated fast, no need to be beautiful.
- Audit events: years, not weeks, kept lean, with personal data minimised at write time — IDs and diffs, not full records pasted in for convenience.
- A written retention rule in the repo, with the date it was agreed and who agreed it. Future you will be asked 'why seven years?' and 'because that is what it was set to' is not an answer a regulator enjoys.
Also decide, in advance, what happens when someone exercises a deletion right. In most systems the right answer is to strip identifying fields from the audit stream, not to delete the events — but write that down before the request arrives.
A checklist you could actually finish this quarter
Not a maturity model. Just things a team of four can do before the next bank holiday.
- Pick the three entities where disputes actually happen. Billing, permissions, and whatever the client rings about. Audit only those to start.
- Add an insert-only audit table, with grants enforced in the database.
- Write events for create, update, delete, and — the one everyone forgets — read, where reading is sensitive.
- Record actor, action, subject, before, after, request ID, and both timestamps.
- Refuse to accept a client-supplied timestamp as
occurred_at. Store it asclaimed_at. - Hash-chain the stream, and write a tiny job that verifies the chain nightly.
- Give the audit table its own retention policy, separate from application logs.
- Make sure support staff and admin tools write events like everyone else. The inquiry's most uncomfortable questions were about the people with the keys.
- Add one integration test that asserts an audit event exists after a state change, so nobody quietly drops it in a refactor.
None of this is exotic. It is a table, a permission, a hash and a bit of discipline. But the difference between 'we can check the database' and 'here is exactly what happened, in order, and here is the proof nobody edited it' is the difference between a system that defends its users and one that quietly convicts them.