As asked
Design a rate limiter for a Ktor REST API that enforces per-user request limits of 100 requests per minute across multiple server instances. Walk me through the data store choice, the algorithm, and how you integrate it as a Ktor plugin.
Sample answer outline
A strong answer picks Redis with a sliding window counter or token bucket. Redis INCR + EXPIRE implements fixed window; a sorted set with ZADD/ZRANGEBYSCORE implements sliding window. The Ktor plugin is installed via createApplicationPlugin and intercepts the ApplicationCallPipeline.Plugins phase, checking the counter before proceeding. The candidate should discuss atomic Redis operations (EVAL with Lua) to avoid race conditions and the latency cost of a Redis round-trip per request.
Expect these follow-ups
- How do you handle Redis being unavailable, fail-open or fail-closed?
- How would you make the rate limiter configurable per route?