Audit logs

Observability and operations Mid-level Python 3.13+ PostgreSQL 17+ Reviewed By Best Latest Apps

Pick the fix

An admin changes a user's role. Where should the audit event be written?

Options
Hint 1 of 1

What if the request fails after the change?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A In the same database transaction as the role change, to an append-only table

Correct. Both happen or neither does.

An audit record must exist if and only if the change happened. Writing it in the same transaction makes that true, and an append-only table means nobody can quietly edit history.

Why not the others?
  • B To stdout after the request finishes. Lost if the process dies; easy to drop.
  • C Only in the browser console. Not on the server at all.
  • D Nowhere; the role table shows the current value. No history of who changed it.

30-second answer

Application logs help engineers debug and can be sampled or dropped. Audit logs are a record of who did what to which resource and when, kept for security, compliance and customer trust, so they must be complete, tamper-evident and retained for a defined period. Write an event for every security-relevant action (logins, permission changes, data exports, admin actions) with actor, action, target, time, source IP and result, to append-only storage.

Practise saying it · 25 s. No recording, just a timer

Build audit logs.

Say your answer out loud, then check which of these points you covered:

  • Who, what, which, when
  • Complete and atomic
  • Append-only, tamper-evident
  • Retention and access

Deep dive

Write the audit event in the same transaction as the change, or through an outbox, so neither is lost.

Hash chaining or write-once storage makes tampering detectable.

Let customers export their own audit logs in B2B products.

Restrict who can read audit logs, and audit those reads.

Common mistakes

  • Using debug logs as the audit trail.
  • Audit events that can be lost when a request fails halfway.
  • Admins able to delete audit entries.

Senior take & follow-ups

Contrast purpose, then fields, atomic writes, tamper evidence and access.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you avoid losing audit events?

    Show hint

    Same transaction as the change, or the outbox pattern.

  2. How do you detect tampering?

    Show hint

    Hash chains or write-once object storage.

Suggest a fix if something here is wrong or out of date.