记一次 redis cluster 事务 (transaction) 翻车及分析总结
· 阅读需 11 分钟
发现生产环境的业务报了好多错误, 涉及的 Node.js 代码是一个基于 Redis 的频率计数器,那部分逻辑大概是这样
// 查询并增加一次计数
async incr (id) {
const key = `${this.namespace}:${id}`
const now = getMicrotime()
const start = now - this.duration * 1000
const operations = [
['zremrangebyscore', key, 0, start],
['zcard', key],
['zadd', key, now, now],
['pexpire', key, this.duration]
]
const res = await this.redis.multi(operations).exec()
const count = toNumber(res[1][1])
return count
}
错误是:
Cannot read property '1' of undefined