<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Patrick Onwujekwe</title>
    <description>The latest articles on DEV Community by Patrick Onwujekwe (@patrick_onwujekwe_5355395).</description>
    <link>https://dev.to/patrick_onwujekwe_5355395</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3901525%2F844fb7f8-df6b-4216-892e-26ac68832520.png</url>
      <title>DEV Community: Patrick Onwujekwe</title>
      <link>https://dev.to/patrick_onwujekwe_5355395</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/patrick_onwujekwe_5355395"/>
    <language>en</language>
    <item>
      <title>Building a Real‑Time Anomaly Detection Engine for Web Traffic</title>
      <dc:creator>Patrick Onwujekwe</dc:creator>
      <pubDate>Tue, 28 Apr 2026 21:38:08 +0000</pubDate>
      <link>https://dev.to/patrick_onwujekwe_5355395/building-a-real-time-anomaly-detection-engine-for-web-traffic-2im8</link>
      <guid>https://dev.to/patrick_onwujekwe_5355395/building-a-real-time-anomaly-detection-engine-for-web-traffic-2im8</guid>
      <description>&lt;p&gt;Introduction&lt;br&gt;
Modern web applications live on the open internet, which means they’re constantly exposed to unpredictable traffic — from normal users, automated bots, and sometimes attackers. One of the most common threats is a DDoS attack, where a flood of requests overwhelms your server and makes your application unusable.&lt;/p&gt;

&lt;p&gt;In this project, I built a real‑time anomaly detection engine that monitors incoming HTTP traffic, learns what “normal” looks like, and automatically reacts when something suspicious happens. It runs alongside a Nextcloud server and watches every request passing through Nginx.&lt;/p&gt;

&lt;p&gt;This post walks through how I built it — in simple, beginner‑friendly language — and explains the core ideas behind sliding windows, baselines, anomaly detection, and automated blocking.&lt;/p&gt;

&lt;p&gt;Why Build an Anomaly Detector?&lt;br&gt;
Imagine you’re running a cloud storage platform. Everything is fine until suddenly:&lt;/p&gt;

&lt;p&gt;One IP starts sending 500 requests per second&lt;/p&gt;

&lt;p&gt;Or thousands of IPs spike traffic at the same time&lt;/p&gt;

&lt;p&gt;Or a user triggers a burst of 4xx/5xx errors&lt;/p&gt;

&lt;p&gt;If you wait for humans to notice, it’s already too late.&lt;/p&gt;

&lt;p&gt;A good anomaly detector should:&lt;/p&gt;

&lt;p&gt;Watch traffic continuously&lt;/p&gt;

&lt;p&gt;Learn normal behavior automatically&lt;/p&gt;

&lt;p&gt;Detect unusual spikes&lt;/p&gt;

&lt;p&gt;Block malicious IPs&lt;/p&gt;

&lt;p&gt;Alert you when something is wrong&lt;/p&gt;

&lt;p&gt;Recover automatically when traffic returns to normal&lt;/p&gt;

&lt;p&gt;That’s exactly what this project does.&lt;/p&gt;

&lt;p&gt;How the System Works (High-Level)&lt;br&gt;
The system has three main components:&lt;/p&gt;

&lt;p&gt;Nginx — reverse proxy that writes JSON access logs&lt;/p&gt;

&lt;p&gt;Detector Daemon — Python service that analyzes traffic&lt;/p&gt;

&lt;p&gt;iptables — firewall used to block suspicious IPs&lt;/p&gt;

&lt;p&gt;Here’s the flow:&lt;/p&gt;

&lt;p&gt;A user makes a request&lt;/p&gt;

&lt;p&gt;Nginx logs it in JSON format&lt;/p&gt;

&lt;p&gt;The detector tails the log file in real time&lt;/p&gt;

&lt;p&gt;It updates sliding windows of recent traffic&lt;/p&gt;

&lt;p&gt;It compares traffic to a rolling baseline&lt;/p&gt;

&lt;p&gt;If something looks abnormal, it triggers an action&lt;/p&gt;

&lt;p&gt;The dashboard shows live metrics&lt;/p&gt;

&lt;p&gt;Everything runs inside Docker.&lt;/p&gt;

&lt;p&gt;Sliding Windows (Explained Simply)&lt;br&gt;
A sliding window is just a list of recent events that automatically removes old ones.&lt;/p&gt;

&lt;p&gt;Imagine a 60‑second window:&lt;/p&gt;

&lt;p&gt;Every time a request comes in, we add its timestamp&lt;/p&gt;

&lt;p&gt;Every second, we remove timestamps older than 60 seconds&lt;/p&gt;

&lt;p&gt;The number of timestamps left = requests per 60 seconds&lt;/p&gt;

&lt;p&gt;This gives us:&lt;/p&gt;

&lt;p&gt;Per‑IP request rate&lt;/p&gt;

&lt;p&gt;Global request rate&lt;/p&gt;

&lt;p&gt;Per‑IP error rate&lt;/p&gt;

&lt;p&gt;Global error rate&lt;/p&gt;

&lt;p&gt;All in real time.&lt;/p&gt;

&lt;p&gt;Why sliding windows?&lt;br&gt;
Because they react instantly.&lt;br&gt;
If an attacker sends 200 requests in 2 seconds, the window sees it immediately.&lt;/p&gt;

&lt;p&gt;The Rolling Baseline (How the System Learns)&lt;br&gt;
A baseline is the system’s idea of “normal traffic.”&lt;/p&gt;

&lt;p&gt;Instead of hardcoding a threshold, we let the system learn from the last 30 minutes of traffic.&lt;/p&gt;

&lt;p&gt;Every second, we record:&lt;/p&gt;

&lt;p&gt;global requests per second&lt;/p&gt;

&lt;p&gt;global error rate&lt;/p&gt;

&lt;p&gt;Every 60 seconds, we compute:&lt;/p&gt;

&lt;p&gt;mean (average traffic)&lt;/p&gt;

&lt;p&gt;standard deviation (how much traffic usually varies)&lt;/p&gt;

&lt;p&gt;This gives us a dynamic baseline that adapts to:&lt;/p&gt;

&lt;p&gt;busy hours&lt;/p&gt;

&lt;p&gt;quiet hours&lt;/p&gt;

&lt;p&gt;natural fluctuations&lt;/p&gt;

&lt;p&gt;Why this matters&lt;br&gt;
If your site normally gets 5 requests per second, a spike to 50 is suspicious.&lt;br&gt;
But if your site normally gets 200 requests per second, 50 is nothing.&lt;/p&gt;

&lt;p&gt;The baseline makes the detector smart.&lt;/p&gt;

&lt;p&gt;How Anomalies Are Detected&lt;br&gt;
The detector uses two rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Z‑Score Rule
If:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Code&lt;br&gt;
(current_rate - baseline_mean) / baseline_stddev &amp;gt; 3&lt;br&gt;
…it’s considered abnormal.&lt;/p&gt;

&lt;p&gt;This is a common statistical rule:&lt;br&gt;
3 standard deviations above normal = anomaly.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Multiplier Rule
If:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Code&lt;br&gt;
current_rate &amp;gt; 5 × baseline_mean&lt;br&gt;
…it’s also an anomaly.&lt;/p&gt;

&lt;p&gt;This catches cases where the standard deviation is small.&lt;/p&gt;

&lt;p&gt;Error Surge Rule&lt;br&gt;
If an IP suddenly generates lots of 4xx/5xx errors:&lt;/p&gt;

&lt;p&gt;Code&lt;br&gt;
ip_error_rate &amp;gt; 3 × baseline_error_mean&lt;br&gt;
…the thresholds for that IP become stricter.&lt;/p&gt;

&lt;p&gt;This helps catch brute‑force login attempts or broken bots.&lt;/p&gt;

&lt;p&gt;Blocking Suspicious IPs&lt;br&gt;
When an IP is flagged:&lt;/p&gt;

&lt;p&gt;The detector inserts an iptables DROP rule&lt;/p&gt;

&lt;p&gt;The IP is immediately blocked&lt;/p&gt;

&lt;p&gt;An alert is written&lt;/p&gt;

&lt;p&gt;The ban is stored with a timer&lt;/p&gt;

&lt;p&gt;Unban Schedule&lt;br&gt;
The system automatically unbans IPs:&lt;/p&gt;

&lt;p&gt;After 10 minutes&lt;/p&gt;

&lt;p&gt;After 30 minutes&lt;/p&gt;

&lt;p&gt;After 2 hours&lt;/p&gt;

&lt;p&gt;After that → permanent ban&lt;/p&gt;

&lt;p&gt;This prevents accidental long‑term blocking of legitimate users.&lt;/p&gt;

&lt;p&gt;The Dashboard&lt;br&gt;
To make everything visible, I built a small FastAPI dashboard that shows:&lt;/p&gt;

&lt;p&gt;Global requests per second&lt;/p&gt;

&lt;p&gt;Error rates&lt;/p&gt;

&lt;p&gt;Baseline mean/stddev&lt;/p&gt;

&lt;p&gt;Top 10 IPs&lt;/p&gt;

&lt;p&gt;Banned IPs&lt;/p&gt;

&lt;p&gt;CPU and memory usage&lt;/p&gt;

&lt;p&gt;System uptime&lt;/p&gt;

&lt;p&gt;It refreshes every 3 seconds and is hosted on a public domain.&lt;/p&gt;

&lt;p&gt;Putting It All Together&lt;br&gt;
The final system:&lt;/p&gt;

&lt;p&gt;Watches traffic in real time&lt;/p&gt;

&lt;p&gt;Learns normal behavior&lt;/p&gt;

&lt;p&gt;Detects anomalies&lt;/p&gt;

&lt;p&gt;Blocks attackers&lt;/p&gt;

&lt;p&gt;Unbans automatically&lt;/p&gt;

&lt;p&gt;Logs everything&lt;/p&gt;

&lt;p&gt;Shows live metrics&lt;/p&gt;

&lt;p&gt;It’s a complete DevSecOps pipeline for traffic monitoring and automated defense.&lt;/p&gt;

&lt;p&gt;What I Learned&lt;br&gt;
This project taught me:&lt;/p&gt;

&lt;p&gt;How to design real‑time systems&lt;/p&gt;

&lt;p&gt;How to use sliding windows efficiently&lt;/p&gt;

&lt;p&gt;How to build statistical baselines&lt;/p&gt;

&lt;p&gt;How to detect anomalies without ML libraries&lt;/p&gt;

&lt;p&gt;How to automate firewall rules&lt;/p&gt;

&lt;p&gt;How to build dashboards for observability&lt;/p&gt;

&lt;p&gt;How to structure a production‑grade Python daemon&lt;/p&gt;

&lt;p&gt;It’s one of the most practical security tools I’ve ever built.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
Anomaly detection doesn’t have to be complicated.&lt;br&gt;
With the right architecture — sliding windows, baselines, and simple math — you can build a powerful real‑time defense system.&lt;/p&gt;

&lt;p&gt;This project was a great exercise in DevOps, security, and distributed systems thinking.&lt;/p&gt;

&lt;p&gt;If you’re learning DevSecOps, I highly recommend building something like this yourself.&lt;/p&gt;

&lt;p&gt;If you wait for humans to notice, it’s already too late.&lt;/p&gt;

&lt;p&gt;A good anomaly detector should:&lt;/p&gt;

</description>
      <category>monitoring</category>
      <category>security</category>
      <category>showdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
