Redis 流式处理列表响应



我们希望从 Redis 获得一个类似于Postgres Streams 的响应

我们有一个限制,即使用尽可能少的服务器内存。我们的一些列表可以是~10MB,并且可以同时从多个用户请求。

以老式的方式执行此操作最终会导致我们服务器上的内存问题。

另一种解决方案是迭代列表并发送lrange命令,每个批次返回列表项的一部分,但由于网络往返,这将效率低下。

这是我现在所拥有的,这是标准的做事方式:

'use strict'
const redis = require('redis'),
client = redis.createClient()
client.on('error', function (err) {
console.log('Error ' + err)
})
client.rpush('foo', 1)
client.rpush('foo', 2)
client.rpush('foo', 3)
client.rpush('foo', 4)
client.rpush('foo', 5)
client.lrange('foo', 0, -1, (err, replies) => {
console.log(replies)
})

Streaming 尚未在 Redis 中实现,因此分页是一种不错的方法。也就是说,List 数据结构不太支持该模式,因为LRANGE是一种昂贵的 (O(N(( 操作。

我建议您考虑使用替代数据结构,例如排序集或 v5 流来存储数据。

最新更新