Limitra
Limitra is a flexible rate-limiting engine that lets you choose and adapt algorithms based on your traffic needs.
Install / Use
/learn @divyansharma001/LimitraREADME
Limitra
Limitra is a production-grade rate limiter that prioritizes system survival.
Unlike standard limiters that force a static choice (Redis vs. Memory), Limitra allows you to hot-swap strategies based on system load. Switch from accurate-but-expensive Redis algorithms to fast-and-cheap Memory algorithms dynamically when your server is under stress.
Features
Adaptive: Automatically switches strategies based on Event Loop Lag or Redis availability.
Modular: Decoupled architecture. Mix and match Algorithms (Sliding, Fixed, Token) with Stores (Redis, Memory).
Atomic Redis: Uses custom Lua scripts for race-condition-free distributed limiting.
Lightweight: Redis is an optional peer dependency. Zero bloat for memory-only users.
TypeScript: Fully typed with modern ESM support.
Installation
npm install limitra
Note: If you plan to use Redis, you must install the client yourself:
npm install ioredis
Quick Start
Here is the standard setup using the Sliding Window algorithm (Industry Standard) with Redis.
import express from "express";
import Redis from "ioredis";
import {
createRedisStore,
createSlidingWindow,
limitra
} from "limitra";
const app = express();
const redisClient = new Redis();
// 1. Create the Store
const store = createRedisStore(redisClient);
// 2. Create the Limiter (100 requests per minute)
const limiter = createSlidingWindow(store, {
points: 100,
duration: 60
});
// 3. Apply Middleware
app.use(limitra({
limiter,
// Optional: Custom key generator (default is req.ip)
keyGenerator: (req) => req.user?.id || req.ip
}));
app.get("/", (req, res) => res.send("Welcome!"));
app.listen(3000);
The Adaptive Limiter (Killer Feature)
This is why you use Limitra. Prevent "Death Spirals" by shedding load cheaply when your server is dying.
Scenario:
- Normal: Use Redis (Sliding Window) for global consistency.
- Panic: If Event Loop Lag > 50ms, switch to Memory (Fixed Window) to save I/O.
import {
createAdaptiveLimiter,
createMemoryStore,
createRedisStore,
createSlidingWindow,
createFixedWindow,
measureEventLoopLag,
limitra
} from "limitra";
import Redis from "ioredis";
// 1. Define Strategies
const redisStore = createRedisStore(new Redis());
const memoryStore = createMemoryStore();
// Accurate but expensive (Network I/O)
const normalStrategy = createSlidingWindow(redisStore, { points: 10, duration: 60 });
// Fast but local (In-Memory)
const panicStrategy = createFixedWindow(memoryStore, { points: 5, duration: 60 });
// 2. Create the Brain
const adaptiveLimiter = createAdaptiveLimiter({
strategies: {
"normal": normalStrategy,
"panic": panicStrategy
},
selector: async () => {
// Check Event Loop Lag
const lag = await measureEventLoopLag();
// If lag is high, server is struggling. Don't call Redis.
if (lag > 50) return "panic";
return "normal";
}
});
// 3. Use it
app.use(limitra({ limiter: adaptiveLimiter }));
API Reference
1. Algorithms
Limitra supports three core algorithms. You can use any algorithm with any store.
| Algorithm | Function | Use Case |
|-----------|----------|----------|
| Sliding Window | createSlidingWindow | Recommended. Best balance of accuracy and fairness. Prevents "edge" spikes. |
| Fixed Window | createFixedWindow | Fastest. Best for "Panic Mode" or simple limits. Resets strictly at time boundaries. |
| Token Bucket | createTokenBucket | Bursty Traffic. Allows a burst of requests up to capacity, then refills at a steady rate. |
2. Stores
createMemoryStore()
Stores data in a JavaScript Map.
- Pros: Fastest (~0ms latency). No external dependencies.
- Cons: State is local to the process (not shared across a cluster). Data lost on restart.
createRedisStore(redisClient)
Requires an ioredis client instance.
- Pros: Distributed state (shared limits across multiple servers).
- Cons: Adds network latency.
- Atomicity: Uses custom Lua scripts to ensure accuracy under high concurrency.
3. Middleware Options
The limitra function accepts the following options:
limitra({
limiter: RateLimiter; // The instance created via algorithms
// How to identify the user. Default: req.ip
keyGenerator?: (req: Request) => string;
// Custom error message or object. Default: "Too many requests..."
message?: string | object;
// HTTP status code. Default: 429
statusCode?: number;
})
Architecture
Limitra uses a Strategy Pattern to decouple logic from storage.
graph TD
UserRequest --> ExpressMiddleware
ExpressMiddleware --> AdaptiveLimiter
subgraph "Decision Layer"
AdaptiveLimiter -- "Check Health" --> EventLoopProbe
AdaptiveLimiter -- "Select Strategy" --> StrategySelector
end
subgraph "Execution Layer"
StrategySelector -- "Normal" --> SlidingWindow
StrategySelector -- "Panic" --> FixedWindow
SlidingWindow -- "Distributed" --> RedisStore
FixedWindow -- "Local" --> MemoryStore
end
Development
To build the project locally or run tests:
# Install dependencies
npm install
# Build the project (outputs to dist/)
npm run build
# Run the example server
npx tsx examples/express-server.ts
Related Skills
node-connect
349.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
349.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
349.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
