Basic Nginx rate limiting using fixed windows has a well-known problem: a burst of requests at the window boundary can double the effective rate. Production APIs serving Asia-Pacific users need fairer, more accurate rate limiting — sliding window counters and token buckets implemented in Redis, enforced at the application layer, with per-API-key tiers rather than per-IP blunt limits. This guide implements professional-grade rate limiting on a Hong Kong VPS.
Rate Limiting Strategies Compared
| Strategy | Accuracy | Burst handling | Complexity |
|---|---|---|---|
| Fixed window (Nginx) | Poor (boundary spikes) | None | Trivial |
| Sliding window (Redis) | Excellent | Smooth | Medium |
| Token bucket | Excellent | Allows controlled bursts | Medium |
| Leaky bucket | Excellent | None (strict queue) | Medium |
Step 1: Redis Sliding Window in Node.js
<code"># npm install ioredis
import Redis from 'ioredis'
const redis = new Redis({
host: '127.0.0.1',
port: 6379,
password: process.env.REDIS_PASSWORD,
})
// Lua script — atomic sliding window rate check
// Returns: [allowed (0/1), current_count, limit, ttl_ms]
const slidingWindowScript = `
local key = KEYS[1]
local now = tonumber(ARGV[1])
local window_ms = tonumber(ARGV[2])
local limit = tonumber(ARGV[3])
local request_id = ARGV[4]
-- Remove requests outside the sliding window
redis.call('ZREMRANGEBYSCORE', key, '-inf', now - window_ms)
-- Count current requests in window
local count = redis.call('ZCARD', key)
if count < limit then
-- Add this request to the window
redis.call('ZADD', key, now, request_id)
-- Set TTL to clean up the key
redis.call('PEXPIRE', key, window_ms)
return {1, count + 1, limit, redis.call('PTTL', key)}
else
return {0, count, limit, redis.call('PTTL', key)}
end
`
interface RateLimitResult {
allowed: boolean
current: number
limit: number
resetMs: number
remaining: number
}
async function checkRateLimit(
identifier: string, // API key, user ID, or IP
tier: 'free' | 'pro' | 'enterprise',
endpoint: string
): Promise {
const limits = {
free: { limit: 100, windowMs: 60_000 }, // 100/min
pro: { limit: 1000, windowMs: 60_000 }, // 1000/min
enterprise: { limit: 10000, windowMs: 60_000 }, // 10000/min
}
const { limit, windowMs } = limits[tier]
const key = `ratelimit:${tier}:${endpoint}:${identifier}`
const now = Date.now()
const requestId = `${now}-${Math.random()}`
const result = await redis.eval(
slidingWindowScript, 1, key, now, windowMs, limit, requestId
) as [number, number, number, number]
const [allowed, current, limitVal, ttlMs] = result
return {
allowed: allowed === 1,
current,
limit: limitVal,
resetMs: ttlMs > 0 ? ttlMs : windowMs,
remaining: Math.max(0, limitVal - current),
}
}Step 2: Express Middleware
<code"># Rate limiting middleware with API key tiers
import type { Request, Response, NextFunction } from 'express'
async function rateLimitMiddleware(req: Request, res: Response, next: NextFunction) {
// Extract identifier — prefer API key over IP
const apiKey = req.headers['x-api-key'] as string | undefined
const identifier = apiKey || req.ip || 'anonymous'
// Determine tier from API key
const tier = await getApiKeyTier(apiKey) // Lookup in your DB/Redis
const endpoint = req.route?.path || req.path
const result = await checkRateLimit(identifier, tier, endpoint)
// Always add rate limit headers (industry standard)
res.set({
'X-RateLimit-Limit': result.limit,
'X-RateLimit-Remaining': result.remaining,
'X-RateLimit-Reset': Math.ceil(Date.now() / 1000 + result.resetMs / 1000),
'X-RateLimit-Policy': `${result.limit};w=60`, // RFC 9110 format
})
if (!result.allowed) {
return res.status(429).json({
error: 'Rate limit exceeded',
limit: result.limit,
remaining: 0,
resetIn: `${Math.ceil(result.resetMs / 1000)} seconds`,
upgrade: tier === 'free' ? 'Upgrade to Pro for 10× higher limits' : undefined,
})
}
next()
}
// Apply globally
app.use(rateLimitMiddleware)
// Or per-route with custom limits
app.get('/api/search', specificRateLimit({ limit: 20, windowMs: 60_000 }), searchHandler)Step 3: Token Bucket (Allows Bursts)
<code"># Token bucket: allows short bursts up to bucket capacity
# Tokens refill at a steady rate — more realistic than strict window counting
const tokenBucketScript = `
local key = KEYS[1]
local capacity = tonumber(ARGV[1]) -- Max tokens (burst capacity)
local refill_rate = tonumber(ARGV[2]) -- Tokens per second
local now = tonumber(ARGV[3])
local tokens_requested = tonumber(ARGV[4])
-- Get current state
local bucket = redis.call('HMGET', key, 'tokens', 'last_refill')
local tokens = tonumber(bucket[1]) or capacity
local last_refill = tonumber(bucket[2]) or now
-- Calculate tokens to add since last request
local elapsed = (now - last_refill) / 1000 -- Convert ms to seconds
local new_tokens = math.min(capacity, tokens + elapsed * refill_rate)
if new_tokens >= tokens_requested then
-- Consume tokens
new_tokens = new_tokens - tokens_requested
redis.call('HMSET', key, 'tokens', new_tokens, 'last_refill', now)
redis.call('PEXPIRE', key, 3600000) -- 1 hour TTL
return {1, math.floor(new_tokens), capacity}
else
-- Not enough tokens
redis.call('HMSET', key, 'tokens', new_tokens, 'last_refill', now)
redis.call('PEXPIRE', key, 3600000)
return {0, math.floor(new_tokens), capacity}
end
`
async function tokenBucketCheck(apiKey: string, config: {
capacity: number // Max burst (e.g., 50 requests)
refillRate: number // Requests per second steady state (e.g., 10/s)
}): Promise<{ allowed: boolean; tokens: number; capacity: number }> {
const key = `tokenbucket:${apiKey}`
const result = await redis.eval(
tokenBucketScript, 1, key,
config.capacity, config.refillRate, Date.now(), 1
) as [number, number, number]
return {
allowed: result[0] === 1,
tokens: result[1],
capacity: result[2],
}
}Step 4: Multi-Tier Rate Limit Plan
<code"># Rate limit tiers stored in Redis or your DB
const API_TIERS = {
free: {
rpm: 100, // Requests per minute
rpd: 1000, // Requests per day
burst: 20, // Burst capacity (token bucket)
endpoints: {
'/api/search': 20, // Search is expensive — limit more
'/api/ai': 5, // AI endpoints — very strict
'/api/*': 100, // Default
}
},
pro: {
rpm: 1000,
rpd: 20000,
burst: 100,
endpoints: {
'/api/search': 200,
'/api/ai': 50,
'/api/*': 1000,
}
},
enterprise: {
rpm: 10000,
rpd: 500000,
burst: 1000,
endpoints: { '/api/*': 10000 }
}
}
// Check both minute and daily limits
async function dualWindowCheck(apiKey: string, tier: string, endpoint: string) {
const config = API_TIERS[tier]
const [minuteResult, dailyResult] = await Promise.all([
checkRateLimit(apiKey, tier, `${endpoint}:minute`),
checkRateLimit(apiKey, `${tier}:daily`, endpoint),
])
return {
allowed: minuteResult.allowed && dailyResult.allowed,
minuteRemaining: minuteResult.remaining,
dailyRemaining: dailyResult.remaining,
}
}Step 5: Rate Limit Dashboard
<code"># Admin endpoint to view current usage per API key
app.get('/admin/rate-limits/:apiKey', adminAuth, async (req, res) => {
const { apiKey } = req.params
const pattern = `ratelimit:*:${apiKey}`
const keys = await redis.keys(pattern)
const usage = await Promise.all(
keys.map(async key => {
const count = await redis.zcard(key)
const ttl = await redis.pttl(key)
return { key, count, resetIn: `${Math.ceil(ttl / 1000)}s` }
})
)
res.json({ apiKey, usage })
})
# Reset rate limit for a specific key (e.g., after plan upgrade)
app.delete('/admin/rate-limits/:apiKey', adminAuth, async (req, res) => {
const { apiKey } = req.params
const keys = await redis.keys(`ratelimit:*:${apiKey}`)
if (keys.length > 0) await redis.del(...keys)
res.json({ cleared: keys.length })
})Conclusion
Redis sliding window rate limiting on a Hong Kong VPS provides production-grade API protection — accurate per-second precision, per-API-key tier enforcement, and standard rate limit response headers that API consumers can act on. The Lua script approach ensures atomicity: no race conditions between the check and the increment, even under high concurrency from Asia-Pacific clients connecting via CN2 GIA. Token bucket limiting adds burst tolerance for legitimate clients with bursty usage patterns without sacrificing protection against abusive traffic.
Protect your API: Browse Server.HK Hong Kong VPS plans — Redis runs alongside your API on any VPS plan; rate limiting at 10,000 requests/second adds negligible latency overhead.