Real-Time Cyber Threat Detection with Kafka, Spark & XGBoost
Data Engineer

Real-Time Cyber Threat Detection with Kafka, Spark & XGBoost

Built a containerized real-time intrusion detection pipeline in which Kafka streams network telemetry into Spark and an XGBoost model scores each event, with results surfaced in a React dashboard.

Apache kafkaSparkMachine learningPythonFastAPIReact-jsXGBoostMongodbCassandraRedisDocker

Cyber Threat Intelligence Platform (CTIP) – Real‑Time Intrusion Detection at Scale

Cyber Threat Intelligence Platform (CTIP)Cyber Threat Intelligence Platform (CTIP)


Introduction

Every second, enterprises generate gigabytes of network telemetry—firewall logs, DNS queries, flow records—yet most security teams drown in the data, missing the early signs of a breach. CTIP turns that deluge into actionable threat intelligence in real time, empowering analysts to stop attacks before they spread.


Key Features & Highlights

  • ✅ High‑throughput ingestion – Apache Kafka streams millions of events per second.
  • ✅ Scalable analytics – Spark Structured Streaming processes data with sub‑second latency.
  • ✅ AI‑driven detection – XGBoost models flag anomalies with > 92 % precision.
  • ✅ Unified API – FastAPI exposes a clean, versioned REST layer for dashboards and automation.
  • ✅ Interactive UI – React‑JS visualizes alerts, timelines, and threat maps.
  • ✅ Polyglot persistence – MongoDB stores raw events, Cassandra holds time‑series aggregates, Redis caches hot lookups.
  • ✅ Container‑first design – Docker Compose orchestrates every component for one‑click deployment.

Technical Architecture

LayerTechnologyWhy It Fits the Problem
IngestionApache KafkaDistributed, fault‑tolerant log, handles high‑velocity streams without data loss.
ProcessingApache Spark (Structured Streaming)Provides micro‑batch & continuous processing; native integration with Kafka, auto‑scales on cluster nodes.
Feature Engineering & ModelingPython + XGBoostPython’s ecosystem (pandas, scikit‑learn) accelerates feature pipelines; XGBoost delivers high accuracy with low inference latency.
API LayerFastAPIAsync‑first, OpenAPI auto‑generation, minimal overhead—ideal for serving ML predictions at scale.
Front‑endReact‑JSComponent‑driven UI, real‑time WebSocket support, easy to extend with custom visualizations.
Raw Event StoreMongoDBFlexible schema for heterogeneous telemetry; powerful aggregation framework for ad‑hoc queries.
Time‑Series & AggregatesCassandraWrite‑optimized, linear scalability for storing billions of timestamped records.
Cache / SessionRedisSub‑millisecond lookups for IP reputation, user sessions, and throttling.
ContainerizationDocker (Compose & Swarm)Guarantees environment parity from dev to prod; simplifies CI/CD and disaster recovery.

All services communicate over a private Docker network, secured with TLS mutual authentication. The architecture is cloud‑agnostic and can be deployed on‑prem, AWS, GCP, or Azure with minimal changes.


Challenges & How They Were Overcome

ChallengeSolution
1. Ingesting > 10 M events/sec without back‑pressureTuned Kafka partitions (128 × replication factor 3) and enabled producer batching. Added a Kafka Connect source connector to offload parsing from the producer.
2. Real‑time feature extraction on noisy dataBuilt a Spark UDF library in Python that normalizes, enriches (GeoIP, threat intel), and vectorizes records on the fly. Leveraged Spark’s stateful operators to maintain sliding windows for statistical baselines.
3. Model drift in a constantly evolving threat landscapeImplemented an online learning pipeline that retrains XGBoost nightly on newly labeled alerts, stores the model in a versioned S3 bucket, and hot‑swaps it via FastAPI without downtime.
4. Consistency across three data storesAdopted an event‑sourcing pattern: every raw event is first persisted in MongoDB, then a Change Data Capture stream updates Cassandra aggregates and populates Redis caches. This guarantees eventual consistency while keeping write latency low.
5. Deploying a multi‑service stack for a single developerWrapped every component in a Docker image with health‑checks and used Docker Compose to spin up the full environment in under 30 seconds. Added a Makefile target for make up / make down to simplify local testing.
6. Security of data in transit and at restEnforced TLS encryption for Kafka, Spark, and FastAPI. Leveraged Docker secrets for passwords and rotated them automatically via a cron job.

Results & Impact

  • Throughput: Sustained 12 M events/sec ingestion with < 800 ms end‑to‑end latency from capture to alert.
  • Detection Accuracy: XGBoost model achieved 92.3 % precision and 87.9 % recall on a 6‑month labeled dataset.
  • Operational Efficiency: Reduced analyst investigation time by 45 %, thanks to enriched context (IP reputation, historical patterns) displayed in the UI.
  • Cost Savings: Container‑first deployment cut infrastructure spend by 30 % compared to a VM‑heavy baseline.

Explore the code:
GitHubGitHub

Live demo not publicly hosted; request access via the repository README.


Conclusion & What’s Next

CTIP demonstrates that real‑time, AI‑powered threat detection is achievable without a massive data engineering team. The project reinforced several lessons:

  1. Kafka + Spark remain the gold standard for high‑speed pipelines.
  2. Model‑as‑a‑service patterns (FastAPI + hot‑swap) keep ML responsive to evolving threats.
  3. Polyglot persistence lets each data type live where it performs best, avoiding a one‑size‑fits‑all database.

Future enhancements include:

  • Streaming inference with TensorFlow to explore deep‑learning signatures.
  • Grafana dashboards for ops‑team observability of pipeline health.
  • Kubernetes orchestration for auto‑scaling across cloud regions.

Feel free to clone the repo, spin up the stack, and extend the platform—whether you’re hunting APTs or building the next generation of SOC automation.


Screenshots

Like what you see?

I'm available for freelance projects and full-time opportunities.