Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

Your team needs a reliable audit history of changes to 30 tables for compliance. Compare database triggers, Hibernate Envers, application-level events and change data capture with Debezium.

Triggers capture every change, including manual SQL, inside the same transaction, but they add cost to every write and hide logic from developers. Hibernate Envers is easy for JPA entities but misses anything not done through Hibernate, such as bulk updates, native SQL and other applications. Application events carry business meaning, who and why, but rely on every code path publishing them, which the outbox pattern makes reliable. CDC with Debezium captures all committed changes from the WAL or binlog with little write overhead, but lacks business context and needs Kafka Connect infrastructure. Compliance usually needs both completeness and context, so a combination is common: CDC for completeness, with who and why recorded in columns or events.

Why this question is asked

Audit history for compliance is a requirement where being mostly right is not enough: an auditor will ask for every change to an account, including the one made by a support engineer in psql at 2 a.m., and also who made it and why. Interviewers ask this design question because each of the four options is good at a different part of that requirement, and the answer shows whether we can match tools to what compliance actually needs. In this answer, we will define the requirement, look at each option with its strengths and blind spots, compare them, and design the combination we would usually choose.

1What Compliance Usually Requires

A compliant audit history generally needs three properties. Completeness: every change to the audited tables is recorded, whatever made it, including migrations, scripts and manual fixes. Context: for each change, who made it, in business terms (a user or a service, not just a database role), and why, such as the business operation or a ticket number. Integrity: the history cannot be silently edited, is kept for the required period, and can be queried when an auditor asks. With 30 tables, we also care about the effort per table and the impact on write performance.

2Option 1: Database Triggers

An AFTER trigger on each table writes the old and new row to an audit table in the same transaction. Because it lives in the database, it catches every change, whether it comes from Hibernate, native SQL, another application or a person in psql, and the audit row commits or rolls back together with the change, so the two can never disagree. That is the strongest completeness guarantee of the four.

The costs are on every write, since each changed row now writes a second row, and on clarity: the logic is hidden from application developers, who may not know it exists, and it is harder to test, review and migrate than application code. Context is also weak: the trigger sees the database user, often one shared application role, unless the application passes the real user into the session, for example with SET LOCAL app.user_id = '...' at the start of each transaction, which the trigger reads with current_setting().

SQL
CREATE FUNCTION audit_row() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
INSERT INTO audit_log (table_name, op, old_row, new_row, changed_by, changed_at)
VALUES (TG_TABLE_NAME, TG_OP, to_jsonb(OLD), to_jsonb(NEW),
current_setting('app.user_id', true), now());
RETURN NULL;
END $$;
-- one generic function, attached to each of the 30 tables with AFTER INSERT OR UPDATE OR DELETE

3Option 2: Hibernate Envers

Envers is a Hibernate module that records a revision of an entity every time Hibernate writes it. We annotate entities with @Audited, and Envers maintains an _AUD table per entity plus a revision table, where a custom revision entity can store the current user from Spring Security. It is very easy to adopt in a JPA application and gives a convenient query API for the history of an entity.

Its blind spot is anything that does not go through Hibernate's entity lifecycle. JPQL or Criteria bulk updates, native SQL queries, JdbcTemplate code, other applications, migrations and manual fixes are all invisible to it. For compliance, that is a serious gap: the change most likely to interest an auditor, a manual correction, is exactly the one Envers misses.

Java
@Entity
@Audited // org.hibernate.envers.Audited
class Account { @Id Long id; BigDecimal limit; }
em.createQuery("update Account a set a.limit = 0 where a.status = 'CLOSED'").executeUpdate();
// 1200 rows changed, 0 Envers revisions written

4Option 3: Application Events

The application publishes an event for each business operation, for example CreditLimitChanged with the account, old and new limit, the user, and the reason or ticket. Events carry the richest context, because the code knows why it is making the change, and they are naturally at business level rather than row level.

Their weakness is that they depend on every code path publishing them. A new endpoint that forgets, a bulk job, or a manual fix leaves no event. Publishing must also be reliable: sending to Kafka after commit can lose the event if the process crashes in between. The outbox pattern fixes that part, writing the event into an outbox table in the same transaction as the change, from which a relay publishes it, but it cannot fix code paths that never publish.

5Option 4: Change Data Capture With Debezium

Change data capture reads the database's own change log, the write-ahead log in PostgreSQL through logical decoding, or the binlog in MySQL, and turns every committed change into an event with the before and after values of the row. Debezium is the common open-source tool; it runs on Kafka Connect and publishes changes to Kafka, from where they are stored in an audit store. It captures all committed changes, whatever made them, with very little overhead on the writing transaction, because the database writes the log anyway.

The costs are infrastructure, Kafka, Kafka Connect, connectors and their monitoring, and context: CDC knows what changed, but not who or why, unless that information is in the row itself. Replication slots in PostgreSQL also need care, since a stalled connector makes the database keep WAL and can fill the disk.

Comparing the four options
TriggersEnversApp eventsCDC (Debezium)
Captures manual SQL and bulk updatesyesnonoyes
Who and whyonly if passed inuser via revision entityyes, richestonly if in the row
Write overheadextra row per changeextra row per entityoutbox rowvery low
Same transaction as changeyesyesyes, with outboxno, after commit
Infrastructurenonenonebroker plus relayKafka Connect
Visibility to developershidden in DBannotationsexplicit codeoutside the app
No single option gives both completeness and business context.

6The Combination We Would Usually Choose

Since compliance needs both completeness and context, we combine. CDC provides completeness for all 30 tables with little write overhead: every committed change, from any source, lands in an append-only audit store with its before and after values. Context travels with the data: each audited table has changed_by and change_reason columns, or a request id, that the application sets on every write, so CDC events carry who and why. For changes made outside the application, a database default or a check makes missing context visible, and manual access goes through a controlled path that records the ticket.

Where the business needs meaningful events, such as notifying another team that a credit limit changed, the application publishes them through an outbox in addition. If the organisation has no Kafka and the write volume is modest, a generic trigger with session context is a reasonable alternative to CDC that also provides completeness. Envers fits best for a single application that owns its data and needs entity history in the UI, not as the compliance record.

7Follow-up questions

How do you prove the audit trail has not been tampered with?

Write it to an append-only store that application roles cannot update or delete, separate from the operational database, with retention and access controls. Some teams also chain record hashes or use storage with write-once retention. Regular reconciliation between audit counts and table changes detects gaps.

How does CDC get the user when changes come from a shared database role?

Only through the data: the application writes changed_by and a request or reason id into the row with every change, so the WAL contains them. Session settings are not part of the WAL, so a trigger can read them but CDC cannot.

8Conclusion

In this answer, we compared four ways to keep an audit history of 30 tables. Triggers capture everything in the same transaction but cost every write and hide logic. Envers is easy for JPA but misses changes made outside Hibernate. Application events carry who and why but depend on every code path publishing them, made reliable by the outbox pattern. CDC with Debezium captures all committed changes with little overhead but lacks context and needs infrastructure. For compliance we combine them: CDC for completeness, with who and why written into each row, and outbox events where business events are needed.

What to say in the interviewAbout 100 seconds aloud

Open with

I would start from what compliance needs: completeness, every change including manual fixes, and context, who and why, stored so it cannot be edited. Then compare. Triggers are complete, because they run for every change including psql, and they commit in the same transaction, but they add a write to every change, hide logic from developers, and only know the database user unless the application passes the real user into the session.

Then, if they want more

Hibernate Envers is the easiest with JPA and can record the user in a revision entity, but it misses everything that bypasses Hibernate, bulk JPQL updates, native SQL, other applications and manual fixes, so I would not use it as the compliance record.

Application events carry the best business context, but only for code paths that remember to publish them; the outbox pattern makes publishing reliable, not complete.

CDC with Debezium reads the WAL or binlog, so it captures every committed change from any source with very little write overhead, but it needs Kafka Connect, careful handling of replication slots, and it has no who or why unless it is in the row.

So my design would be CDC into an append-only audit store for completeness, changed_by and a reason or request id columns written by the application on every change so the context is in the CDC events, and outbox events where other teams need business events. Without Kafka, a generic trigger with session context is the fallback.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Your team needs a reliable audit history of changes to 30 tables for compliance. Compare...": https://scrivio-interview-library.netlify.app/databases/stored-code/554ee1f13c/