Implementing Distributed Rate Limiting in Spring Cloud Gateway with Token Bucket and Lua Script

This article explains how Spring Cloud Gateway uses a token‑bucket algorithm backed by Redis and a Lua script to perform distributed rate limiting, reviews common limiting algorithms, provides detailed Java and Lua code examples, and analyzes each step of the implementation for high‑concurrency systems.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Implementing Distributed Rate Limiting in Spring Cloud Gateway with Token Bucket and Lua Script

In distributed high‑concurrency systems, rate limiting is essential to prevent overload during traffic spikes such as Double‑11 sales or ticket‑booking bursts.

The article first reviews common rate‑limiting algorithms: simple counter/window, leaky bucket, and token bucket, highlighting their advantages and drawbacks.

Spring Cloud Gateway provides a built‑in rate‑limiting filter that implements the token‑bucket algorithm using Redis and a Lua script.

The Java class RedisRateLimiter (gateway/filter/ratelimit/RedisRateLimiter.java) prepares Redis keys, builds script arguments, executes the Lua script via redisTemplate.execute, and processes the result to determine whether the request is allowed and how many tokens remain.

The accompanying Lua script ( request_rate_limiter.lua) defines keys for token count and timestamp, reads parameters (rate, capacity, now, requested), calculates the fill time, token refill based on elapsed time, decides if the request can be served, updates Redis with the new token count and timestamp, and returns {allowed_num, new_tokens}.

A line‑by‑line analysis explains the meaning of each argument (tokens key, timestamp key, rate, capacity, now, requested) and the core logic: compute bucket fill time, retrieve and initialize token count, calculate elapsed time, refill tokens, check allowance, decrement tokens if allowed, and persist the state.

The article concludes with a call to share the content and join a technical community.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Distributed Systemsrate limitingLuaToken BucketSpring Cloud Gateway
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.