本文介绍了当一次请求很多时,Redis 不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Node.js 应用程序,我正在尝试使用 Redis 缓存来计算发出的请求数量.这只是一个概念证明,看看 Redis 是否适合我,但我对结果有点失望,我想知道 Redis 的性能是否真的很差,或者我的代码是否存在缺陷.

I have a Node.js application and I am trying to use Redis cache to keep count of the amount of requests made. This is just a proof of concept to see if Redis is the right tool for me, but I am a bit disappointed by the outcome and I'm wondering if Redis is just performing really badly or if there is a flaw in my code.

我有以下代码:

const express = require('express')
const Redis = require('ioredis')

const client = new Redis(6379, 'redis');

client.on('connect', function() {
    console.log('Redis client connected');
});

client.on('error', function (err) {
    console.log('Something went wrong ' + err);
});

const getRedisCount = async (question_id) => {
    const count = await client.get('votes_' + question_id);

    if(count) {
        return count;
    }

    await setRedisCount(question_id, 0)
    return 0;
}

const setRedisCount = async (question_id, count) => {
    await client.set('votes_' + question_id, count)
}

const app = express();
app.use(express.json());

// post a vote
app.post('/vote', async (req, res) => {
    let count = await getRedisCount(req.body.question_id)
    count++
    await setRedisCount(req.body.question_id, count)

    res.status(200).send({ status: 200, data: count });

})

app.listen(5000, () => {
    console.log('Vote service listening on port 5000')
})

现在,当我只在 Postman 中执行单个发布请求时,这一切都按预期工作.计数器按预期更新.

Now this all works as expected when I just do a single post request in Postman. the counter updates as expected.

但是当我在 JMeter 中运行一次测试时,我一次执行 150 个请求,在最后一个请求之后我得到大约 20-30 个计数,而不是预期的 150 个.

But when I run a test in JMeter where I do 150 requests at once, I get a count of around 20-30 after the last request, instead of the expected 150.

所以我的问题是 - 这是预期的行为,还是我的代码中的错误?在这两种情况下 - 如何解决?

So my question is - is this expected behavior, or is this a fault in my code ? and in both cases - how to fix it ?

推荐答案

这可能与您的 redis 服务器配置有关.

This maybe something to do with your redis server config.

您可以从主机上的 redis cli 运行 MONITOR.

You can run MONITOR from the redis cli on your host machine.

通常您通过 cli 进行连接,例如:

Usually you connect via the cli for example:

https://redis.io/topics/rediscli

redis-cli -h <your server ip> -p <your server port> -a <if you have a server password enabled>

$ redis-cli -h host -p port -a password

默认端口为 6379

这将显示与 redis 服务器的所有交互,但是不要运行它,因为总是运行它会影响性能,但非常适合故障排除.

This will show all of the interactions with the redis server, however don't run this as always running it has a performance hit but great for troubleshooting.

https://redis.io/commands/monitor

 $ redis> monitor
 1339518083.107412 [0 127.0.0.1:60866] "keys" "*"
 1339518087.877697 [0 127.0.0.1:60866] "dbsize"
 1339518090.420270 [0 127.0.0.1:60866] "set" "x" "6"
 1339518096.506257 [0 127.0.0.1:60866]

这应该可以告诉您问题所在.

This should give you an indication of the issue.

您还可以从 redis cli 运行 INFO 命令以查看服务器统计信息和内存使用情况等信息.

You can also run the INFO command from the redis cli to see server stats and information such as memory usage.

https://redis.io/commands/info

redis> INFO
 # Server 
 redis_version 999.999.999
 redis_git_sha1:3c968ff0
 redis_git_dirty:0
 redis_build_id:51089de051945df4
 redis_mode:standalone 
 os:Linux 4.8.0-1-amd64 x86_64 
 arch_bits:64 
 multiplexing_api:epoll
 atomicvar_api:atomic-builtin 
 gcc_version:6.3.0
 process_id:8394
 </snip>

还要在代码中使用来自 redis 客户端的 INCRBY 命令,否则需要在设置计数值之前增加计数值,这与异步模式不一致.

Also use the INCRBY command from your redis client in your code otherwise will need to increment your count value before setting it which is not consistent with the async pattern.

https://redis.io/commands/incrby

我认为您的问题是因为这是异步的,对吗?

I think your problem is because this is asynchronous right?

因此,当它们被发布时,计数是不同步的,因为有些可能在其他人之前等待.

So the counts are out of sync when they get posted since some may be awaited before others are.

无论哪种方式,您都可能想尝试同步发布它们以查看是否有所不同,那么您就会知道这是问题所在,最好为此目的使用 redis INCR 命令.

Either way you mighty want to try and post them synchronous to see if that makes a difference then you know that is the issue and would be better off using the redis INCR commands for this purpose.

这篇关于当一次请求很多时,Redis 不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 02:13