1.9 KiB
1.9 KiB
ADR-0020: Audit Logging Storage
Status
Accepted
Context
The platform needs to audit all security-relevant actions:
- User logins and authentication attempts
- Permission changes
- Data modifications
- Administrative actions
Audit logs must be:
- Immutable (append-only)
- Queryable
- Performant (don't slow down operations)
- Compliant with audit requirements
Storage options considered:
- PostgreSQL table - Simple, queryable, transactional
- Elasticsearch - Excellent for searching, but additional dependency
- File-based logs - Simple but hard to query
- External audit service - Overkill for initial version
Decision
Store audit logs in PostgreSQL append-only table with JSON metadata:
- Table structure:
audit_logswith columns:id,actor_id,action,target_id,metadata(JSONB),timestamp
- Append-only: No UPDATE or DELETE operations
- JSON metadata: Flexible storage for additional context
- Indexing: Index on
actor_id,action,timestampfor queries
Rationale:
- Simple (no additional infrastructure)
- Queryable via SQL
- Transactional (consistent with other data)
- JSONB provides flexibility for metadata
- Can migrate to Elasticsearch later if needed
- Good performance for typical audit volumes
Consequences
Positive
- Simple implementation
- Queryable via SQL
- No additional infrastructure
- Transactional consistency
- Can archive old logs if needed
Negative
- Adds load to primary database
- May need archiving strategy for large volumes
- Less powerful search than Elasticsearch
Implementation Notes
- Create
audit_logstable via Ent schema - Use JSONB for metadata column (PostgreSQL-specific)
- Add indexes:
(actor_id, timestamp),(action, timestamp) - Implement async logging (optional, via channel) for high throughput
- Consider partitioning by date for large volumes
- Add retention policy (e.g., archive after 1 year)