【redis】redis滑动时间窗口算法实现限流

滑动时间窗口实现限流

依赖

首先创建一个Springboot项目

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.5.4</version>
        </dependency>

配置

server:
  port: 7777
spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password: 你的redis密码,一般默认为空

实现

@Service
@Slf4j
public class RedisSlidingWindowDemo {
    @Resource
    RedisTemplate<Object, Object> redisTemplate;
    @Resource
    StringRedisTemplate stringRedisTemplate;
    String key = "redis_limiter";
    // 窗口大小, 单位:毫秒
    long windowTime = 1000;
    // 每 窗口大小时间 最多 多少个请求
    int limitCount = 5;
    long ttl = 10000;

    public void req() {
        // 当前时间
        long currentTime = System.currentTimeMillis();
        // 窗口起始时间
        long windowStartMs = currentTime - windowTime;
        ZSetOperations<Object, Object> zSetOperations = redisTemplate.opsForZSet();
        // 清除窗口过期成员
        zSetOperations.removeRangeByScore(key, 0, windowStartMs);
        // 添加当前时间 score=当前时间 value=当前时间+随机数,防止并发时重复
        zSetOperations.add(key, currentTime, currentTime + RandomUtil.randomInt());

    }

    public boolean canReq() {
        long currentTime = System.currentTimeMillis();
        int count = redisTemplate.opsForZSet().rangeByScore(key, currentTime - windowTime, currentTime).size();
        log.info("当前线程进入判断能否请求,当前时间={},窗口={}-{},数量={}", currentTime, (currentTime - windowTime), currentTime, count);
        if (count < limitCount) {
            req();
            return true;
        } else {
            return false;
        }
    }

    public boolean canReqByLua() {
        DefaultRedisScript<Long> script = new DefaultRedisScript<>();
        script.setScriptText(lua());
        script.setResultType(Long.class);
        long currentTime = System.currentTimeMillis();
        Long execute = stringRedisTemplate.execute(script, Collections.singletonList(key), String.valueOf(currentTime),
                String.valueOf(ttl), String.valueOf(windowTime), String.valueOf(limitCount),
                String.valueOf(currentTime + RandomUtil.randomInt()));
        boolean result = execute != 0;
        log.info("{}线程进入判断能否请求,当前时间={},窗口={}-{},数量={},result={}", Thread.currentThread().getName(), currentTime,
                (currentTime - windowTime), currentTime, execute, result);
        return result;
    }

    public String lua() {
        return "local key = KEYS[1]\n" +
                "local currentTime = tonumber(ARGV[1])\n" +
                "local ttl = tonumber(ARGV[2])\n" +
                "local windowTime = tonumber(ARGV[3]) --\n" +
                "local limitCount = tonumber(ARGV[4])\n" +
                "local value = tonumber(ARGV[5])\n" +
                "redis.call('zremrangebyscore', key, 0, currentTime - windowTime)\n" +
                "local currentNum = tonumber(redis.call('zcard', key))\n" +
                "local next = currentNum + 1\n" +
                "if next > limitCount then\n" +
                "return 0;\n" +
                "else\n" +
                "redis.call(\"zadd\", key, currentTime, value)\n" +
                "redis.call(\"expire\", key, ttl)\n" +
                "return next\n" +
                "end";
    }
}

测试

@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBootDemoApplication.class)
public class RedisSlidingWindowDemoTest {

    @Resource
    RedisSlidingWindowDemo redisSlidingWindowDemo;

    @Test
    public void req() {
        Integer threadNum = 10;
        CountDownLatch downLatch = new CountDownLatch(threadNum);
        for (int i = 0; i < threadNum; i++) {
            new Thread(() -> {
                for (int j = 0; j < 20; j++) {
                    boolean access = redisSlidingWindowDemo.canReqByLua();
                    try {
                        TimeUnit.MILLISECONDS.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                downLatch.countDown();
            }, "t" + i).start();
        }
        try {
            downLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
### 使用 Redis 实现滑动窗口算法进行限流的最佳实践 #### 1. 滑动窗口限流的特点 滑动窗口限流相比固定窗口限流更加精确,能够更好地处理突发流量。然而,这种实现方式确实更为复杂,并且对Redis的性能有更高的要求,在高并发场景下尤其如此[^1]。 #### 2. 利用 ZSET 数据结构 通过使用 Redis 的 `ZSET` (有序集合),可以在一定时间内记录请求的时间戳并对其进行排序。对于每一个新的请求到来时,程序会移除超过指定时间段之外的数据项,从而保持数据的有效性和准确性[^2]。 #### 3. Pipeline 提升效率 由于涉及到多次与 Redis 进行交互的操作(如增加新成员、删除过期成员以及获取当前计数值等),采用 pipeline 方式批量执行命令能显著减少网络延迟带来的开销,提高整体性能表现。 #### 4. Lua 脚本确保原子性 为了避免竞争条件的发生,推荐利用 Redis 内置的支持事务特性的 Lua 脚本来完成整个流程的一次性提交。这不仅简化了客户端代码的设计难度,同时也保障了操作的安全可靠[^4]。 --- 以下是 Python 版本的一个简单示例来展示如何基于上述原则构建一个高效的滑动窗口限流器: ```python import time from redis import StrictRedis class SlidingWindowRateLimiter(object): def __init__(self, redis_client: StrictRedis, key_prefix='swrl:', window_size=60, max_requests=100): self.redis = redis_client self.key_prefix = key_prefix self.window_size = window_size self.max_requests = max_requests def _get_key(self, identifier): return f"{self.key_prefix}{identifier}" def is_allowed(self, identifier): now_ms = int(time.time() * 1000) key = self._get_key(identifier) lua_script = """ local current_time = tonumber(ARGV[1]) local cutoff_time = current_time - tonumber(ARGV[2]) -- Remove expired entries from the sorted set. redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', '(' .. tostring(cutoff_time)) -- Count remaining items within allowed timeframe. local count = redis.call('ZCOUNT', KEYS[1], '-inf', '+inf') if tonumber(count) >= tonumber(ARGV[3]) then return 0 else redis.call('ZADD', KEYS[1], current_time, current_time) return 1 end """ result = self.redis.eval(lua_script, [key], [ str(now_ms), str(self.window_size*1000), str(self.max_requests)]) return bool(result) if __name__ == "__main__": rdb = StrictRedis(host="localhost", port=6379, db=0) limiter = SlidingWindowRateLimiter(rdb, 'test_api_', 60, 5) user_id = "user_1" for i in range(8): print(f"Attempt {i}: ", limiter.is_allowed(user_id)) ``` 此段代码定义了一个名为 `SlidingWindowRateLimiter` 类用于管理特定 API 接口下的用户访问频率控制逻辑;其中包含了两个主要方法 `_get_key()` 和 `is_allowed()` 。前者负责生成唯一标识符以便区分不同资源对象间的统计数据差异;后者则实现了核心业务功能——即依据传入的身份参数决定是否允许此次调用继续前进还是返回拒绝响应给前端应用层面上做进一步处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值