request path
Walk of GET /alice/stats with Redis on. Same CacheRateLimitMiddleware as the rest of the family. platform="gfg".
- CORS middleware (added first, runs second).
CacheRateLimitMiddlewarewithplatform="gfg"(added last, runs first).- Skip check. Path is not
/,/docs,/redoc,/openapi.json,/favicon.ico. Method is GET. Redis is on. - Handle is the first path segment, lowercased:
alice. A segment with a.is skipped, sofavicon.iconever becomes a handle. - Cache key is
cache:gfg:{sha256(GET:/alice/stats:sorted_query)}. - On HIT, body is base64-decoded and returned with
X-Cache: HIT. Done. - Negative key
invalid:gfg:alice. On HIT, HTTP 404User does not exist,X-Cache: NEGATIVE-HIT. Invalid-user rate limits apply (10/IP and 5/handle per 10 minutes). - Live limits: 60 req/min per IP, 30 req/min per handle. Over: 429 with
Retry-Afterand exponential backoff 5s to 300s. - Route
get_statshits authapi for the profile and practiceapi for submissions, thenstats_fromplusbuild_topic_analysis. make_envelopewraps it. HTTP 200 cached for 3600s unlessCache-Controlsays otherwise.X-Cache: MISS.
Without Redis, steps 5 to 8 disappear. Every GET hits GFG. Rate limits disappear too. That is fine on a laptop and noisy on a public host.
Tag cache _TAG_CACHE is process-local and independent of Redis. Problem tags do not change often, so they survive a Redis miss.
sequenceDiagram
participant B as Browser
participant MW as CacheRateLimit
participant R as Redis
participant RT as stats route
participant A as authapi.geeksforgeeks.org
participant P as practiceapi.geeksforgeeks.org
B->>MW: GET /alice/stats
MW->>R: GET cache:gfg:digest
alt hit
R-->>MW: body
MW-->>B: 200 X-Cache HIT
else miss
MW->>R: GET invalid:gfg:alice
MW->>R: INCR rl:ip and rl:handle
MW->>RT: call_next
RT->>A: user-profile-info
RT->>P: problems submissions
RT-->>MW: envelope
MW->>R: SETEX cache 3600s
MW-->>B: 200 X-Cache MISS
endIP is X-Forwarded-For first hop, else X-Real-IP, else request.client.host. Vercel sits in front, so the forwarded header is the one that matters.
/playground is not in SKIP_PATHS. With Redis on, the first path segment is treated as handle playground. Harmless, slightly weird.
Invalid-user markers: user does not exist, user not found, not found on, invalid username. Contest-empty is not one of them. Contests, rating, and badges return empty models on a real user, so those 200s are cached as normal HIT, not NEGATIVE-HIT.
Redis env#
REDIS_URL turns the middleware on. There is no in-process HTTP cache. Redis errors fail open: cache miss, rate limit allow.
| Env | Default |
|---|---|
API_CACHE_TTL_SECONDS |
3600 |
INVALID_USER_CACHE_TTL_SECONDS |
300 |
RATE_LIMIT_IP_REQUESTS |
60 per 60s |
RATE_LIMIT_HANDLE_REQUESTS |
30 per 60s |
INVALID_RATE_LIMIT_IP_REQUESTS |
10 per 600s |
INVALID_RATE_LIMIT_HANDLE_REQUESTS |
5 per 600s |
RATE_LIMIT_BACKOFF_BASE_SECONDS |
5 |
RATE_LIMIT_BACKOFF_MAX_SECONDS |
300 |
Keys:
cache:gfg:{sha256}invalid:gfg:{handle}rl:ip:gfg:{ip}/rl:handle:gfg:{handle}backoff:{same}/violations:{same}