Home›Design›Audit and event logs
Design doc · Audit and event logs

Audit logs and event history

Where should an append-only history of everything that happened go?

Applications like this: audit trails, activity history, event sourcing, compliance records. This page is written the way I write a database design doc: the workload first, then where each kind of data goes and why, then how it grows, what breaks, and what I rejected.

The short answerAn append-only table partitioned by time in the relational database, or a stream (Kafka) with a long retention plus a copy in object storage. Never updated, rarely read, kept for years.
1 · The workload

What the application asks of the database

DimensionThis application
WritesSteady. One row per action.
ReadsRare: investigations, compliance exports, a user's activity page.
Read to writeVery low.
ConsistencyThe entry must be committed with the action it records.
LatencyWrites must be cheap. Reads can be slow.
GrowthForever, often by law.
Connected

Real failures behind this design

These incidents are why the choices below matter. Each one is written up with its numbers and sources.

2 · What goes where

Each kind of data, its store, and why

ApplicationRecent historyA partitioned table in the same database as the actionSOURCE OF TRUTHOld historyObject storage in Parquet, or a column storeARCHIVEEvent streams for other servicesKafka or a similar logCOPY

Solid arrows: the application writes there. Dashed arrows: a copy fed from the source of truth (change data capture, an outbox, or a cache fill), which can be rebuilt from it.

DataWhere it goesWhy
Recent historyA partitioned table in the same database as the actionWritten in the same transaction, so an action never happens without its record.
Old historyObject storage in Parquet, or a column storeCheap, and still queryable by analytics tools.
Event streams for other servicesKafka or a similar logOther systems consume the events without querying your database.

How each family of store behaves under traffic: Database families.

3 · Data model

The core tables and the queries they serve

CREATE TABLE audit_log (
  id         bigint GENERATED ALWAYS AS IDENTITY,
  at         timestamptz NOT NULL DEFAULT now(),
  actor_id   bigint NOT NULL,
  action     text   NOT NULL,
  target     text   NOT NULL,
  details    jsonb,
  PRIMARY KEY (at, id)
) PARTITION BY RANGE (at);

REVOKE UPDATE, DELETE, TRUNCATE ON audit_log FROM app_user;
4 · How it grows

Start simple, change one thing at a time

  1. A table in the main databaseSame transaction as the action.
  2. The table dominates storage and backupsMonthly partitions, export old ones to object storage.
  3. Many services produce eventsA shared event stream, with the audit store as one consumer.

Scale it: watch this design grow

Pick a stage, then throw something at it. The numbers are a model to show the shape of each change, not a measurement; the measured numbers are in the lab notebooks.

5 · What breaks first

The failures to plan for

What happensWhat you seeWhat I do
Audit written after the transaction commitsActions with no record when the process crashes in betweenWrite it in the same transaction, or use the outbox pattern.
One giant unpartitioned tableBackups and vacuum take hoursPartition by time.
Personal data in the logA deletion request you cannot honorStore references, not personal data, or plan how to redact.
6 · Alternatives

What I considered and did not pick

Cost. Keep months online, years in object storage. The online part should be the smallest.

How this page was writtenFrom the engines' documentation and common practice. Measured on real servers: what an open transaction costs, which is why audit rows are written in the same short transaction, in the notebooks. Before you build: check the anti-patterns, size it with the capacity calculator, and write your own version from the design doc template. Want a second opinion? Submit it for review.
7 · Where to run it

On-prem, managed, EC2 or Kubernetes

StageWhere it runs
Where I would startIn the same managed database as the action
When it growsOld partitions exported to object storage

For a team without a dedicated database team. Who does which job in each option, and the full-cost comparison: Where to run the database.

8 · Estimate it

The numbers, worked live

The back-of-the-envelope numbers I would want before choosing anything. Move the assumptions to match your application. The link keeps your numbers, so a colleague sees the same ones.

Part 3 of 4 · Design · 24 pagesAll pages

Which database does my application need? What kind of application data goes in which database, and why, as design docs, plus the anti-patterns and the decisions behind them.