Recommended resources
The freely available resources this lesson will direct you to.
- 14: Distributed Logging & Metrics Framework | Systems Design Interview Questions With Ex-Google SWEwww.youtube.com
- ✅ System Design 16: Design Distributed Logging System | Splunk | Logstash | HLD | LLDwww.youtube.com
- Automatic Audit Logs: new updates deliver increased transparency ...blog.cloudflare.com
- Real-Time Compliance & Audit Logging With Apache Kafka®www.confluent.io
Hello! Welcome back to our course on architecting security products.
In our previous lesson, we focused on the real-time availability of an identity system, designing token caching and session replication strategies to ensure it remains responsive and resilient. We've established how to manage a user's session while they are active. But what happens after the fact? How do we trace a user's actions for security, compliance, or debugging?
This brings us to today's crucial topic. Our learning outcome is to design a high-throughput audit logging pipeline for critical identity events to support security investigations.
We will architect an end-to-end system that captures every significant action within our identity platform—from logins and password changes to permission grants—and transforms this raw data into a secure, searchable, and invaluable resource for security teams. As a software engineer with a deep interest in first principles, you'll see this is a classic distributed data engineering problem applied to the high-stakes domain of security.
The Five Pillars of a Trustworthy Audit Pipeline
Before we start designing components, it's essential to establish the foundational requirements for an audit logging system, especially one handling sensitive identity events. These systems must meet stringent demands for compliance standards like SOC 2 or HIPAA.
Real-Time Compliance & Audit Logging With Apache Kafka®
This article from Confluent provides an excellent overview of the non-negotiable requirements for a compliance-ready audit pipeline. Understanding these principles will guide all of our subsequent design decisions.
Please read the section titled 'What Are the Requirements for Building a Real-Time Compliance Pipeline?'. Pay close attention to the five key requirements listed.
As the article outlines, our pipeline must be built on five pillars:
- Data Accuracy and Immutability: The log is the source of truth. It cannot be altered.
- Guaranteed Long-Term Retention: Data must be stored securely for years.
- Ironclad Security: Data must be encrypted and accessible only to authorized personnel.
- Real-Time Processing: Insights and alerts must be generated with low latency.
- Clear Auditability and Data Lineage: We must be able to prove the data's origin and transformations.
With these principles in mind, let's look at the overall architecture of such a pipeline.

1. The Ingestion Layer: Capturing Events at Scale
The first challenge is to capture events from our various identity microservices (authentication, user management, etc.) without impacting their performance and without losing a single event, even under massive load.
The most robust solution is to decouple the event producers (our microservices) from the consumers (the rest of the logging pipeline) using a distributed message broker. Apache Kafka is the industry standard for this task due to its scalability, durability, and fault tolerance.
Events are published to Kafka topics and consumed by the next stage of the pipeline. This "fire-and-forget" model allows the identity services to offload events quickly and reliably.
But how do we get the events into Kafka in the first place? And how do we handle the sensitive nature of identity events, like passwords or tokens? Let's look at a real-world example from Cloudflare.
Automatic Audit Logs: new updates deliver increased transparency ...
Cloudflare's blog post on their Automatic Audit Logs provides a masterclass in modern, non-intrusive event capture and sanitization. It details how they architected their pipeline to automatically generate logs from API calls.
Please read the sections 'Which APIs are audit logged?', 'Streaming HTTP requests', and 'Redacting sensitive information'. Focus on how they use their API Gateway and OpenAPI schema to automatically capture and redact events before they even enter the main pipeline.
Cloudflare's architecture reveals a key insight: the best place to capture and sanitize an event is as close to the source as possible. Their approach involves:
- Interception at the API Gateway: All user actions go through the API Gateway.
- Forwarding to a Redactor: The gateway streams the request/response to a dedicated service (a Cloudflare Worker).
- Schema-Driven Redaction: This service uses the product's OpenAPI schema to identify and remove sensitive fields (e.g., passwords, API keys), keeping only fields explicitly marked as
auditable. - Publishing to the Pipeline: The sanitized, structured event is then published to the data pipeline (which would be our Kafka layer).
This is a powerful pattern because it centralizes the logging logic and ensures that no sensitive data is accidentally leaked into the audit trail.
2. The Processing Layer: Adding Context and Structure
Once raw events are in Kafka, they are often inconsistent and lack the context needed for a security investigation. The processing layer's job is to transform this raw stream into a high-value, structured data stream. For this, we use a stream processing engine like Apache Flink or Spark Streaming.
✅ System Design 16: Design Distributed Logging System | Splunk | Logstash | HLD | LLD
To understand how to build this processing layer, let's watch a video that details the design of a general-purpose distributed logging system. It clearly explains the role of Kafka as a buffer and Flink as the processing engine.
Please watch the segment from 30:09 to 35:48. This part of the video explains why a simple ingestion service is insufficient and introduces Kafka and Apache Flink to handle the processing load. Note the list of responsibilities given to Flink.
As the video explains, the stream processor is responsible for several critical tasks:
- Normalization: Transforming logs from various sources into a single, consistent schema. Using a format like Apache Avro with a Schema Registry enforces this contract across the pipeline.
- Enrichment: This is where we add critical context to make logs useful. For an identity event, we might:
- Join a
user_idwith a user database to add the user's email, name, and role. - Take the
source_ipand enrich it with geolocation data. - Cross-reference an IP address or user ID against a threat intelligence feed to flag known malicious actors.
- Join a
- Validation & Filtering: Ensuring events are well-formed and dropping noisy, irrelevant logs.
The output of this layer is a clean, enriched, and structured stream of audit events, ready for storage and analysis.
3. The Storage Layer: Balancing Speed and Cost
Our pipeline needs to store data for potentially years, but security analysts need to query the most recent data in milliseconds. A single database cannot efficiently serve both needs. The solution is a tiered storage strategy.
Let's return to the "Distributed Logging System" video, which explains this concept perfectly.
✅ System Design 16: Design Distributed Logging System | Splunk | Logstash | HLD | LLD
This next segment of the video breaks down the storage architecture, introducing different databases for different purposes, which is the core of our tiered strategy.
Please watch from 35:48 to 44:37. Focus on the justification for using three different storage systems: Elasticsearch for recent data, Cassandra for medium-term storage, and S3 for long-term cold storage. Also, note the use of TTLs and cron jobs for data lifecycle management.
Based on this model, we can design our storage layer with three tiers:
- Hot Tier (Fast Search): For the most recent data (e.g., 30 days) that needs to be searched interactively. Elasticsearch is the ideal choice here. Its inverted index allows for lightning-fast full-text searches, which is exactly what an analyst needs when investigating an active incident.
- Warm Tier (Analytical Queries): For historical data that is queried less frequently but still needs to be accessible for reporting or trend analysis (e.g., 30 days to 18 months).
- Option 1 (Write-Optimized): A database like Apache Cassandra, as suggested in the video, is excellent for handling the high write throughput from the processing layer.
- Option 2 (Analytics-Optimized): Storing data in a column-oriented format like Apache Parquet in an object store (e.g., Amazon S3). This is highly efficient for analytical queries that only touch a subset of columns, a common pattern in security analytics.
- Cold Tier (Archival): For long-term retention (e.g., 7+ years) to meet compliance mandates. This data is stored in the most cost-effective object storage, like Amazon S3 Glacier. Crucially, this storage should be immutable (WORM - Write-Once, Read-Many) to guarantee the integrity of the audit trail, a key principle from our initial requirements.
The Flink processor writes the enriched logs to the Hot and Warm tiers simultaneously. Automated data lifecycle policies then manage moving data between tiers or deleting it based on age.
4. The Access Layer: Supporting Security Investigations
Finally, we need to make all this stored data useful for security teams. The access layer provides the tools and interfaces for monitoring, alerting, and investigation.
Real-Time Compliance & Audit Logging With Apache Kafka®
The Confluent article we started with also provides a great model for the access layer and the governance that must be woven throughout.
Please revisit this article and read the sections 'The Access Layer' and 'End-to-End Governance'. This puts the finishing touches on our design by showing how different users interact with the data.
Our access layer will support several key use cases:
-
Real-Time Alerting: The Flink processor can do more than just transform data; it can also analyze it in real-time. We can define rules to detect suspicious patterns (e.g., "5 failed logins for one user in 1 minute"). When a pattern is matched, Flink pushes an event to a dedicated Kafka
alertstopic. A separate Alerting Service consumes from this topic and sends notifications via PagerDuty, Slack, or email. The "Distributed Logging System" video details this exact alerting architecture in its final section (44:37 onwards). -
Interactive Investigation (SIEM/Dashboards): Security analysts will use a tool like Kibana (part of the Elastic Stack) or a general-purpose SIEM to perform interactive searches. These tools will query the Hot Tier (Elasticsearch) to provide sub-second responses.
-
Forensic Analysis and Reporting: For deep, long-term analysis or generating compliance reports, analysts will use query engines like Presto or Spark SQL to run queries against the Warm Tier (e.g., Parquet files in S3).
This multi-faceted access layer ensures that the right data is available to the right tool at the right speed.
Conclusion
In this lesson, we have designed a complete, high-throughput audit logging pipeline specifically tailored for critical identity events. We leveraged your background in distributed systems to navigate the trade-offs at each layer, from ingestion to analysis.
Key Takeaways:
- Layered Architecture: A robust logging pipeline consists of distinct layers: Ingestion, Processing, Storage, and Access.
- Decoupling with Kafka: Using Kafka as a central message bus is crucial for handling high throughput and ensuring data durability.
- Real-Time Processing with Flink: A stream processor is essential for normalization, enrichment, and real-time pattern detection for alerts.
- Tiered Storage: A multi-tier storage strategy (e.g., Elasticsearch for hot, Parquet/S3 for warm, Glacier for cold) is the only way to balance performance, cost, and long-term retention requirements.
- Security First: For identity events, sanitizing and redacting sensitive data at the source using a schema-driven approach is a non-negotiable security control.
Preview of the Next Lesson:
We've successfully built a pipeline that collects, enriches, and stores a wealth of security data. But so far, its analysis relies on humans or simple, predefined rules. Our next lesson will take this a step further. We will tackle the learning outcome: Design an anomaly detection system that consumes identity audit logs to identify suspicious behavior like impossible travel. We will use the output of the pipeline we designed today as the input for a machine learning-based system that can proactively hunt for threats.