Design of pooledredisclientmanager for servicestack. redis

Source: Internet
Author: User

Pooledredisclientmanager is the connection pool management class of servicestack. redis. Through the connection pool, you can achieve more efficient redis operations.The getclient design related to pooledredisclientmanager seems to have some problems. If you only direct the pool to one apsaradb for redis, there will be no problems, but if you direct it to multipleRedis may have a tragedy. The following explains how to point to multipleRedis has some problems.

Details Code
 1   ///   <Summary>  2           ///  Called within a lock  3           ///   </Summary>  4           ///  <Returns> </returns>  5           Private  Redisclient getinactivewriteclient ()  6   {  7               VaR Desiredindex = writepoolindex % Writeclients. length;  8               //  This will loop through all hosts in readclients once even though there are 2 for Loops  9               // Both loops are used to try to get the prefered host according to the round robin algorithm  10               For ( Int X = 0 ; X <readwritehosts. Count; X ++ )  11   {  12                   VaR Nexthostindex = (desiredindex + x) % Readwritehosts. count;  13                   VaR Nexthost =Readwritehosts [nexthostindex];  14                   For ( VaR I = nexthostindex; I <writeclients. length; I ++ = Readwritehosts. Count)  15   {  16                       If (Writeclients [I]! = Null &&! Writeclients [I]. Active &&! Writeclients [I]. hadexceptions)  17                           Return Writeclients [I];  18                       Else   If (Writeclients [I] = Null | Writeclients [I]. hadexceptions)  19   {  20                           If (Writeclients [I]! = Null  )  21   Writeclients [I]. disposeconnection (); 22                           VaR Client = Redisclientfactory. createredisclient (nexthost. Host, nexthost. Port );  23   24                           If  (Nexthost. requiresauth)  25 Client. Password = Nexthost. Password;  26   27 Client. ID = redisclientcounter ++ ;  28 Client. clientmanager = This  ;  29 Client. namespaceprefix = Namespaceprefix;  30 Client. connectionfilter = Connectionfilter;  31                           32 Writeclients [I] = Client;  33   34                           Return  Client; 35   }  36   }  37   }  38               Return   Null  ;  39 }
Working Principle

The principle of the above code is very simple, that is, to round-robin available connections under different hosts, if the relevant connection test is available, directly return. If the connection is lost, release and recreate.

Problems

If only one host is used, but multiple hosts are used, you will find that if one of the redis services suffers an exception, the corresponding host will still be rotated, this will causeAll operations on the service are abnormal, and even more tragic is that when a server is blocked, the connection to the corresponding host will be created timeout.ProgramA long wait and an error is reported. This is a very tragic situation for concurrency.

Solution

In fact, nodes can be divided for the host, and each node stores its own connection pool. When the connection operation of the relevant host encounters a network exception, the host should be taken from the currentThis ensures that the operation will be migrated to the normal host immediately.

Set up a host recovery mechanism. pooledredisclientmanager should have an internal mechanism to check the damaged host and run the ping command from connect to redis to check whether the host is normal. If it is normal, the host will be restored immediatelyWithin the cycle range.

Thoughts

The author does not use the stack in the connected storage. In fact, it is easier to use the stack in design and management. This is also because he does not want to have thread synchronization processing overhead in the connection push to the pool.

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.