记一次SSE数据被缓存导致实时性失效问题

Hi I'm Shendi

最近在编写SSE接口遇到了缓存问题,导致实时性失效,在这里记录一下。


记一次SSE数据被缓存导致实时性失效问题


问题

环境:SpringBoot+Nginx+AnythingLLM

SSE接口调用拥有缓存,导致数据被缓存后才返回给前端。

排查

对SpringBoot接口的检查:

检查接口代码,直接使用 resp.OutputStream 或者使用 SseEmitter 皆有此问题

检查接口中对AnythingLLM调用的代码,发现使用BufferedReader.readLine,将其直接改为InputStream.read判断\n,效果的确好了一点,但依然有此问题。

编写测试代码,看是否是接口问题:

java 复制代码
@Anonymous
@GetMapping(value = "/test", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public void test(HttpServletResponse resp) throws Exception {
    resp.setContentType("text/event-stream;charset=UTF-8");
    resp.setCharacterEncoding("UTF-8");
    // 禁止压缩,防止流式被缓存
    resp.setHeader("Content-Encoding", "identity");
    resp.setHeader("Cache-Control", "no-cache");

    OutputStream output = resp.getOutputStream();

    for (int i = 0; i < 20; i++) {
        output.write(("data: " + RespCode.toResp(RespCode.PARAM_NULL) + "\n\n").getBytes());
        output.flush();
        Thread.sleep(2000);
    }
}

测试发现无问题,前端稳定的两秒收到数据(皆使用Nginx反向代理)

对 AnythingLLM的检查:

直接调用接口,发现问题复现,然后猜测是否Nginx的问题,不使用代理访问,一切正常。

解决

最终的问题就是Nginx的问题,Nginx默认会将数据进行缓存,要进行相应配置,并且SSE接口就那么几个,所以可以使用rewrite 对单独的几个接口关闭

sh 复制代码
location /llm/ {
    rewrite ^/llm/(.*)$ /api/$1 break;
    # 原有host 与 ip
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    # 支持websocket
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    proxy_pass   http://127.0.0.1:3001;
}
location ~ ^/llm/v1/workspace/[^/]+/thread/[^/]+/stream-chat$ {
    rewrite ^/llm/(.*)$ /api/$1 break;

    proxy_pass   http://127.0.0.1:3001;

    # 关闭缓冲,确保实时转发
    proxy_buffering off;
    proxy_cache off;

    # 设置 HTTP/1.1 以保持长连接
    proxy_http_version 1.1;
    proxy_set_header Connection '';

    # 防止 gzip 压缩,避免 chunked 被打包
    gzip off;

    # 增加超时时间,避免 SSE 长连接被断开
    proxy_read_timeout 3600s;
    proxy_send_timeout 3600s;

    # 如果后端是基于 Host 做路由的,保留原 Host
    proxy_set_header Host $host;

    # 转发其他头信息
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

END

相关推荐
全栈弄潮儿²⁰²⁴2 天前
AI Agent 开发实战(30):限流、缓存与成本控制
人工智能·gpt·缓存·agent·限流·agi·成本控制
Together_CZ3 天前
DeepSeek-V4.1-Flash:Pushing the Limits of KV Cache Compression——推动 KV 缓存压缩的极限
缓存·llm·compression·kv cache·deepseek·v4.1-flash·推动 kv 缓存压缩的极限
Pioneer000013 天前
我用 Redis + 网关做多模型 API 路由:缓存命中率 95%+ 的工程实践
人工智能·redis·后端·缓存·性能优化·架构
hweiyu004 天前
Redis命令:MSETNX
redis·缓存
wdfk_prog4 天前
ROS教程08:从 TransportTCP::connect() 追到 TCPROS Connection Header、序列化与 Socket 数据传输
运维·缓存·docker·容器·ros
是Dream呀4 天前
Harness 工程:让 Agent 真正把任务做完
人工智能·分布式·缓存·agent
努力努力再努力wz4 天前
【Redis进阶系列】:从主从复制到 Sentinel,一文建立故障检测、Leader 选举与 Failover 的完整心智模型
数据库·redis·缓存
wdfk_prog5 天前
ROS教程10:从 gtest 到 rostest——Unit Test、Node 集成测试、rosbag 与 rqt 验证闭环
运维·缓存·docker·容器·ros
程序猿乐锅6 天前
【黑马点评 | 第四篇】Redis缓存雪崩
java·数据库·spring boot·redis·缓存
HanhahnaH6 天前
Redis单线程和Tair多线程架构设计对比
分布式·缓存