Skip to main content
FA
Faiz Akram
HomeAboutExpertiseProjectsBlogContact
FA
Faiz Akram

Senior Technical Architect specializing in enterprise-grade solutions, cloud architecture, and modern development practices.

Quick Links

Privacy PolicyTerms of ServiceBlog

Connect

© 2026 Faiz Akram. All rights reserved.

Back to Blog
Real-Time Audit Logging in Full-Stack Apps: Patterns, Tools, and Secure Implementation
Full-Stack

Real-Time Audit Logging in Full-Stack Apps: Patterns, Tools, and Secure Implementation

F
Faiz Akram
August 29, 2026
9 min read

Modern full-stack applications face increasing scrutiny over security, compliance, and user activity accountability. Real-time audit logging is now a non-negotiable requirement for regulated industries and any product where trust and traceability matter. However, designing and implementing secure, scalable audit logging is far from trivial—especially with cloud-native, microservices, and serverless architectures.

What Is Real-Time Audit Logging and Why Does It Matter?

Audit logging refers to the systematic recording of all critical actions performed by users, services, or systems. These logs are central to security investigations, regulatory compliance (e.g., GDPR, HIPAA, SOX), and operational forensics. In many SaaS or enterprise platforms, the lack of reliable audit trails is a deal-breaker for customers.

A production-grade audit logging system must:

  • Capture who did what, when, and from where (user/service identity, action, timestamp, source, outcome)
  • Be tamper-resistant and immutable
  • Scale with application load
  • Support real-time querying and alerting
  • Integrate with existing observability and SIEM tools

Here’s a minimal example of an audit log entry in JSON format:

{
  "timestamp": "2024-06-05T13:45:23.123Z",
  "userId": "user-42",
  "action": "UPDATE_ACCOUNT_EMAIL",
  "resource": "account:1835",
  "status": "SUCCESS",
  "ip": "203.0.113.25",
  "meta": {
    "oldEmail": "john@example.com",
    "newEmail": "john.doe@example.com"
  }
}

Key insight: Audit logging is not just logging—it's a structured, secure, and queryable record system that can make or break your compliance posture.

How to Design a Secure, Scalable Audit Logging Architecture

1. Define What to Audit and the Minimum Schema

Start with a data taxonomy exercise: explicitly define which actions must be logged, which context fields are mandatory, and what constitutes sensitive data. In my experience, a good starting schema includes:

  • actor (user/service ID)
  • action (enum string)
  • target resource (object or resource ID)
  • timestamp (ISO-8601 UTC)
  • origin (IP address, geo, device info)
  • status/outcome (success/failure)
  • metadata (optional context)

Example config for a NestJS app using TypeScript interfaces:

export interface AuditLogEntry {
  timestamp: string; // ISO-8601
  actor: string; // user or service ID
  action: string; // e.g., 'DELETE_USER'
  resource: string; // e.g., 'user:1234'
  status: 'SUCCESS' | 'FAILURE';
  origin?: string; // IP/device
  meta?: Record<string, any>;
}

Key insight: A consistent, minimal schema makes querying and compliance reporting far easier downstream.

2. Choose an Immutable, Queryable Storage Backend

Audit logs must be immutable and support fast queries. In production, I recommend:

  • Elasticsearch 8.x for real-time search and Kibana dashboards
  • Amazon OpenSearch (managed ES) or Google Cloud Logging for managed options
  • PostgreSQL (with append-only tables) for regulated environments needing transactional guarantees
  • Cloud-native object storage (S3, GCS) with versioning for cold, immutable archives

Example: Creating an append-only audit log table in PostgreSQL 15+

CREATE TABLE audit_logs (
  id BIGSERIAL PRIMARY KEY,
  timestamp TIMESTAMPTZ NOT NULL,
  actor TEXT NOT NULL,
  action TEXT NOT NULL,
  resource TEXT NOT NULL,
  status TEXT NOT NULL,
  origin TEXT,
  meta JSONB,
  CONSTRAINT immutable_update CHECK (false) -- disables UPDATE
);
-- Disable delete and update
REVOKE UPDATE, DELETE ON audit_logs FROM PUBLIC;

Key insight: Always use append-only, immutable storage for audit logs—never allow UPDATE or DELETE on log entries.

3. Implement Real-Time Log Ingestion and Delivery

To avoid bottlenecks and data loss, decouple log generation from storage using:

  • Message brokers (Apache Kafka 3+, AWS Kinesis, Google Pub/Sub)
  • Structured log shippers (Fluent Bit v2, Logstash, Filebeat)
  • Async logging middleware in your backend (e.g., NestJS Interceptor, Express middleware)

Example: NestJS interceptor (audit.interceptor.ts) for async log publishing to Kafka

@Injectable()
export class AuditInterceptor implements NestInterceptor {
  constructor(private readonly kafka: KafkaProducer) {}

  async intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<any>> {
    const now = Date.now();
    const req = context.switchToHttp().getRequest();
    return next.handle().pipe(
      tap(async (result) => {
        const entry: AuditLogEntry = {
          timestamp: new Date().toISOString(),
          actor: req.user?.id ?? 'anonymous',
          action: req.route.path,
          resource: req.params?.id || '',
          status: 'SUCCESS',
          origin: req.ip,
          meta: { ...result }
        };
        await this.kafka.send({
          topic: 'audit-logs',
          messages: [{ value: JSON.stringify(entry) }]
        });
      })
    );
  }
}

Key insight: Decoupling log capture from persistence ensures you never block user requests or lose logs under load.

4. Secure Logs at Rest and In Transit

Audit logs are prime targets for attackers. Implement:

  • TLS 1.2+ everywhere (Kafka, database connections, log shippers)
  • Encryption at rest (cloud KMS, PostgreSQL TDE, Elasticsearch encryption)
  • Strict IAM roles (limit who/what can write or read audit logs)
  • Log integrity checks (hash chains, Merkle trees, AWS CloudTrail validation)

Example: Enabling encryption in Elasticsearch 8.x

xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.http.ssl.enabled: true
xpack.security.http.ssl.keystore.path: certs/http.p12

And for AWS OpenSearch, ensure node-to-node encryption and encryption at rest are enabled in the console.

Key insight: Protecting audit logs with strong encryption and access controls is mandatory for compliance and trust.

5. Expose and Query Audit Logs for Security and Compliance

Audit logs are only valuable if they can be searched or exported for compliance. Build:

  • Role-based dashboards (Kibana, Grafana, OpenSearch Dashboards)
  • SIEM integrations (Splunk, Sumo Logic, Azure Sentinel)
  • Export APIs (filtered, paginated endpoints for compliance teams)

Example: Elasticsearch query to find failed deletions in the last 7 days

{
  "query": {
    "bool": {
      "must": [
        { "term": { "action": "DELETE_USER" } },
        { "term": { "status": "FAILURE" } },
        { "range": { "timestamp": { "gte": "now-7d/d" } } }
      ]
    }
  }
}

Key insight: Fast, role-based queries are essential for both security ops and regulatory audits.

Tooling Options for Real-Time Audit Logging: Comparison Table

Tool/ServiceImmutabilityReal-Time QueryManaged OptionCostEcosystemNotes
Elasticsearch (8.x)OptionalYesElastic Cloud$$Kibana, BeatsHigh scale, fast search, runs anywhere
Amazon OpenSearchOptionalYesYes$$AWS nativeManaged, integrates w/ AWS SIEM
PostgreSQL (append-only)YesYes (limited)RDS, Cloud SQL$SQL, integrationsBest for strict RDBMS environments
AWS CloudTrail/LakeYesBatchYes$$AWS SIEM/BQBuilt-in for AWS API, slower
Google Cloud LoggingYesYesYes$GCP nativeEasy integration, IAM-backed
Kafka + S3 (object archive)YesNo (batch)MSK + S3$Flink, SparkLowest cost, for archive only
SplunkYesYesYes$$$SIEMBest for large-scale enterprise SIEM

Key insight: Choose your storage based on query/alert needs, compliance strictness, and cloud platform alignment.

How to Build a Production-Ready Audit Logging Pipeline: Step-by-Step

Step 1: Instrument the Application Layer for Audit Events

Audit logging starts at the point of action. In modern full-stack apps (Node.js, Python, Java), use middleware/interceptors to capture relevant events.

  1. Define a central audit logger service/class that all controllers/services call.
  2. Use framework hooks (e.g., NestJS interceptors, Django middleware, Spring AOP) to automatically log defined actions.
  3. For user actions, always include authenticated user context. For service/service calls, use service principals or JWT claims.
  4. Make logging asynchronous to avoid blocking user-facing requests.
  5. Use structured log formats (JSON) with enforced schema validation (e.g., Zod, Joi) to prevent schema drift.

Key insight: Centralized, framework-native instrumentation ensures coverage and consistency.

Step 2: Decouple Log Ingestion with a Message Broker

Avoid writing directly from your app to the log database; instead, publish audit events to a durable queue or stream.

  1. Choose a broker: Kafka (best for scale, 0.11+), AWS Kinesis (if on AWS), or Google Pub/Sub.
  2. Use a library/SDK with at-least-once delivery (e.g., node-rdkafka, kafkajs, aws-sdk).
  3. Set up a topic/stream dedicated to audit logs, with strict access controls.
  4. Retain messages for at least 7-30 days to allow for replay and recovery.
  5. Use message keys (e.g., user ID or resource ID) to enable partitioning and parallel processing downstream.

Example: KafkaJS producer config with TLS/SASL

const kafka = new Kafka({
  brokers: ["broker1:9093", "broker2:9093"],
  ssl: true,
  sasl: {
    mechanism: 'plain',
    username: process.env.KAFKA_USER,
    password: process.env.KAFKA_PASS,
  },
});

Key insight: A broker-based approach guarantees durability, resilience, and scalability under high load.

Step 3: Persist Logs to an Immutable, Searchable Store

Now, consume audit events from the broker and write them to your chosen storage.

  1. Deploy a consumer service (e.g., Kafka Connect, custom Node.js worker, Logstash pipeline).
  2. Validate and transform events to match your target schema (flatten nested fields, drop PII as needed).
  3. Write to Elasticsearch using the official client (elasticsearch@8.x) or compatible Logstash output.
  4. Ensure write-only access for the consumer and disable deletes/updates at the storage level.
  5. Regularly snapshot or replicate data for disaster recovery and legal hold.

Example: Logstash pipeline to ingest Kafka audit logs to Elasticsearch

input {
  kafka {
    bootstrap_servers => "broker1:9093,broker2:9093"
    topics => ["audit-logs"]
    security_protocol => "SSL"
    ssl_keystore_location => "/etc/logstash/keystore.jks"
    ssl_keystore_password => "${KEYSTORE_PASS}"
    codec => "json"
  }
}
output {
  elasticsearch {
    hosts => ["https://es-prod:9200"]
    index => "audit-logs-%{+YYYY.MM.dd}"
    user => "audit_writer"
    password => "${ES_PASS}"
    ssl => true
  }
}

Key insight: Dedicated consumers/processors decouple ingestion from storage, enabling scale and resilience.

Step 4: Enable Secure, Real-Time Querying and Alerting

Once logs are indexed, make them available (read-only) to security and compliance teams.

  1. Deploy Kibana or OpenSearch Dashboards with SSO (SAML/OIDC) and role-based access.
  2. Create pre-built queries and visualizations for top compliance scenarios (e.g., all failed login attempts, privileged data exports).
  3. Use alerting engines (ElastAlert, Watcher, AWS CloudWatch) to trigger on suspicious patterns in near real-time.
  4. Expose a read-only API (e.g., GraphQL or REST) for internal tools to fetch and export audit logs securely.
  5. Set retention policies: hot data (live search) for 30-90 days, cold archive (S3/Glacier) for 7+ years as required.

Key insight: Real-time dashboards and alerts enable proactive threat detection—not just reactive forensics.

Frequently Asked Questions

Q: What is the difference between application logs and audit logs? A: Application logs track internal events, errors, and performance metrics, while audit logs are structured records of sensitive actions for compliance and traceability. Audit logs prioritize immutability, security, and user attribution—application logs do not.

Q: How long should I retain audit logs for compliance? A: Most industry standards (e.g., PCI DSS, HIPAA, SOX) require retaining audit logs for 1-7 years. I recommend 90 days hot (searchable) and 7+ years cold (archive), but always check your specific regulatory requirements.

Q: Can I use a NoSQL database like MongoDB for audit logs? A: While technically possible, MongoDB lacks native immutability and is harder to lock down for compliance. I recommend Elasticsearch or append-only RDBMS tables for audit log use cases.

Key Takeaways

  • Immediately define a minimal, consistent schema for audit events across all services.
  • Use message brokers (Kafka, Kinesis) to decouple log collection from storage and support scale.
  • Store audit logs in immutable, queryable backends (Elasticsearch, OpenSearch, append-only RDBMS).
  • Enforce encryption, strict IAM, and integrity checks at every stage—logs are a top target for attackers.
  • Expose real-time dashboards and alerts to security/compliance teams, with role-based access.
  • Regularly review log retention policies and automate archival to meet compliance and cost goals.

Tags

full-stackaudit loggingcompliancecloudnestjselasticsearch

Share this article

Found it helpful? Share it with your network.

X / TwitterLinkedInFacebookWhatsApp

Related Articles

More on Full-Stack and related topics

Building a Production-Ready Serverless Full-Stack App with Next.js, AWS Lambda, and DynamoDB
Full-Stack
August 21, 2026
6 min read

Building a Production-Ready Serverless Full-Stack App with Next.js, AWS Lambda, and DynamoDB

Learn how to architect, configure, and deploy a full-stack, serverless application using Next.js, AWS Lambda, and DynamoDB for scalable production workloads.

full-stacknext.jsaws lambda
Read More
Building Real-Time Collaborative Applications with CRDTs and WebSockets
Full-Stack
August 13, 2026
7 min read

Building Real-Time Collaborative Applications with CRDTs and WebSockets

Learn how to build production-grade, real-time collaborative apps in 2024 using CRDTs, WebSockets, and modern full-stack frameworks. Step-by-step guide and tool comparisons inside.

real-timefull-stackCRDT
Read More
Full-Stack Observability: Modern Patterns, Tools, and Real-World Setups
Full-Stack
August 5, 2026
5 min read

Full-Stack Observability: Modern Patterns, Tools, and Real-World Setups

Master full-stack observability in 2024: end-to-end tracing, metrics, and logs for cloud-native apps. Key tools, best patterns, and step-by-step production configs.

cloudfull-stackobservability
Read More