Redis 从入门到精通(十二):典型业务场景实战 —— 排行榜、限流器、秒杀系统、Session 共享

Redis 从入门到精通(十二):典型业务场景实战 ------ 排行榜、限流器、秒杀系统、Session 共享


一、排行榜系统(Sorted Set)

1.1 需求分析

实现一个游戏全服排行榜,支持:

  • 玩家实时更新分数
  • 查询 Top 100
  • 查询某个玩家的排名和分数
  • 查询某个玩家前后的竞争对手

1.2 完整实现

java 复制代码
@Service
public class LeaderboardService {

    private static final String RANK_KEY = "game:rank:global";

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 更新玩家分数
     */
    public void updateScore(String playerId, double score) {
        redisTemplate.opsForZSet().add(RANK_KEY, playerId, score);
    }

    /**
     * 增加分数(增量更新)
     */
    public double addScore(String playerId, double delta) {
        return redisTemplate.opsForZSet().incrementScore(RANK_KEY, playerId, delta);
    }

    /**
     * 获取 Top N(降序,分数最高的在前)
     */
    public List<RankEntry> getTopN(int n) {
        Set<ZSetOperations.TypedTuple<String>> set = 
            redisTemplate.opsForZSet().reverseRangeWithScores(RANK_KEY, 0, n - 1);

        if (set == null) return Collections.emptyList();

        int rank = 1;
        List<RankEntry> result = new ArrayList<>();
        for (ZSetOperations.TypedTuple<String> tuple : set) {
            result.add(new RankEntry(rank++, tuple.getValue(), tuple.getScore()));
        }
        return result;
    }

    /**
     * 获取玩家排名(降序,第 1 名 rank 为 1)
     */
    public Long getPlayerRank(String playerId) {
        Long rank = redisTemplate.opsForZSet().reverseRank(RANK_KEY, playerId);
        return rank == null ? null : rank + 1;
    }

    /**
     * 获取玩家分数
     */
    public Double getPlayerScore(String playerId) {
        return redisTemplate.opsForZSet().score(RANK_KEY, playerId);
    }

    /**
     * 获取玩家前后 N 名的竞争对手
     */
    public List<RankEntry> getNearbyPlayers(String playerId, int n) {
        Long rank = redisTemplate.opsForZSet().reverseRank(RANK_KEY, playerId);
        if (rank == null) return Collections.emptyList();

        long start = Math.max(0, rank - n);
        long end = rank + n;

        Set<ZSetOperations.TypedTuple<String>> set = 
            redisTemplate.opsForZSet().reverseRangeWithScores(RANK_KEY, start, end);

        List<RankEntry> result = new ArrayList<>();
        long currentRank = start + 1;
        for (ZSetOperations.TypedTuple<String> tuple : set) {
            result.add(new RankEntry((int) currentRank++, tuple.getValue(), tuple.getScore()));
        }
        return result;
    }

    @Data
    @AllArgsConstructor
    public static class RankEntry {
        private int rank;
        private String playerId;
        private double score;
    }
}

性能分析

  • 更新分数:O(log N),百万级数据毫秒级
  • 查询 Top 100:O(log N + 100),恒定快速
  • 查询排名:O(log N)

二、限流器:滑动窗口算法

2.1 需求分析

限制每个用户每分钟最多 100 次请求。使用滑动窗口算法,比固定窗口更平滑。

2.2 完整实现 ------ Lua 脚本版

lua 复制代码
-- sliding_window_rate_limiter.lua
-- KEYS[1] = 限流 key(如 rate_limit:user:1001)
-- ARGV[1] = 窗口大小(毫秒,如 60000 表示 1 分钟)
-- ARGV[2] = 最大请求数(如 100)

local key = KEYS[1]
local window = tonumber(ARGV[1])
local max_requests = tonumber(ARGV[2])
local now = redis.call('TIME')  -- 返回 {秒, 微秒}
local current = now[1] * 1000 + math.floor(now[2] / 1000)

-- 删除窗口外的旧记录
redis.call('ZREMRANGEBYSCORE', key, 0, current - window)

-- 统计当前窗口内的请求数
local count = redis.call('ZCARD', key)

if count < max_requests then
    -- 允许通过,记录本次请求
    redis.call('ZADD', key, current, current .. '-' .. math.random())
    -- 设置 key 过期时间(窗口结束 + 缓冲)
    redis.call('PEXPIRE', key, window + 1000)
    return 1  -- 通过
else
    return 0  -- 限流
end
java 复制代码
@Component
public class SlidingWindowRateLimiter {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private String scriptSha;

    @PostConstruct
    public void init() {
        String script = """
            local key = KEYS[1]
            local window = tonumber(ARGV[1])
            local max_requests = tonumber(ARGV[2])
            local now = redis.call('TIME')
            local current = now[1] * 1000 + math.floor(now[2] / 1000)
            redis.call('ZREMRANGEBYSCORE', key, 0, current - window)
            local count = redis.call('ZCARD', key)
            if count < max_requests then
                redis.call('ZADD', key, current, current .. '-' .. math.random())
                redis.call('PEXPIRE', key, window + 1000)
                return 1
            else
                return 0
            end
            """;
        scriptSha = redisTemplate.execute(
            (RedisCallback<String>) conn -> conn.scriptLoad(script.getBytes())
        );
    }

    /**
     * @param key 限流标识
     * @param windowSeconds 窗口大小(秒)
     * @param maxRequests 最大请求数
     * @return true=通过, false=限流
     */
    public boolean isAllowed(String key, int windowSeconds, int maxRequests) {
        String fullKey = "rate_limit:" + key;
        Long result = redisTemplate.execute(
            new DefaultRedisScript<>(scriptSha, Long.class),
            Collections.singletonList(fullKey),
            String.valueOf(windowSeconds * 1000L),
            String.valueOf(maxRequests)
        );
        return result != null && result == 1;
    }
}
java 复制代码
// 拦截器中使用
@Component
public class RateLimitInterceptor implements HandlerInterceptor {

    @Autowired
    private SlidingWindowRateLimiter rateLimiter;

    @Override
    public boolean preHandle(HttpServletRequest request, 
            HttpServletResponse response, Object handler) {
        
        String userId = getUserId(request);
        boolean allowed = rateLimiter.isAllowed(userId, 60, 100);
        
        if (!allowed) {
            response.setStatus(429);
            response.getWriter().write("Too Many Requests");
            return false;
        }
        return true;
    }
}

三、秒杀系统:库存扣减与超卖防护

3.1 需求分析

秒杀的核心难题:高并发下防止超卖。单纯的"查库存 → 减库存 → 写回"在并发下不可靠(读-改-写竞态)。

3.2 方案:Lua 脚本 + 原子扣减

lua 复制代码
-- seckill.lua
-- KEYS[1] = 库存 key
-- KEYS[2] = 已购买用户集合 key
-- ARGV[1] = 用户 ID

local stock_key = KEYS[1]
local purchased_key = KEYS[2]
local user_id = ARGV[1]

-- 检查是否已经抢到过(防重复)
if redis.call('SISMEMBER', purchased_key, user_id) == 1 then
    return -1  -- 已购买
end

-- 检查库存
local stock = tonumber(redis.call('GET', stock_key) or '0')
if stock <= 0 then
    return 0  -- 库存不足
end

-- 扣减库存
redis.call('DECR', stock_key)
-- 记录购买用户
redis.call('SADD', purchased_key, user_id)

return 1  -- 秒杀成功
java 复制代码
@Service
public class SeckillService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private String seckillSha;

    @PostConstruct
    public void init() {
        String script = """
            local stock_key = KEYS[1]
            local purchased_key = KEYS[2]
            local user_id = ARGV[1]
            if redis.call('SISMEMBER', purchased_key, user_id) == 1 then
                return -1
            end
            local stock = tonumber(redis.call('GET', stock_key) or '0')
            if stock <= 0 then
                return 0
            end
            redis.call('DECR', stock_key)
            redis.call('SADD', purchased_key, user_id)
            return 1
            """;
        seckillSha = redisTemplate.execute(
            (RedisCallback<String>) conn -> conn.scriptLoad(script.getBytes())
        );
    }

    /**
     * 执行秒杀
     */
    public SeckillResult seckill(String productId, String userId) {
        String stockKey = "seckill:stock:" + productId;
        String purchasedKey = "seckill:purchased:" + productId;

        Long result = redisTemplate.execute(
            new DefaultRedisScript<>(seckillSha, Long.class),
            Arrays.asList(stockKey, purchasedKey),
            userId
        );

        if (result == null) {
            return SeckillResult.ERROR;
        }
        return switch (result.intValue()) {
            case 1 -> SeckillResult.SUCCESS;
            case 0 -> SeckillResult.SOLD_OUT;
            case -1 -> SeckillResult.ALREADY_PURCHASED;
            default -> SeckillResult.ERROR;
        };
    }

    /**
     * 初始化秒杀库存
     */
    public void initStock(String productId, int stock) {
        redisTemplate.opsForValue().set(
            "seckill:stock:" + productId, String.valueOf(stock));
        redisTemplate.delete("seckill:purchased:" + productId);
    }

    public enum SeckillResult {
        SUCCESS, SOLD_OUT, ALREADY_PURCHASED, ERROR
    }
}

完整秒杀流程
#mermaid-svg-9CcxWQ714luuATjY{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-9CcxWQ714luuATjY .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-9CcxWQ714luuATjY .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-9CcxWQ714luuATjY .error-icon{fill:#552222;}#mermaid-svg-9CcxWQ714luuATjY .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-9CcxWQ714luuATjY .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-9CcxWQ714luuATjY .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-9CcxWQ714luuATjY .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-9CcxWQ714luuATjY .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-9CcxWQ714luuATjY .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-9CcxWQ714luuATjY .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-9CcxWQ714luuATjY .marker{fill:#333333;stroke:#333333;}#mermaid-svg-9CcxWQ714luuATjY .marker.cross{stroke:#333333;}#mermaid-svg-9CcxWQ714luuATjY svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-9CcxWQ714luuATjY p{margin:0;}#mermaid-svg-9CcxWQ714luuATjY .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-9CcxWQ714luuATjY .cluster-label text{fill:#333;}#mermaid-svg-9CcxWQ714luuATjY .cluster-label span{color:#333;}#mermaid-svg-9CcxWQ714luuATjY .cluster-label span p{background-color:transparent;}#mermaid-svg-9CcxWQ714luuATjY .label text,#mermaid-svg-9CcxWQ714luuATjY span{fill:#333;color:#333;}#mermaid-svg-9CcxWQ714luuATjY .node rect,#mermaid-svg-9CcxWQ714luuATjY .node circle,#mermaid-svg-9CcxWQ714luuATjY .node ellipse,#mermaid-svg-9CcxWQ714luuATjY .node polygon,#mermaid-svg-9CcxWQ714luuATjY .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-9CcxWQ714luuATjY .rough-node .label text,#mermaid-svg-9CcxWQ714luuATjY .node .label text,#mermaid-svg-9CcxWQ714luuATjY .image-shape .label,#mermaid-svg-9CcxWQ714luuATjY .icon-shape .label{text-anchor:middle;}#mermaid-svg-9CcxWQ714luuATjY .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-9CcxWQ714luuATjY .rough-node .label,#mermaid-svg-9CcxWQ714luuATjY .node .label,#mermaid-svg-9CcxWQ714luuATjY .image-shape .label,#mermaid-svg-9CcxWQ714luuATjY .icon-shape .label{text-align:center;}#mermaid-svg-9CcxWQ714luuATjY .node.clickable{cursor:pointer;}#mermaid-svg-9CcxWQ714luuATjY .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-9CcxWQ714luuATjY .arrowheadPath{fill:#333333;}#mermaid-svg-9CcxWQ714luuATjY .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-9CcxWQ714luuATjY .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-9CcxWQ714luuATjY .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-9CcxWQ714luuATjY .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-9CcxWQ714luuATjY .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-9CcxWQ714luuATjY .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-9CcxWQ714luuATjY .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-9CcxWQ714luuATjY .cluster text{fill:#333;}#mermaid-svg-9CcxWQ714luuATjY .cluster span{color:#333;}#mermaid-svg-9CcxWQ714luuATjY div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-9CcxWQ714luuATjY .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-9CcxWQ714luuATjY rect.text{fill:none;stroke-width:0;}#mermaid-svg-9CcxWQ714luuATjY .icon-shape,#mermaid-svg-9CcxWQ714luuATjY .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-9CcxWQ714luuATjY .icon-shape p,#mermaid-svg-9CcxWQ714luuATjY .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-9CcxWQ714luuATjY .icon-shape .label rect,#mermaid-svg-9CcxWQ714luuATjY .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-9CcxWQ714luuATjY .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-9CcxWQ714luuATjY .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-9CcxWQ714luuATjY :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 返回1: 抢到
返回0: 售罄
返回-1: 已抢
用户请求秒杀
Lua 脚本

原子操作
发送 MQ 消息

异步创建订单
返回'已售罄'
返回'已参与'
MQ 消费者

写入数据库订单
返回用户

秒杀结果

关键设计原则

  1. 库存扣减放在 Redis:原子操作,绝不超卖
  2. 订单创建异步化:MQ 削峰,不阻塞秒杀响应
  3. 前端防重:按钮置灰 + 验证码
  4. 网关限流:在秒杀接口上再加一层限流

四、Session 共享

4.1 问题

分布式系统中,用户请求可能被负载均衡到不同服务器。如果 Session 存在各服务器的本地内存中,用户会频繁"被登出"。

4.2 Spring Session + Redis 方案

xml 复制代码
<!-- pom.xml -->
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
</dependency>
yaml 复制代码
# application.yml
spring:
  session:
    store-type: redis
    redis:
      namespace: spring:session
    timeout: 30m  # Session 过期时间
  data:
    redis:
      host: localhost
      port: 6379
java 复制代码
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
public class SessionConfig {

    // 自定义 Session 序列化(可选)
    @Bean
    public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
        return new GenericJackson2JsonRedisSerializer();
    }

    // Cookie 配置
    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer serializer = new DefaultCookieSerializer();
        serializer.setCookieName("SESSIONID");
        serializer.setCookiePath("/");
        serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
        return serializer;
    }
}

使用方式完全不变,HttpSession 自动存入 Redis:

java 复制代码
@RestController
public class LoginController {

    @PostMapping("/login")
    public String login(@RequestParam String username, 
                        HttpSession session) {
        // 自动存入 Redis
        session.setAttribute("user", username);
        return "OK";
    }

    @GetMapping("/user")
    public String getUser(HttpSession session) {
        // 自动从 Redis 读取
        return (String) session.getAttribute("user");
    }
}

五、附近的人(Geo 实现)

java 复制代码
@Service
public class NearbyService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String LOCATION_KEY = "user:locations";

    /**
     * 更新用户位置
     */
    public void updateLocation(String userId, double lng, double lat) {
        redisTemplate.opsForGeo().add(LOCATION_KEY, 
            new Point(lng, lat), userId);
    }

    /**
     * 查找附近的人
     */
    public List<NearbyUser> findNearby(String userId, double radiusKm, int limit) {
        // GeoSearchArgs 需要 Lettuce 或 Redisson 的原生 API
        // 这里用通用 RedisTemplate 方式
        
        // 先获取目标用户位置
        List<Point> points = redisTemplate.opsForGeo()
            .position(LOCATION_KEY, userId);
        if (points == null || points.isEmpty()) return Collections.emptyList();
        
        Point myPoint = points.get(0);
        return findNearbyByPoint(myPoint.getX(), myPoint.getY(), radiusKm, limit);
    }

    /**
     * 按坐标查找附近的人(Redis 6.2+)
     */
    public List<NearbyUser> findNearbyByPoint(double lng, double lat, 
                                               double radiusKm, int limit) {
        // GEOSEARCH 是 Redis 6.2+ 的命令
        // 如果不支持,可以用 GEORADIUS
        Distance radius = new Distance(radiusKm, Metrics.KILOMETERS);
        
        RedisGeoCommands.GeoRadiusCommandArgs args = 
            RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs()
                .includeDistance()
                .includeCoordinates()
                .sortAscending()
                .limit(limit);

        GeoResults<RedisGeoCommands.GeoLocation<String>> results = 
            redisTemplate.opsForGeo().radius(LOCATION_KEY,
                new Circle(new Point(lng, lat), radius), args);

        if (results == null) return Collections.emptyList();

        return results.getContent().stream()
            .map(geo -> new NearbyUser(
                geo.getContent().getName(),
                geo.getDistance().getValue(),
                geo.getContent().getPoint().getX(),
                geo.getContent().getPoint().getY()
            ))
            .collect(Collectors.toList());
    }

    @Data
    @AllArgsConstructor
    public static class NearbyUser {
        private String userId;
        private double distanceKm;
        private double lng;
        private double lat;
    }
}

六、消息已读未读(Bitmap)

java 复制代码
@Service
public class MessageReadService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 发送消息:为每个用户创建消息的已读标记位(初始为 0)
     */
    public void sendMessage(long messageId, List<Long> receiverIds) {
        for (Long userId : receiverIds) {
            // 初始化为未读(0),GETBIT 获取不存在的位置返回 0,所以不显式设置也行
        }
    }

    /**
     * 标记已读
     */
    public void markAsRead(long userId, long messageId) {
        String key = "msg:read:" + userId;
        redisTemplate.opsForValue()
            .setBit(key, messageId, true);
    }

    /**
     * 检查是否已读
     */
    public boolean isRead(long userId, long messageId) {
        String key = "msg:read:" + userId;
        Boolean bit = redisTemplate.opsForValue().getBit(key, messageId);
        return Boolean.TRUE.equals(bit);
    }

    /**
     * 获取未读消息数量(群聊场景)
     * 总消息数 - 已读数
     */
    public long getUnreadCount(long userId, long totalMessages) {
        String key = "msg:read:" + userId;
        Long readCount = redisTemplate.execute(
            (RedisCallback<Long>) conn -> conn.bitCount(key.getBytes())
        );
        return totalMessages - (readCount == null ? 0 : readCount);
    }

    /**
     * 批量查询多条消息的已读状态
     */
    public Map<Long, Boolean> batchCheckRead(long userId, List<Long> messageIds) {
        String key = "msg:read:" + userId;
        Map<Long, Boolean> result = new HashMap<>();
        for (Long msgId : messageIds) {
            Boolean bit = redisTemplate.opsForValue().getBit(key, msgId);
            result.put(msgId, Boolean.TRUE.equals(bit));
        }
        return result;
    }
}

七、总结

本文六个实战场景的核心技术选型:

场景 数据结构 关键技术
排行榜 Sorted Set ZADD / ZREVRANGE / ZREVRANK
限流器 Sorted Set + Lua 滑动窗口算法
秒杀 String + Set + Lua 原子库存扣减 + MQ 异步下单
Session 共享 String(Hash) Spring Session 自动管理
附近的人 Geo GEOADD / GEOSEARCH
消息已读 Bitmap SETBIT / GETBIT / BITCOUNT

如有疑问或指正,欢迎在评论区交流。

相关推荐
你想考研啊1 小时前
mysql数据库导出导入
数据库·mysql·oracle
cup112 小时前
[开源] Meta Assistant / 告别命令行,我为一堆 Python 脚本做了一个 Windows 任务栏的“家”
windows·python·工具·nuitka·脚本运行
十年编程老舅2 小时前
Linux DRM:底层逻辑与实践架构
数据库·mysql
小小编程路2 小时前
Python 还有容器类型互转、进制转换、字符编码转换
开发语言·windows·python
The Sheep 20233 小时前
Vue复习
linux·服务器·数据库
云边有个稻草人3 小时前
深度解析:KingbaseES高可用架构落地原理与生产运维实战
数据库·读写分离·数据库运维·金仓数据库·国产数据库技术·数据备份恢复
Samooyou3 小时前
RAG项目案例--02在线检索&过滤流水线
人工智能·python·ai·全文检索·检索
动能小子ohhh3 小时前
DocForge平台的设计与开发--文件上传接口的实现
开发语言·人工智能·python·langchain·ocr·fastapi
满天星83035773 小时前
【Qt】信号和槽(二) (自定义信号和槽)
开发语言·数据库·qt