Actual Code All articles
Code Quality

The Horizon Inquiry Wrote Your Audit Trail Spec. You Should Read It.

Actual Code
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:

  1. Whose time is it? If occurred_at comes 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.
  2. 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.
  3. 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:

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.

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.

All Articles

Related Articles

The Clocks Went Back and Your Cron Jobs Did Not Notice

The Clocks Went Back and Your Cron Jobs Did Not Notice

Running a Business on a £3 Droplet: An Ode to the Most Fragile Infrastructure in Britain

Running a Business on a £3 Droplet: An Ode to the Most Fragile Infrastructure in Britain

Your Sprint Ceremony Is Costing More Than the Contractor: A Napkin Audit of Meeting Waste

Your Sprint Ceremony Is Costing More Than the Contractor: A Napkin Audit of Meeting Waste