Sync or Async connect Redis in Golang

Source: Internet
Author: User
Tags redis server
This is a creation in Article, where the information may have evolved or changed. # <span style= "Font-family:microsoft Yahei" >head of head</span> in Golang's entire ecosystem, Redis client Lib all uses a multiple connection or connection pool. It's hard to understand, so I wrote with Xiaofei a Redis client lib:redisgo-async that supports both synchronous and asynchronous. GitHub Address: Https://github.com/gistao/RedisGo-Async. QQ Group: 131958277. &nbsp;# <span style= "Font-family:microsoft yahei" > Sync mode </span>a-&gt; BA &lt;-BA requests, and obtains the result, through 1 RTT, which is called synchronous mode. In order to achieve a high QPS, M-AB is required to handle the task, so that a formula can be obtained: M/rtt. &nbsp;# <span style= "Font-family:microsoft Yahei" >[] (https://github.com/gistao/RedisGo-Async/wiki/ async-or-sync-%ef%bc%88%e4%b8%ad%e6%96%87%ef%bc%89#%e5%bc%82%e6%ad%a5%e6%a8%a1%e5%bc%8f) Asynchronous Mode </span>a- &gt; -&gt; -&gt; BA &lt;-&lt;-&lt;-BA requests, does not wait for an answer to continue the request, and gets the full result, through 1 RTT, which is called the asynchronous Pattern. In order to achieve high QPS, you can also get a formula: C,c represents the contract frequency. &nbsp;# <span style= "Font-family:microsoft Yahei" >[] (https://github.com/gistao/RedisGo-Async/wiki/ ASYNC-OR-SYNC-%EF%BC%88%E4%B8%AD%E6%96%87%EF%BC%89#%E5%A4%8D%E6%9D%82%E5%BA%A6) Complexity </span>Asynchronous patterns generally use callbacks, which are much more complex than synchronous methods. But the two modes provided by Goredis-async are used the same way. <div class= "Cnblogs_highlighter" ><pre class= "brush:go;gutter:true;" >conn: = Pool. Get () ret, ERR: = conn. Do () dosomething (ret) ' &nbsp; ' </pre></div> when you want to switch between two modes, the code doesn't have to be changed. &nbsp; notable is pipelining, the use of synchronous mode is as follows <div class= "Cnblogs_highlighter" ><pre class= "brush:go;gutter:true;" >conn. Send (A) Conn. Send (B) Conn. Flush () Conn. Recive ()//RET &lt;-aconn.recive ()//RET &lt;-B</pre></div> asynchronous mode is naturally supported pipelining, so use or <div class= "Cnblogs_highlighter" ><pre class= "brush:go;gutter:true;" >conn. Do (A) Conn. Do (B) </pre></div>&nbsp; Sometimes you will want to request the Redis servera,b,c at the same time, and finally deal with the results at once. The synchronization mode is used as follows <div class= "Cnblogs_highlighter" ><pre class= "brush:go;gutter:true;" >conna.send () Conna.flush () Connb.send () Connb.flush () Connc.send () Connc.flush () conna.recive () connB.Recive () Connc.recive () </pre></div> Async mode uses the following <div class= "Cnblogs_highlighter" ><pre class= "BRUSH:GO; Gutter:true; " &gT;reta: = Conna.asyncdo () Retb: = Connb.asyncdo () retc: = Connc.asyncdo () reta.get () Retc.get () Retb.get () </pre> </div># <span style= "Font-family:microsoft Yahei" >[] (https://github.com/gistao/RedisGo-Async/wiki/ ASYNC-OR-SYNC-%EF%BC%88%E4%B8%AD%E6%96%87%EF%BC%89#%E6%89%A9%E5%B1%95%E6%80%A7) Extensibility </span>## <span Style = "Font-family:microsoft Yahei" >[] (https://github.com/gistao/RedisGo-Async/wiki/async-or-sync-%EF%BC%88%E4%B8 %AD%E6%96%87%EF%BC%89#%E5%90%8C%E6%AD%A5%E6%A8%A1%E5%BC%8FMRTT) sync mode:m/rtt</span> in Goredis-async, M for < Div class= "Cnblogs_highlighter" ><pre class= "brush:go;gutter:true;" >Pool.MaxActive</pre></div> This value is fixed, depending on the current test environment: 1. The network RTT2 of the application and Redis server. The concurrency level in the application is 3. The maximum number of connections for Redis server is 4. The maximum number of connections on this machine is 5. The number of CPU cores for this machine. &nbsp; for an example of RTT: The test environment has a network RTT of 1ms, and after the application has been tested to the QPS requirements, M is identified in the code, then goes online to the production environment, and the production environment has a RTT of 10ms, then the production environment will be 10 times times lower than expected QPS. Or in a production environment, when the target Redis server fails and is cut to the standby machine, the standby machine and the application are likely to cross the engine room, which also poses the same problem. &nbsp;## <span style= "Font-family:microsoft YaheI ">[" (https://github.com/gistao/RedisGo-Async/wiki/async-or-sync-%EF%BC%88%E4%B8%AD%E6%96%87%EF%BC%89#%E5% BC%82%E6%AD%A5%E6%A8%A1%E5%BC%8FC) asynchronous mode:c</span> in Goredis-async, C is only related to the routine number of applications, and the RTT problem described above does not exist in asynchronous mode. Of course, both synchronous and asynchronous mode choices are appropriate for applications that can be accepted within a specific range. &nbsp;# <span style= "Font-family:microsoft Yahei" >[] (https://github.com/gistao/RedisGo-Async/wiki/ Async-or-sync-%ef%bc%88%e4%b8%ad%e6%96%87%ef%bc%89#mmmin-cost-max-payload) MM (Min cost max payload) </span> An important measure of the quality of the base library is mm. Synchronous mode theoretically, more threads and connection resources are required to compare asynchronous patterns. Below is the comparison of Redisgo-async, [Redigo] (Https://github.com/garyburd/redigo), the official Redisclient [Benchmark report] (https://github.com/ Gistao/redisgo-async/blob/master/bench/benchresult.txt). The y-axis is the time-consuming ms,x axis for each comparison library, with test data of 10 million. [] (https://github.com/gistao/RedisGo-Async/raw/master/bench/bench.png) 249 reads  
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.