Backend Development 16 min read

Idempotent API Design and Distributed Rate Limiting with Token Bucket, Leaky Bucket, Nginx, and Redis+Lua

This article explains how to achieve interface idempotency using unique business IDs or token mechanisms and presents comprehensive distributed rate‑limiting techniques—including token‑bucket and leaky‑bucket algorithms, Nginx directives, Guava RateLimiter, and Redis‑Lua scripts—along with practical Spring Boot code examples.

Architecture Digest
Architecture Digest
Architecture Digest
Idempotent API Design and Distributed Rate Limiting with Token Bucket, Leaky Bucket, Nginx, and Redis+Lua

Interface idempotency ensures that repeated requests produce the same result, typically by using a unique business identifier or a token mechanism to lock updates and inserts.

Distributed rate limiting controls traffic across multiple dimensions such as QPS, connection count, transmission speed, black‑white lists, and the whole distributed environment.

Common algorithms include the token‑bucket and leaky‑bucket methods, each with distinct characteristics for handling burst traffic.

Typical implementations:

Guava RateLimiter client‑side limiting (Maven dependency and controller examples).

Nginx limit_req and limit_conn directives with zone, burst, and nodelay settings.

Redis + Lua scripts for atomic limit checks, integrated with Spring Boot via StringRedisTemplate and RedisScript .

Additional components such as a custom AccessLimiter service, Spring configuration, and an AOP annotation are provided to encapsulate the limiting logic and apply it declaratively.

RedisIdempotencynginxRate LimitingLuatoken bucketleaky bucket
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login 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.