Live data from Hacker News

Ask HN: How do you implement audit trail for a product?

news.ycombinator.com

1–10 of 18 posts

Ask HN: How do you implement audit trail for a product?

#1
Say I want to log every change to the database, how does one go about implementing that when using open source DBs like Postgres or CouchDB? Are there open source, on-premise solutions available for this?

Also, what other factors need to be considered when implementing audit trail?

Re: Ask HN: How do you implement audit trail for a product?

#2
I think the best pattern for this is Change Data Capture (CDC). Checkout out something like Debezium.

In short, IIRC, it looks into the RDBMSs redo log, and turns it into a message stream. You can then stream this into some kind of messaging platform (Kakfa is typical) and then process the data any way you want. Log it, aggregate it, archive it, stick it in another database, or object store, etc...

Re: Ask HN: How do you implement audit trail for a product?

#3
Audit trails have several considerations. For example, you need to them to be immutable, ensure guaranteed delivery, and be able to support extended data retention (on the order of years).

Because audit trails record activity, I would generally recommend linking the audit trail to the API call site rather than the database, as you should include information such as the actor (who made the call/request) and context (e.g. what IP address and user agent the call came from). An additional good practice is to add the request and response of the API call being recorded. You should also redact any sensitive or PII fields.

If you're looking for a managed service that takes care of all of this, and delivering it to your customers for you, check out https://apptrail.com (Disclaimer: founder).

Re: Ask HN: How do you implement audit trail for a product?

#4
At my previous company, we implemented basic audit trails using postgres triggers that recorded the before & after state of a row on upsert / delete. The triggers had to be set individually for each of the tables (same trigger function worked for all), and an added convenience view to see the diff of the changes helped. This worked pretty well us, since we didn't need details of the actor / IP address etc.

Re: Ask HN: How do you implement audit trail for a product?

#6

INSERT only, no UPDATEs. Queries return most recent record. Periodically groom older entries.

Just wanted to x2 this, in several projects we end up with this setup and gets the work done.

If you want to get just a bit more fancy, add a hash that depends on previous entries, like some sort of proto-blockchain.

Re: Ask HN: How do you implement audit trail for a product?

#7
post #2

I think the best pattern for this is Change Data Capture (CDC). Checkout out something like Debezium. In short, IIRC, it looks into the RDBMSs redo log, and turns it into a message stream. You can then stream this into some kind of messaging platform (Kakfa is typical) and then process the data any way you want. Log it, aggregate it, archive it, stick it in another database, or object store, etc...

How would CDC include information like the user making the change?

Re: Ask HN: How do you implement audit trail for a product?

#9
post #7
post #2

I think the best pattern for this is Change Data Capture (CDC). Checkout out something like Debezium. In short, IIRC, it looks into the RDBMSs redo log, and turns it into a message stream. You can then stream this into some kind of messaging platform (Kakfa is typical) and then process the data any way you want. Log it, aggregate it, archive it, stick it in another database, or object store, etc...

How would CDC include information like the user making the change?

A CDC alone wouldn't. You would need to track and store that metadata in an additional database along with transaction ID, so it can be joined with the raw change stream to form a complete audit log.
Post reply on HN