<?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>Forem: Jasmin Tankić</title>
    <description>The latest articles on Forem by Jasmin Tankić (@jasmintankic).</description>
    <link>https://forem.com/jasmintankic</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%2F247757%2Fc38fadb0-0221-49e2-b16d-494f63bd232d.jpg</url>
      <title>Forem: Jasmin Tankić</title>
      <link>https://forem.com/jasmintankic</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jasmintankic"/>
    <language>en</language>
    <item>
      <title>Real-Time API Abuse Detection Engine</title>
      <dc:creator>Jasmin Tankić</dc:creator>
      <pubDate>Wed, 06 Aug 2025 23:21:54 +0000</pubDate>
      <link>https://forem.com/jasmintankic/real-time-api-abuse-detection-engine-2mij</link>
      <guid>https://forem.com/jasmintankic/real-time-api-abuse-detection-engine-2mij</guid>
      <description>&lt;p&gt;&lt;a href="https://dev.tourl"&gt;&lt;/a&gt;&lt;em&gt;This is a submission for the &lt;a href="https://dev.to/challenges/redis-2025-07-23"&gt;Redis AI Challenge&lt;/a&gt;: Beyond the Cache&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  🚧 What I Built
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;API Abuse Detection Engine&lt;/strong&gt; is designed to instantly detect, analyze, and mitigate cybersecurity threats. The solution handles various common API abuse patterns such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Brute-force attacks:&lt;/strong&gt; Quickly identifies repeated failed login attempts across username, IP, and username+IP scopes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enumeration attacks:&lt;/strong&gt; Spots attackers systematically probing endpoints by tracking distinct paths per IP per minute.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IP Recurrence / Rate-Limit Abuse:&lt;/strong&gt; Detects aggressive requesters using a Redis-backed token bucket.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Replay attack Detection:&lt;/strong&gt; Rejects re-submitted requests via idempotency/correlation keys with TTLs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The system only stores confirmed threats in Redis, using quick “minute buckets” and unique lists to detect them almost instantly. It can quickly group threats by IP and show the most recent examples for fast investigation.&lt;/p&gt;

&lt;p&gt;It also keeps fast-access “jars” of known malicious IPs, usernames, and fingerprints, so it can immediately escalate or block threats as soon as they appear.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgj6bz6x8h89894mrhjde.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgj6bz6x8h89894mrhjde.png" alt="Solution Diagram"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🚀 Demo
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;ANY&lt;/strong&gt; request to &lt;code&gt;/api/ingest/**&lt;/code&gt; (GET/POST/PUT/PATCH/DELETE/HEAD/OPTIONS/TRACE)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The engine extracts identity signals (username, client-ip, user-agent, correlation-id) from headers/query/body per config.&lt;/li&gt;
&lt;li&gt;Redis counters/sets/hashes are updated atomically; detectors return a verdict immediately.&lt;/li&gt;
&lt;li&gt;If threats are detected, the event is stored in Redis for analytics (body truncated &amp;amp; base64 if large; header allowlist applied).&lt;/li&gt;
&lt;li&gt;The response contains &lt;code&gt;threats&lt;/code&gt;, &lt;code&gt;recommendations&lt;/code&gt;, and &lt;code&gt;details&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;GET&lt;/strong&gt; &lt;code&gt;/api/dashboard/threats?from=2025-08-06T01:12:00Z&amp;amp;to=2025-08-06T01:20:00Z&amp;amp;samplesPerIp=3&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Returns totals and IP-grouped results for the window, with the newest samples per IP.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GET&lt;/strong&gt; &lt;code&gt;/api/dashboard/threats/{id}&lt;/code&gt; returns full stored event details.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;The engine also ships with an &lt;strong&gt;integrated, out-of-the-box dashboard&lt;/strong&gt; that visualizes all detected attacks in real time and lets you drill down into the full details of each threat event.&lt;/p&gt;

&lt;p&gt;GIT source code: &lt;a href="https://github.com/jasmintankic/api-guard" rel="noopener noreferrer"&gt;https://github.com/jasmintankic/api-guard&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj1jdcdz7k608un2rigxp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj1jdcdz7k608un2rigxp.png" alt="Threat Dashboard"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg7wy02ffx76kzasva0h4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg7wy02ffx76kzasva0h4.png" alt="Threat details"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faejcsp7eg41saxn2e1li.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faejcsp7eg41saxn2e1li.png" alt="API Response Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/sszKHey0PC0"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  🔰 How I Used Redis 8
&lt;/h2&gt;

&lt;p&gt;Redis 8 is the &lt;strong&gt;core&lt;/strong&gt; of both detection and analytics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Primary Real-Time Store:&lt;/strong&gt; Redis holds minute buckets, distinct sets, token buckets, and threat records. That gives me constant-time (O(1)) updates and lookups on the hot path.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Atomic Counters &amp;amp; TTL:&lt;/strong&gt; &lt;code&gt;INCR&lt;/code&gt; + &lt;code&gt;EXPIRE&lt;/code&gt; enable rolling-window analytics without background jobs. Buckets just expire away.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fast Distincts:&lt;/strong&gt; Using Sets/HyperLogLog, I track unique endpoints per IP per minute to surface enumeration behavior instantly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Replay Safety with SETNX:&lt;/strong&gt; Idempotency/correlation keys are stored with TTL so replays are rejected immediately.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Token Bucket in a Hash + CAS:&lt;/strong&gt; A per-principal token bucket lives in a Redis Hash (&lt;code&gt;c&lt;/code&gt; = credits, &lt;code&gt;ts&lt;/code&gt; = last update). I apply &lt;strong&gt;WATCH/MULTI/EXEC&lt;/strong&gt; for optimistic concurrency so limits are precise under contention.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ZSET Time Indexes:&lt;/strong&gt; Threat events are indexed by timestamp for millisecond-range queries. Grouping by IP retrieves newest samples without scanning.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🔋 Under the Hood: How Redis Powers Real-Time API Threat Detection
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Brute-force Attack Detection:&lt;/strong&gt;&lt;br&gt;
Per-minute counters in Redis track failed attempts across &lt;strong&gt;username&lt;/strong&gt;, &lt;strong&gt;IP&lt;/strong&gt;, and &lt;strong&gt;username+IP&lt;/strong&gt;. A 1-minute bucket granularity with a multi-minute window and TTL provides precise rolling windows. Threshold trips set cool-off locks in Redis to dampen bursts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Enumeration Attack Detection:&lt;/strong&gt;&lt;br&gt;
For each IP and minute, I track &lt;strong&gt;distinct endpoints&lt;/strong&gt; accessed using Sets/HyperLogLog. A high cardinality over a short window is a strong enumeration signal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Replay Attack Prevention:&lt;/strong&gt;&lt;br&gt;
An idempotency/correlation key is written with &lt;code&gt;SETNX&lt;/code&gt; and a short TTL. If the same key reappears, Redis rejects it at the door — no double processing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IP Rate-Limit Abuse:&lt;/strong&gt;&lt;br&gt;
A &lt;strong&gt;token bucket&lt;/strong&gt; per principal is stored in a Hash (credits + last timestamp). With &lt;strong&gt;WATCH/MULTI/EXEC&lt;/strong&gt;, credits are replenished by elapsed time and atomically decremented per request, ensuring accurate limits even at high concurrency.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Traffic/DoS Anomaly Signal:&lt;/strong&gt;&lt;br&gt;
Each endpoint maintains per-minute counts. I keep an &lt;strong&gt;EWMA&lt;/strong&gt; mean/variance in Redis and compute a &lt;strong&gt;z-score&lt;/strong&gt; per minute; large spikes flag anomalies fast, without shipping data elsewhere.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real-time Analytics:&lt;/strong&gt;&lt;br&gt;
Threat-positive events are stored as compact Hashes and indexed in &lt;strong&gt;ZSETs&lt;/strong&gt; by time. Queries over &lt;code&gt;[from,to]&lt;/code&gt; return totals and IP-grouped samples in milliseconds, regardless of raw traffic volume.&lt;/p&gt;

&lt;p&gt;This approach lets me track:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Total threats in any timeframe.&lt;/li&gt;
&lt;li&gt;Breakdown by IP with the newest, most relevant samples.&lt;/li&gt;
&lt;li&gt;Drill-down to &lt;strong&gt;full event details&lt;/strong&gt; by ID.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The high performance, flexibility, and atomic semantics of Redis 8 power the entire engine with minimal latency and maximum reliability.&lt;/p&gt;

&lt;h2&gt;
  
  
  🚢 Conclusion
&lt;/h2&gt;

&lt;p&gt;At the core of this engine, &lt;strong&gt;Redis 8&lt;/strong&gt; acts as the brain that powers every detection and analytics decision. From counting requests and tracking unique endpoints, to blocking replays and indexing threats by time, Redis does all the heavy lifting in real time. Its atomic operations, TTL-based rolling windows, sets, hashes, and sorted sets keep detection instant and analytics lightning-fast, all within a single, high-performance system.&lt;br&gt;
Redis isn’t just supporting the process, it is the process that makes real-time API security possible.&lt;/p&gt;




</description>
      <category>redischallenge</category>
      <category>devchallenge</category>
      <category>database</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
