MedConnect: Designing an Audit Trail

When something goes wrong in a clinical system, the first question is rarely technical. It is: who looked at this record, and why? An audit trail is how you answer, and it only works if it was designed in from the start. MedConnect, the book's healthcare case study, makes four design decisions that are worth borrowing.

One trace identifier, issued at the door

Every inbound request is given a unique trace identifier at the API gateway. That identifier travels through every layer inside a typed context object, together with a span identifier, the parent span, the user, the session, the stated purpose and the start time. Each layer creates a child span of its own, which builds a tree an auditor can walk to reconstruct everything done on behalf of one request.

The context is immutable once created. Layers may read it and add child spans, but not rewrite what is already there. That rule is what makes the trail trustworthy: a layer cannot quietly adjust its own history.

A log that cannot be edited without a trace

Regulated audit logs need to be tamper-evident. MedConnect uses an append-only log with cryptographic chaining. Each entry records what happened and carries a hash that covers its own contents together with the previous entry's hash. Change anything in the past and every later hash stops matching. The entries live in a dedicated append-only partition, and no application role is granted permission to update or delete them.

The mechanism is small enough to sketch in a few lines:

import hashlib, json

def append(log, event):
    prev = log[-1]["hash"] if log else "0" * 64
    body = json.dumps(event, sort_keys=True)
    digest = hashlib.sha256((prev + body).encode()).hexdigest()
    log.append({"event": event, "prev": prev, "hash": digest})

A general caution, which is not specific to the book: chaining detects edits, it does not prevent them, and a truncated tail can look valid unless the latest hash is also recorded somewhere the writers cannot reach. MedConnect pairs the chain with the permission rule above, which covers the prevention side.

Who, what, when and why, plus what was withheld

Every access to protected data is logged along four mandatory dimensions: who acted, which fields were accessed, when, and for what purpose. The case study adds how the access happened (endpoint, client, address) and whether it was granted, denied or partly filtered.

The last of these is the most telling. The log records which fields were requested against which were actually returned after the minimum-necessary filter ran. That makes the privacy rule visible in the audit data: you can see not only what a user read but what they asked for and were not given.

Keeping it, and watching it

Audit data piles up. MedConnect adopts a retention window longer than the baseline the book cites, to leave margin for variation between jurisdictions, and moves entries through storage tiers as they age: a fast primary store for recent entries, a columnar archive in the middle and encrypted object storage for the oldest. The chain survives every tier, and the moves between tiers are themselves audited. Retention periods are a legal question, so check yours; the book's choices are one example.

On top of the stream sits anomaly detection. The book describes four families of signal: unusual volumes of access by one user or address, access at odd times for a role, access to records outside a provider's own care team, and sequential sweeps through many patient records. A detection triggers a graduated response that starts with alerting the security engineer and escalates to the compliance officer and the start of breach-assessment paperwork. I leave the notification timelines to the book and to your legal advisers.

The takeaway

Notice how much of this is easy only because of the layers. A request crosses the same boundaries every time, so one context object can follow it, and one append operation can record it. That is the difference between a trail and a pile of logs. For the controls that feed it, see MedConnect: security across the layers, and for the structure underneath, MedConnect layer by layer. The resilience side of external dependencies is in resilience patterns for the Resources layer.

What the book covers

  • The typed trace context and how layers add child spans
  • The full audit entry structure with hash chaining
  • The four mandatory access-logging dimensions plus the extra fields
  • A tiered hot, warm and cold retention design
  • Anomaly detection types and the graduated breach response