Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

Design an audit trail for changes to customer bank details in a Spring Boot service that must show who changed what, when, from which value, and be tamper-evident.

Every change to bank details writes an audit record in the same database transaction, or through the outbox pattern, containing the actor, time, masked old and new values, the reason and the request id, so the change and its record succeed or fail together. The records go into an append-only table on which the application role has only INSERT and SELECT, each record holds a hash of the previous one so any edit breaks the chain, and the records are shipped to write-once storage. Retention and access rules apply to the audit data itself.

Why this question is asked

Bank details are a favourite target for fraud: an attacker or a dishonest insider changes a customer's account number, the next payout goes to the wrong account, and the first question afterwards is who changed it and when. If the audit trail can be edited by the same people or services that make changes, its answer is worthless. Interviewers ask this design question because it tests transactional thinking, data protection and tamper evidence together. In this answer, we will define what the trail must record, write it atomically with the change, compare Envers with domain events, make the table append-only, add a hash chain and write-once storage, and finish with retention and access.

1What the Audit Trail Must Answer

The requirement names the questions: who changed what, when, and from which value to which value, in a way that shows if anyone tampered with the record. Who means the authenticated person or system, not the database account. What means which customer and which field. When is a server timestamp, not the client's clock. From which value is the delicate part, because storing full account numbers in a second table creates a second copy of sensitive data, so we store them masked, for example the last four digits, plus a keyed hash that lets an investigator confirm whether a suspected value matches without revealing it. Tamper-evident means we cannot prevent every change by a powerful insider, but we can make any change detectable.

2Writing the Audit Record in the Same Transaction

The core rule is atomicity: the change and its audit record are written in one database transaction. If the update commits, the record exists; if anything fails, neither exists. Writing the audit record afterwards, or to a separate system in the request, leaves gaps when the second write fails.

Java
@Transactional
public void changeBankDetails(long customerId, BankDetails newDetails, Actor actor, String reason) {
CustomerBankDetails current = repository.findByIdForUpdate(customerId); // row lock
String oldMasked = mask(current.iban()); // "****4321"
byte[] oldHmac = hmac(current.iban());
current.update(newDetails);
auditRepository.save(new BankDetailsAudit(
customerId, actor.id(), actor.type(), Instant.now(clock),
oldMasked, mask(newDetails.iban()),
oldHmac, hmac(newDetails.iban()),
reason, RequestContext.requestId()));
} // one commit: the change and its audit record, or neither
Step throughA bank details change and its audit record
AgentServicePostgreSQLRelayWrite-once store
PUT /customers/42/bank-details, reason
BEGIN; SELECT ... FOR UPDATE
UPDATE bank details
INSERT audit record (prev hash, hash)
INSERT outbox row; COMMIT
read new outbox rows
append audit record
stored, retention lock set

8 messages in order. Play them, or step through them one at a time.

The audit record commits with the change; shipping to write-once storage happens after commit.

Notice the row lock on the current details. It makes sure two concurrent changes are recorded in the order they happened, each with the correct old value. If the audit record must also reach another system, such as a fraud monitoring service, we write an outbox row in the same transaction and a relay publishes it after commit, which gives at-least-once delivery without losing events.

3Envers or Explicit Domain Events?

Hibernate Envers can version the entity automatically: with @Audited, every change creates a row in a history table, linked to a revision with a timestamp, and a revision listener can add the user name. It is quick to adopt and good for seeing what a record looked like at a past date.

For bank details I would still prefer an explicit domain audit event. Envers records columns, not intentions, so it cannot say this was a customer-requested change through support ticket 9134 versus a correction by back office. It stores full old and new values unless we customise it, which conflicts with masking. And its tables are ordinary tables that the application can update. An explicit event written by the service carries the business action and reason, applies masking deliberately, and fits the tamper-evidence design below. Envers can still sit alongside it for technical history.

4Making the Table Append-Only

Append-only means rows can be added and read, but never changed or removed. We enforce it with privileges first, because the application should not even be able to try.

SQL
CREATE TABLE bank_details_audit (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id bigint NOT NULL, actor_id text NOT NULL, actor_type text NOT NULL,
occurred_at timestamptz NOT NULL DEFAULT now(),
old_masked text, new_masked text, old_hmac bytea, new_hmac bytea,
reason text NOT NULL, request_id text NOT NULL,
prev_hash bytea, record_hash bytea NOT NULL);
GRANT INSERT, SELECT ON bank_details_audit TO app_rw; -- no UPDATE, no DELETE
CREATE TRIGGER no_change BEFORE UPDATE OR DELETE ON bank_details_audit
FOR EACH ROW EXECUTE FUNCTION raise_append_only(); -- also stops the owner by mistake
-- as app_rw:
UPDATE bank_details_audit SET reason = 'x' WHERE id = 1; -- ERROR: permission denied

The trigger is a second guard for roles that do have the privilege, such as the owner during a careless maintenance session. It does not stop a superuser, who can disable triggers, which is why we also need tamper evidence.

5Tamper Evidence: Hash Chain and Write-Once Storage

A hash chain makes every record depend on the one before it. When we insert a record, we compute record_hash as SHA-256 over the previous record's hash plus a canonical form of this record's fields, and store the previous hash in prev_hash. If anyone later edits or deletes a record in the middle, its hash no longer matches, and every following record's prev_hash points to a value that does not exist. A verification job walks the chain daily and alerts on the first break.

The chain needs a defined order, so we keep one chain per customer and take the row lock on the customer's details, which we already hold, while reading the last hash. A powerful insider could still rewrite the entire chain from some point on, so we also ship each record, through the outbox, to write-once storage, for example an object store bucket with a retention lock that nobody, including administrators, can shorten. Periodically we anchor the latest chain hash there too. Now an attacker would need to alter both the database and the locked copy, which is the property auditors want.

6Retention and Access to the Audit Data

The audit data is sensitive in its own right, since it holds masked bank details, keyed hashes and who did what. We define how long records are kept, as the financial regulation in our market requires, and enforce it with the storage retention settings and a scheduled purge in the database that a separate privileged job runs. Read access is limited to fraud, compliance and support leads, every read of the audit data is itself logged, and investigators use a tool that shows masked values unless an approved reveal is recorded.

7Follow-up questions

Why not write the audit record from a database trigger instead of the service?

A trigger guarantees that every row change is captured even from outside the application, which is useful as a backstop. But it only knows the database role, not the person or the reason, and it sees columns rather than business actions. I would use the service-level event as the primary record and consider a trigger as an additional safety net for changes made outside the service.

How do you verify the hash chain without slowing down writes?

Verification is a separate job that reads the chain in order and recomputes the hashes, often on a replica, so it never blocks writes. Writes only compute one hash over the previous one, which costs microseconds.

What if the outbox relay is down?

The audit record is already committed in the database, so nothing is lost; the outbox rows wait and the relay catches up when it recovers. We alert if the age of the oldest unshipped row passes a threshold, such as ten minutes.

8Conclusion

In this answer, we designed an audit trail for bank details changes. The service writes an audit record with actor, time, masked old and new values, keyed hashes, reason and request id in the same transaction as the change, and an outbox row ships it after commit. We preferred an explicit domain event to Envers for business meaning and masking, made the table append-only with INSERT and SELECT grants and a trigger, added a per-customer hash chain and write-once storage for tamper evidence, and set retention and access rules for the audit data itself.

What to say in the interviewAbout 80 seconds aloud

Open with

I would write the audit record in the same database transaction as the change, so they commit or fail together. The service locks the current bank details row, records the authenticated actor, server time, the old and new values masked to the last four digits plus keyed hashes so investigators can confirm a value, the reason and the request id.

Then, if they want more

For other systems I add an outbox row in the same transaction and a relay publishes it after commit. Hibernate Envers can version the entity, but I prefer an explicit domain event because it carries the business action and reason and lets me control masking.

The audit table is append-only: the application role only has INSERT and SELECT, and a trigger rejects updates and deletes even from the owner.

For tamper evidence, each record stores the previous record's hash and its own hash over the previous hash plus its fields, one chain per customer under the row lock I already hold, and a daily job verifies the chain.

Since a superuser could still rewrite it, the relay also ships each record to write-once storage with a retention lock and we anchor chain hashes there. Finally, the audit data gets its own retention per regulation and tight, logged read access.

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 "Design an audit trail for changes to customer bank details in a Spring Boot service that...": https://scrivio-interview-library.netlify.app/databases/database-security/0f385b28a9/