Problems and fixes of ServiceStack. Redis

Source: Internet
Author: User
Tags redis
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.