Audit trails: write events, don't rely on database triggers

In regulated software, an audit trail has to answer who did what, where, when and why. Database triggers only capture what changed. Explicit audit events capture the rest, and they are easier to replay for an audit.

Ashar Khalid · · 3 min read · Healthcare, Architecture

In healthcare and pharma software, the audit trail is not a nice-to-have. When an auditor asks about a record, you need to show who changed it, from where, when, under which permissions and, ideally, why. On a multi-country portal I designed for a top-50 pharmaceutical company, every action on regulated data had to be traceable in exactly that way.

There are two common ways to build it. One is quick to add. The other is the one I chose.

The quick option: database triggers

A trigger on each table writes a copy of every insert, update and delete into an audit table. It is attractive because it is automatic. Nobody has to remember to log anything; the database does it.

The problem is what a trigger can see. It sees the row before and after. It does not reliably see:

  • who made the change. The database usually knows only the application’s own account, not the person behind the request;
  • where the request came from, or which country, tenant or role it was made under;
  • why it happened. A status changing from “active” to “inactive” could be a correction, a request from the patient, an automated expiry or a mistake.

You can work around some of this by passing the user into every database session, but it is fragile, and the “why” is still missing. You end up with a perfect record of what changed and a weak answer to the questions an auditor actually asks.

The option I chose: explicit audit events

Instead of inferring audit data from table changes, the application writes an audit event whenever it performs an action on regulated data. Each event carries:

  • the actor: the person, resolved from single sign-on;
  • the scope: the country and role the action was performed under;
  • the action and the record it touched;
  • the timestamp;
  • the reason, where the action has one.

Three rules make this reliable:

  1. Append-only. Events are never edited or deleted. A correction is a new event, not a changed one.
  2. Written before the response returns. If the audit event cannot be recorded, the action is not reported as done. There is no window where something happened but was not recorded.
  3. One shared shape. Every part of the system writes the same event schema, so an audit query works across all of them.

What you get

  • Answers to the real questions. Who, from where, under which role and why are all on the record, not reconstructed after the fact.
  • Replayable history. Because events are append-only and complete, you can replay them to show the sequence of actions on a record for an audit.
  • A shorter compliance story. Explaining “every action writes an event with these fields, and here is the test that proves it” is easier than explaining trigger edge cases.

The trade-off

Explicit events need discipline. A trigger cannot be forgotten; an event can. Two habits manage that:

  • Make writing the event part of the action itself, in shared code that every feature calls, rather than something each developer remembers.
  • Test for it. Automated tests that perform each regulated action and check that the right event was written catch the feature that forgot.

What I would do differently

Agree the audit event schema in the first week, not once the system is taking shape. Every feature writes audit events, so settling their shape early saves rework across every service that produces them.

The short version

Triggers record what changed. Regulated software needs to know who, where, when and why. Write explicit, append-only audit events in one shared shape, before the action reports success.

The pharma engagement portal write-up shows how this fits with country-level data scoping and single sign-on.

Have a project in mind?

Tell me what you want to fix or build, in your own words. You don't need a technical brief.

  • NDA available for sensitive projects