Quick revision
The short answer
First separate the two needs: who last changed a record, which Spring Data auditing columns handle well, and full change history, which needs one organisation-wide mechanism such as change data capture into a central audit store, so it covers every write path including bulk SQL. Then standardise the audit record (actor, time, operation, before and after values, correlation id), protect audit data from modification with a defined retention, and ship a shared starter that provides AuditorAware and security context integration so every service records the actor the same way.
What to remember
In this answer, we saw that a consistent approach starts by separating last-change columns, which every service implements with Spring Data auditing, from full history, which uses one organisation-wide mechanism such as CDC into a central store so every write path is covered. The audit record has a standard shape with actor, time, operation, before and after values and a correlation id; the store is append-only with defined retention; and a shared starter makes the service side consistent by default.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
First separate the two needs: who last changed a record, which Spring Data auditing columns handle well, and full change history, which needs one organisation-wide mechanism such as change data capture into a central audit store, so it covers every write path including bulk SQL. Then standardise the audit record (actor, time, operation, before and after values, correlation id), protect audit data from modification with a defined retention, and ship a shared starter that provides AuditorAware and security context integration so every service records the actor the same way.
Why this question is asked
When each team solves auditing on its own, the company ends up with four answers to one question. One service has updated_by columns, another uses Envers, a third writes to a custom history table, a fourth just logs changes. Then an auditor asks for every change to customer data in March across all systems, and nobody can answer without weeks of work. Interviewers ask this design question at a senior level to see whether we can design a standard that fits different services, covers the gaps, and is easy enough that teams actually adopt it. In this answer, we will separate the two auditing needs, choose one history mechanism, define a standard audit record, make the store trustworthy, package the shared parts as a starter, and plan the migration from today's mix.
1Separating the Two Needs
Much of the inconsistency comes from mixing two different needs. The first is last-change metadata on the row: created_at, created_by, updated_at, updated_by. It is cheap, useful in every screen and support case, and Spring Data auditing does it well with @CreatedDate, @LastModifiedBy and an AuditorAware bean. Every service should have it, implemented the same way.
The second is full change history: every change, with the values before and after, kept for years. That is a different problem with different costs, and it should not be solved per service. The standard states both rules separately: last-change columns in every service using the shared starter, and history through one central mechanism for any data classified as needing it.
- Shared auditing starter
AuditorAware, security context, correlation id - Last-change columns in every serviceSpring Data auditing on each row
- One history mechanismCDC from the database log
- Central audit storeStandard record, append-only, retention
- Audit queries and reportsBy entity, actor, time, correlation id
5 layers, from the top down. Step through them one at a time.
Each service uses the top two layers; the platform runs the bottom three.
2Choosing One History Mechanism
For history, the mechanism must see every write path: application updates, bulk JPQL, native SQL, batch jobs and manual fixes. Envers sees only Hibernate's entity lifecycle and misses the rest; custom history tables written by application code miss whatever the developer forgot; logs are not structured or reliable enough to be a record.
Change data capture (CDC) reads the database's own change log, the WAL in PostgreSQL or the binlog in MySQL, and publishes each committed change with before and after values. Debezium is the common tool, publishing to Kafka. Because it reads the log, it sees every write regardless of source, and it works the same way for services written in any language. A platform team runs the connectors and a consumer that writes events into a central audit store. That makes CDC a good organisation-wide choice: one mechanism, one team operating it, every service covered.
Two things must be designed with it. The log does not know the end user, so each write must carry the actor in the same transaction, which the starter below arranges. And not every table needs history; a data classification decides which tables are captured, which keeps volume and cost in check.
3Standardising the Audit Record
Whatever produces them, audit records must have the same shape, or the central store is just a pile of formats. The standard record has: the actor, a user id or a service identity such as svc:billing-job; the time of the change; the operation, insert, update or delete; the entity type and id; the values before and after, or the changed fields; the source service; and a correlation id that links the change to the request or job that caused it, so one user action that touched three services can be traced across all of them.
The CDC consumer builds this record from the change event plus the actor and correlation id recorded in the same transaction. Version the schema of the record itself, so it can evolve without breaking audit queries over seven years of data.
4Making the Audit Store Trustworthy
Audit data is only evidence if nobody can quietly change it. The audit store is append-only: the consumer can insert, nobody can update or delete, enforced by database permissions or by storage with write-once retention locks. It lives in a separate account or schema from the services, with separate credentials, so a compromised service cannot rewrite its own history. For stronger guarantees, each record can carry a hash of the previous record, so any change breaks the chain and is detectable.
Retention is defined per data class, for example seven years for customer and financial records and one year for operational data, with older data moved to cheaper storage and deleted automatically at the end. Where privacy law requires erasing a person's data, the design must say how: for example, storing personal values encrypted with a per-person key and destroying the key on erasure, so the audit structure remains while the personal data becomes unreadable.
5A Shared Starter for the Service Side
Teams adopt standards that are easier than doing it themselves. A Spring Boot starter, a library with auto-configuration, provides the service side in one dependency: it enables JPA auditing, registers a standard AuditorAware that reads the user from Spring Security's SecurityContext and falls back to the service identity for jobs and consumers, propagates the correlation id from request headers or message headers, and writes the actor and correlation id into the transaction so CDC can pick them up.
- Inventory current auditingPer service: mechanism, gaps, retention
- Classify data needing historyCustomer, financial, access rights
- Turn on CDC for those tablesPlatform team operates connectors
- Adopt the starter in each serviceActor and correlation id on every write
- Run old and new side by sideCompare records for a period
- Retire custom mechanismsKeep or migrate old history per retention
6 steps in order. Play them, or step through them one at a time.
With the starter, a team's work is to add one dependency, extend the shared Auditable base class and mark which tables need history. Everything else is consistent by construction.
6Follow-up questions
What about services that do not use a relational database?
CDC exists for several databases, including MongoDB, but some stores have no usable change log. For those, the service publishes audit events in the standard record format through an outbox, a table or collection written in the same transaction and relayed reliably, so the central store still receives one format.
Why not let teams keep Envers if it works for them?
Because it misses bulk and native writes, and because the auditor's question crosses services. Teams may keep Envers for user-facing history screens if they like, but the audit of record is the central store, so coverage and format are the same everywhere.
Who owns the audit platform?
A platform or data team owns CDC, the consumer and the store; security or compliance owns the retention and access policy; each service team owns recording the actor correctly and classifying its tables. Writing that ownership down is part of the standard.
7Conclusion
In this answer, we saw that a consistent approach starts by separating last-change columns, which every service implements with Spring Data auditing, from full history, which uses one organisation-wide mechanism such as CDC into a central store so every write path is covered. The audit record has a standard shape with actor, time, operation, before and after values and a correlation id; the store is append-only with defined retention; and a shared starter makes the service side consistent by default.
Open with
I would start by separating two needs that are usually mixed up. One is who last changed a record, which is just created and updated columns on the row; Spring Data auditing does that well, and every service should have it, implemented the same way.
Then, if they want more
The other is full change history, which should not be solved per service at all.
For history I would choose one mechanism for the organisation, and my preference is change data capture, like Debezium reading the database log into a central audit store, because it sees every write path, including bulk SQL, scripts and manual fixes, which Envers or hand-written history tables miss.
Then I would standardise the audit record: the actor, as a user or a service identity, the time, the operation, the entity and id, before and after values, the source service and a correlation id that links changes across services to the request that caused them.
The store must be trustworthy: append-only, separate credentials, no update or delete rights, possibly hash chaining, and retention defined per data class, with a plan for privacy erasure.
To make adoption easy, I would ship a Spring Boot starter that enables JPA auditing with a shared AuditorAware, reading the user from the SecurityContext and falling back to the service name for jobs, and that propagates the correlation id and records the actor in the same transaction so CDC can pick it up.
Finally, I would migrate gradually: classify which tables need history, run old and new side by side for a while, then retire the custom mechanisms.
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.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "Several services each implement auditing differently (Spring Data annotations, Envers...": https://scrivio-interview-library.netlify.app/databases/spring-data-jpa/640a003da6/