Skip to main content

Posts

Showing posts with the label Redis

Implementing Distributed Rate Limiting in REST APIs Using Redis

  Scaling a backend to handle millions of requests is a significant architectural milestone. However, operating a distributed API architecture introduces an immediate vulnerability: coordinated abuse. When malicious actors scrape endpoints, enumerate data, or launch layer 7 DDoS attacks, local memory limits provide zero protection. If you rely on per-instance, in-memory rate limiting within a load-balanced environment, you are effectively multiplying your request limits by the number of active server instances. A client allowed 100 requests per minute can consume 100 requests  per node . To enforce a global, strict limit across a cluster, the architecture requires a centralized, high-performance state store. Redis is the industry standard for this task due to its microsecond latency and single-threaded execution model. The Root Cause: Local State and Race Conditions In a monolithic architecture, a standard middleware tracks IP addresses and request counts directly in RAM. In a...

Docker Networking Fix: Connecting Redis Session Stores from Localhost

  You have just containerized a working backend application. It runs perfectly on your local machine using   npm run dev   or   go run main.go . However, the moment you spin it up inside a Docker container, your logs explode with a fatal error: Error: connect ECONNREFUSED 127.0.0.1:6379 You are attempting to connect to a Redis session store. You know Redis is running. You can connect to it via your CLI. Yet, the containerized application acts as if the database doesn't exist. This is the single most common networking hurdle developers face when migrating from bare-metal development to containerized environments. It stems from a fundamental misunderstanding of how Docker handles the loopback interface ( localhost ). The Root Cause: The Loopback Lie To fix this, you must understand why  127.0.0.1  fails inside a container. When your application runs directly on your host OS (Windows, macOS, or Linux),  localhost  refers to your computer's network in...