简体   繁体   English

如何使用StackExchange.Redis解决客户端的Redis超时问题?

[英]How to solve a Redis timeout on client side with StackExchange.Redis?

I'm using StackExchange.Redis client in ac# wcf application : I use only the synchronous command to get and set values. 我在ac#wcf应用程序中使用StackExchange.Redis客户端:我仅使用sync命令来获取和设置值。 The problem is I have a timeout with this curious log : 问题是我有一个好奇的日志超时:

Timeout performing EXISTS DataKey:50, 
inst: 1, queue: 1, qu: 0, qs: 1, qc: 0, wr: 0, wq: 0, in: 0, ar: 0, clientName: Machine1, 
serverEndpoint: redis-server:6381, keyHashSlot: 7984, 
IOCP: (Busy=1,Free=799,Min=8,Max=800), 
WORKER: (Busy=5,Free=795,Min=8,Max=800) 
(Please take a look at this article for some common client-side issues that can 
cause timeouts: http://stackexchange.github.io/StackExchange.Redis/Timeouts) 

If I understand that correctly it means that my get value is queued because there is five worker threads ? 如果我正确理解,那意味着我的get值已排队,因为有五个工作线程? Using netstat, I see that my application is opening two physical connections to the server . 使用netstat,我看到我的应用程序正在打开到服务器的两个物理连接。 I have made sure to have enought threads available in my threadpool. 我确保线程池中有足够的可用线程。 In my connection settings I have a syncTimeout=3000... If I use the redis-cli, I could get the value of the key in 0.64 secondes. 在我的连接设置中,我有一个syncTimeout = 3000 ...如果使用redis-cli,则可以在0.64秒内获得密钥的值。

Can anyone help please? 有人可以帮忙吗? What can I do? 我能做什么? Does I have to use async in my code all the way or find another redis client lib ? 我是否必须一直在代码中使用async或找到另一个redis客户端库?

There are a few solutions. 有一些解决方案。 First of all they have a writeup on the github repo about the solutions they propose, found here . 首先,他们在github仓库上有关于他们提出的解决方案的文章,可在此处找到。

What I've found though is just use the async functions as much as you can, it is only the synchronous functions that timeout. 我发现,尽管您会尽可能多地使用异步功能,但只有同步功能会超时。 It seems to me that the timeouts are a design decision on their part based on the idea that they don't want to block your code if something goes wrong. 在我看来,超时是他们自己的设计决策,是基于这样的想法:如果出现问题,他们不想阻塞您的代码。 I don't buy that, and so the work around I use is to use the async functions and then wait for the task. 我不购买,所以我使用的解决方法是使用异步功能,然后等待任务。 So instead of db.StringGet("thestring") I just do db.StringGetAsync("thestring").Result . 因此,我只是执行db.StringGetAsync("thestring").Result而不是db.StringGet("thestring") db.StringGetAsync("thestring").Result That is, if I cannot use await for whatever reason. 也就是说,无论出于何种原因我都无法使用await。

You might want to look into using async/await as much as possible anyway. 无论如何,您可能想尽可能多地使用异步/等待。 You might also want to use FireAndForget if you that is appropriate. 如果合适的话,您可能还想使用FireAndForget。 You can also use ContinueWith. 您也可以使用ContinueWith。 They talk about those solutions here . 他们在这里谈论这些解决方案。

From "WORKER: (Busy=5,Free=795,Min=8,Max=800)", I see that there are 5 Busy worker threads and a minimum of only 8 worker threads. 从“工作人员:(忙碌= 5,免费= 795,最小= 8,最大= 800)”中,我看到有5个忙碌工作线程,最少只有8个工作线程。 I suspect that if you look at the whole set of errors that you received, you might find that Busy is greater than the Min count in some cases. 我怀疑如果查看收到的全部错误,在某些情况下可能会发现“忙”大于“最小”计数。 This would indicate threadpool throttling . 这将表明线程池正在节流 The link also provides some solutions on increasing the Min Thread counts for Worker and IOCP threads. 该链接还提供了一些有关增加Worker和IOCP线程的最小线程数的解决方案。

fixing RedisTimeout exception is always a challenge, there any many factors that cause timeout Exception, 修复RedisTimeout异常总是一个挑战,有许多因素导致超时异常,

in your log you can see for IOCP thread there are 1 busy threads and your system is configured to allow 8 minimum threads. 在您的日志中,您可以看到IOCP线程有1繁忙线程,并且您的系统配置为允许8最小线程。 so it's not the case because 1 is not > 8 所以不是这样,因为1不是> 8

once I had the same situation and I fixed timeout exceptions by increasing syncTimeout in the connection string, you could try to increase it 一旦遇到相同的情况,并通过增加连接字符串中的syncTimeout来修复超时异常,则可以尝试增加它

I also realized that number of open connections affect Redis performance, you can look at open connections by running this command 我还意识到打开的连接数会影响Redis的性能,您可以通过运行以下命令查看打开的连接

info clients

I had around 90 open connections at the time that timeout exception was thrown, I restarted the server to reset open connection and timeout exceptions disappeared 抛出超时异常时,我大约有90个打开的连接,我重新启动服务器以重置打开的连接,并且超时异常消失了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM