Back to Projects

Context-Aware Traffic Controller

Heavy Service Meshes introduce operational bloat, network latency, and massive friction for engineering teams. This project introduces a paradigm shift: a developer-native, drop-in middleware that brings enterprise-grade Quality of Service (QoS) and dynamic load shedding directly into the Node.js runtime, mathematically guaranteeing 'VIP lanes' stay open during catastrophic surges without the DevOps overhead.

Architecture & PrototypingTypeScriptNode.js (perf_hooks & v8 modules)Express / Fastify AdaptersIn-Memory Data Structures (Token Buckets)Little's Law Queuing Theory

The Vision

In the modern SaaS landscape, traditional infrastructure-level rate limiting is blind to business context. It treats a VIP checkout exactly the same as an anonymous bot refreshing a homepage. While massive enterprises attempt to solve this by deploying complex, heavy Service Meshes (like Istio), this approach introduces unnecessary network hops and massive operational overhead.
The vision for this Traffic Controller is to build a lean, developer-native solution. By packaging complex context-aware routing into a highly optimized, drop-in runtime middleware, engineering teams can seamlessly protect their database and event loops from Layer 7 traffic spikes. It delivers surgical, business-aware load shedding with zero friction to adoption.

The Edge-Runtime Architecture

To achieve microsecond execution times without relying on external infrastructure, the controller sits directly at the edge of the application's request lifecycle, intercepting traffic before it hits heavy business logic or databases.
  • The Metrics Engine: A lightweight background thread using Node's native perf_hooks that constantly monitors Event Loop Lag and Heap Memory, providing high-fidelity telemetry without adding blocking overhead.
  • The In-Memory Ledger: A strictly RAM-based Multi-Tier Token Bucket system. It tracks request capacity in nanoseconds without relying on external caches like Redis, eliminating network bottlenecks during system degradation.
  • The Gatekeeper Middleware: A synchronous, zero-dependency interceptor that evaluates current system health, parses stateless headers (e.g., X-User-Tier), and either instantly drops the request (503 Service Unavailable) or allows it through to the core application.

The Challenges

Challenge
The CPU Trap: In Node.js, monitoring overall host CPU is a deeply flawed metric for systemic stress. Because Node is single-threaded, a server can be completely unresponsive to users while the host machine only shows 25% CPU utilization.
Solution
We shifted the telemetry focus entirely to runtime Event Loop Lag.
  • The middleware runs a persistent, highly optimized background interval.
  • By calculating the drift between the expected execution time and the actual execution time, the middleware mathematically calculates the Event Loop Lag.
  • Once the lag crosses dynamic critical thresholds, the system implicitly understands the application is choking and autonomously triggers the load shedding protocols.
Challenge
The Cache Death Spiral: Traditional rate limiters rely on Redis to track request counts. During a traffic surge, forcing a struggling server to make thousands of network calls to Redis to determine routing logic creates a fatal I/O bottleneck.
Solution
We implemented strict zero-I/O Token Buckets entirely in JavaScript memory.
  • Capacity limits and multi-tiered Token Buckets are instantiated as highly optimized JS Map objects on application boot.
  • Evaluating a request's priority and debiting a token happens synchronously in O(1)O(1) time.
  • By eliminating the network hop, the middleware can reject thousands of non-essential requests per second with virtually zero added latency, allowing the event loop to instantly recover.
Challenge
The Wide Security Net: When system performance degrades, how do you ensure that critical business requests (like an enterprise checkout or webhook) aren't accidentally dropped alongside low-priority traffic?
Solution
We engineered a Multi-Tiered Token Bucket system with Dynamic Refill Throttling.
  • Requests are classified via rapid, shallow header parsing (e.g., X-Tier: VIP) rather than expensive database user lookups.
  • Separate token ledgers are maintained for distinct business tiers (e.g., Critical, Standard, Background).
  • When Event Loop Lag spikes, the middleware autonomously throttles the token refill rate of the lower-priority buckets to zero. The 'VIP lane' remains open and funded, mathematically guaranteeing high-value traffic survives the surge.

Architectural Trade-Offs & Vulnerabilities

trade-off
A core philosophy of this architecture is the strict delegation of network concerns. This middleware is purpose-built exclusively for Layer 7 (Application) Overload—protecting the database and event loop from massive spikes in legitimate, authenticated traffic or complex query exhaustion.
By design, we delegate blunt-force Layer 4 volumetric DDoS protection to Edge network providers like Cloudflare, AWS Shield, or Fastly. Attempting to solve volumetric network attacks within the application runtime is an anti-pattern. This separation of concerns allows our middleware to remain hyper-focused, lightweight, and deeply context-aware for business routing.
  • Layer 4 Network Defense is delegated to the Edge.
  • Layer 7 Business-Aware QoS is handled natively by the middleware.

© 2026 — This site documents my work and thinking around software system.

Open to senior full-stack web engineering roles — [email protected]Privacy Policy