Skip to content
Charan Kumar Reddy
Distributed Systems / SDEComplete

OctoScale

URL shortener with an atomic Redis Lua sliding-window rate limiter

URL shortener on FastAPI, PostgreSQL and Redis: Base62 codes from an atomic Redis counter, cache-aside redirects, an atomic Lua sliding-window rate limiter with an in-memory fallback, and batched click analytics. Load-tested at 2,404.8 redirects/s with zero errors and exact rate-limit enforcement.

Free instance; the first request after idle can take a minute.

cached redirects at 126 connections, 0 errors
2,404.8 req/s
benchmarks/results.json
requests allowed in a 3× burst: exactly the limit
600 / 1,800
benchmarks/results.json

The problem

A URL shortener's redirect path must stay fast under load, issue collision-free codes across workers, keep one client from flooding it, and keep serving when Redis fails.

The approach

A Lua script run with EVALSHA trims, counts and records each request atomically on a Redis sorted set; redirects read through a Redis cache with PostgreSQL fallback; codes come from INCR seeded with SET NX; clicks are queued and written in batches.

Architecture

  1. 01

    Rate limiter (Lua)

    One script per request via EVALSHA: Redis TIME, ZREMRANGEBYSCORE, ZCARD, then ZADD with a unique member and PEXPIRE only when allowed; separate per-client limits for redirects (600/min) and the API (100/min).

  2. 02

    Fallback and circuit breaker

    On a Redis error or 0.5 s timeout, Redis is skipped for 5 s and an in-process sliding window enforces the same limits per worker.

  3. 03

    Redirect path

    Cache-aside read from Redis with PostgreSQL fallback, 307 response, and a non-blocking enqueue of the click event.

  4. 04

    Short-code allocation

    INCR on a counter seeded from MAX(id) with SET NX, encoded in Base62; custom-code races map to 400 and a Redis outage to 503.

  5. 05

    Batched click writer

    Per-worker queue flushed every 0.5 s: counter UPDATEs in ascending id order in one executemany plus a bulk INSERT; failed batches are retried up to 3 times.

  6. 06

    Pools and workers

    Shared Redis connection pool (RESP2) and an asyncpg pool per uvicorn worker; Docker Compose with PostgreSQL, Redis and health checks.

Key decisions

  • Does the whole sliding-window check in one Lua script so trim, count and insert cannot interleave between workers, using the Redis clock so app hosts with skewed clocks agree.
  • Records only allowed requests, so a client that keeps retrying is not locked out beyond the window.
  • Falls back to a per-process limiter instead of failing open, accepting that the effective limit multiplies by the worker count during an outage.
  • Batches click writes instead of two writes per redirect, accepting that up to 0.5 s of clicks can be lost on a crash.
  • Orders counter updates by id so concurrent batches from different workers always lock rows in the same order.

Results

  • Recorded run: 2,404.8 redirects/s (p50 37.005 ms, p99 219.874 ms) and 1,155.8 creates/s (p99 629.644 ms) with 0 errors at 126 connections.
  • Every 201 produced one URL row and every 307 one click row (73,728 of 73,728) after the deadlock fix.
Measured results and the files they come from
MetricValueSource
Cached redirects, 126 connections, 30 s2,404.8 req/s · p99 219.874 msbenchmarks/results.json
Link creation, 126 connections, 15 s1,155.8 req/s · p50 73.909 msbenchmarks/results.json
Rate limiter, 3x burst from one client600 of 1,800 allowed (exact)benchmarks/results.json
Responses persisted after the run73,728 / 73,728 clicksbenchmarks/results.json

How it is tested

  • 36 pytest tests on fakeredis with Lua: exact limits, an 80-request concurrent burst against a limit of 50, fallback on Redis failure, race-free counter seeding, redirects during a Redis outage, and click-batch retries. CI runs them on Python 3.12 and 3.13.
  • benchmarks/load_test.py: multi-process asyncio and httpx load generator with redirect, create and limiter-burst phases, hardware metadata and an optional database integrity check.

Limits, stated plainly

  • Benchmarked on one 14-core Windows laptop with the client, PostgreSQL and a Windows build of Redis 5 on the same host; not a multi-host capacity figure.
  • Clicks wait in memory for up to 0.5 s and are lost if a worker crashes.
  • No Alembic migrations; tables are created at startup.

Stack

PythonFastAPIRedis (Lua, ZSET)PostgreSQLasyncpgSQLAlchemyDockerpytest