Redis is an open-source, high-performance Key-value storage engine.
Recently, we used redis to replace the previous memcache on a website with a daily access volume of about 1 kW, successfully dropping the CPU from 30% to 15%, and the effect was quite remarkable.
ServiceStackRedis is one of the most popular C # drivers. For more information about how to use ServiceStackRedis, see here -- use ServiceStackRedis to link to Redis
However, we still encountered many problems when using the ServiceStackRedis thread pool (PooledRedisClientManager.
1. The number of links is incorrect.
A webserver occupies 80 links. When there are more than 15 webservers, some clients may not be connected.
Solution:
In the GetInActiveWriteClient method
// Find the next target
// Start searching after the current read/write pointer, instead of starting from 0
Var nextIndex = (WritePoolIndex + I) % writeClients. Length;
Change
Var nextIndex = I;
Modify the DisposeClient method to set readClient. Active = false to DisposeConnection. The thread can be recycled.
Effect:
In a website like ours, a single webserver will occupy about 10 ~ 15 links are much less than the previous 80 links.
Analysis:
From the code point of view, the author's original intention is to find idle threads faster, but recognizes that all threads are continuously used, and no thread may be idle.
If the site is small, there are not many webservers, and the problem is not big if you don't change it. However, I think it is not cost-effective to use long links, because establishing a link with redis is still relatively "cheap.
More than two redis instances store the same content.
The same content will be redundant in all redis
Solution
Add the int type parameter to GetInActiveWriteClient to identify the redis instance.
Var start = 0;
Var step = 1;
If (index>-1 & index <ReadWriteHosts. Count)
{
Start = index;
Step = ReadWriteHosts. Count;
}
// Traverse the read/write pool
// The pool is locked at this time
For (var I = start; I <writeClients. Length; I ++ = step)
{
Omitted
In this way, the thread pool is allocated sequentially according to the number of ReadWriteHosts.
Effect:
When reading and writing, you only need to use the key. GetHashCode method to obtain a hash value, which can be accurately allocated to one of redis. Ensure that all redis data is not duplicated