你的 ChatMemory 卡顿?MySQL vs Redis 性能实测

上一篇我实现了 Spring AI ChatMemory 的两种持久化方式:MySQL 和 Redis。

评论区有人问:"到底是哪个快?"

很多博客都说"Redis 比 MySQL 快很多",但到底快多少?

我直接上测试,写了 3 个测试接口,跑了 6000+ 字的性能对比...

结果可能超出你的预期:写入快 60%,读取却慢 6.5 倍!


一、为什么做性能对比?

在实现 ChatMemory 持久化时,我遇到了一个经典选择:

  • MySQL: 生产稳定,数据可查询,支持 ACID
  • Redis: 高性能,支持 TTL,适合缓存

很多博客都说"Redis 比 MySQL 快很多",但到底快多少?

我做了这个测试:

测试环境:

  • CPU: Intel i7
  • 内存: 16GB
  • 磁盘: SSD
  • Spring Boot 3.3.5 + Spring AI 1.0.0

测试工具:

  • 写入测试:/perf/write?count=10000
  • 混合测试:/perf/mixed?writeCount=5000&readEvery=100

二、MySQL 版测试结果

Step 1: 启动 MySQL 版应用

bash 复制代码
CHAT_MEMORY_TYPE=mysql java -jar hello-ai.jar

Step 2: 写入 10000 条消息

访问:http://localhost:8080/perf/write?count=10000

结果:

复制代码
写入 10000 条消息耗时: 2245 ms (0.22 ms/条)

Step 3: 混合测试(写 5000 条,每 100 条读一次)

访问:http://localhost:8080/perf/mixed?writeCount=5000&readEvery=100

结果:

复制代码
混合测试:写入 5000 条,其中读取 50 次
总耗时: 992 ms
写入耗时: 931 ms
读取耗时: 61 ms
平均读取耗时: 1.22 ms/次

详细分析:

操作 耗时 平均耗时
写入 10000 条 2245 ms 0.22 ms/条
写入 5000 条 931 ms 0.19 ms/条
读取 50 次 61 ms 1.22 ms/次

MySQL 的优势:

  • ✅ 事务支持,数据一致性保证
  • ✅ 数据可查询,方便调试
  • ✅ 生产环境稳定可靠

MySQL 的劣势:

  • ❌ 每次写入都要执行 SQL
  • ❌ 需要处理事务提交
  • ❌ 磁盘 I/O 限制性能

三、Redis 版测试结果

Step 1: 启动 Redis 版应用

bash 复制代码
CHAT_MEMORY_TYPE=redis java -jar hello-ai.jar

Step 2: 写入 10000 条消息

访问:http://localhost:8080/perf/write?count=10000

结果:

复制代码
写入 10000 条消息耗时: 898 ms (0.09 ms/条)

Step 3: 混合测试(写 5000 条,每 100 条读一次)

访问:http://localhost:8080/perf/mixed?writeCount=5000&readEvery=100

结果:

复制代码
混合测试:写入 5000 条,其中读取 50 次
总耗时: 1406 ms
写入耗时: 1007 ms
读取耗时: 399 ms
平均读取耗时: 7.98 ms/次

详细分析:

操作 耗时 平均耗时
写入 10000 条 898 ms 0.09 ms/条
写入 5000 条 1007 ms 0.20 ms/条
读取 50 次 399 ms 7.98 ms/次

四、性能对比:写入 vs 读取

写入性能对比:

复制代码
MySQL: 10000 条 → 2245 ms (0.22 ms/条)
Redis: 10000 条 → 898 ms (0.09 ms/条)
差异: Redis 快 60% ⚡

读取性能对比:

复制代码
MySQL: 平均 1.22 ms/次
Redis: 平均 7.98 ms/次
差异: MySQL 快 6.5 倍 🔥

混合测试对比:

测试项 MySQL Redis 胜者
写入 5000 条 931 ms 1007 ms ✅ MySQL
读取 50 次 61 ms 399 ms ✅ MySQL
总耗时 992 ms 1406 ms ✅ MySQL

五、为什么写入快,读取慢?

写入性能:Redis 快 60%

原因:

  • MySQL: 每次写入要执行 SQL,处理事务,磁盘 I/O
  • Redis: 内存写入,JSON 序列化,无磁盘 I/O

读取性能:MySQL 快 6.5 倍

原因:

  • MySQL: 直接从数据页读取,二进制格式
  • Redis: JSON 序列化/反序列化,解析对象,额外 CPU 开销

六、生产选型建议

什么时候用 MySQL?

  • 读取密集型应用
  • 需要查询历史对话
  • 数据需要长期存储和备份
  • 团队熟悉关系型数据库
  • 需要 ACID 事务保证

适合的应用:

  • AI 客服系统,用户频繁咨询
  • 企业内部 AI 助手,需要查询历史记录
  • 数据分析平台,需要导出对话记录

什么时候用 Redis?

  • 写入密集型应用
  • 会话记忆只保留最近 N 条(如 20 条)
  • 需要自动过期(会话超时)
  • 分布式部署,需要共享内存
  • 对写入性能要求高

适合的应用:

  • ChatGPT 类应用,持续对话,频繁写入
  • AI 助手,每轮对话都要更新记忆
  • 聊天机器人,多用户并发,高写入

混合方案:MySQL + Redis

高级方案:MySQL 持久化 + Redis 缓存

java 复制代码
// MySQL 持久化
JdbcChatMemoryRepository mysqlRepo;

// Redis 缓存
RedisChatMemoryRepository redisRepo;

// 写入:先写 MySQL,再写 Redis
redisRepo.saveAll(conversationId, messages);
mysqlRepo.saveAll(conversationId, messages);

// 读取:先读 Redis,没有再读 MySQL
List<Message> messages = redisRepo.findByConversationId(conversationId);
if (messages == null || messages.isEmpty()) {
    messages = mysqlRepo.findByConversationId(conversationId);
    // 回填 Redis
    redisRepo.saveAll(conversationId, messages);
}

优势:

  • MySQL 确保数据持久化
  • Redis 提供高性能读取
  • 灵活应对不同场景

七、如何切换存储后端?

Spring AI ChatMemory 支持两种存储,切换非常简单!

方案 1:配置文件切换

application.yml:

yaml 复制代码
spring:
  ai:
    chat:
      memory:
        type: redis  # mysql 或 redis

  data:
    redis:
      host: localhost
      port: 6379

MySQL 配置:

yaml 复制代码
spring:
  ai:
    chat:
      memory:
        type: mysql  # 默认值

  datasource:
    url: jdbc:mysql://localhost:3308/hello_ai
    username: root
    password: root

方案 2:环境变量切换

启动时设置:

bash 复制代码
# 使用 Redis
CHAT_MEMORY_TYPE=redis java -jar hello-ai.jar

# 使用 MySQL
CHAT_MEMORY_TYPE=mysql java -jar hello-ai.jar

八、总结

核心结论:

指标 MySQL Redis 胜者
写入性能 0.22 ms/条 0.09 ms/条 ✅ Redis 快 60%
读取性能 1.22 ms/次 7.98 ms/次 ✅ MySQL 快 6.5 倍
写入 10000 条 2245 ms 898 ms ✅ Redis

最佳实践:

  1. 写入密集:用 Redis,快 60%
  2. 读取密集:用 MySQL,快 6.5 倍
  3. 混合场景:根据读写比例选择
  4. 长期存储:用 MySQL,数据不丢
  5. 会话超时:用 Redis,TTL 自动清理

如果 ChatMemory 卡顿,试试这些方法:

  1. 切换到 Redis:写入性能提升 60%
  2. 调小 maxMessages:减少数据量,如从 50 改为 20
  3. 优化 SQL 索引:MySQL 加索引,查询更快
  4. 批量写入:减少事务次数,提升吞吐量
  5. 使用连接池:JDBC/HikariCP,减少连接开销

下一篇预告:ChatMemory 分布式部署方案

关注我,获取更多 Spring AI 开发干货!


写在最后

我是一名 8年 Java 后端,正在转型 AI 应用开发。Spring AI 系列会持续更新,从 hello world 到 RAG 到 Agent,一路踩坑一路写。

如果你也在转型 AI,关注我,一起走。有问题评论区聊,我会逐条回复。

如果这篇文章帮到了你,点个赞就是对我最大的鼓励 ❤️