Nginx+lua+redis

Source: Internet
Author: User
Tags intel core i5
A system has recently been used with Nginx+lua+redis to support high-concurrency high-traffic applications. Development when suddenly thought Golang can also achieve the same effect. So I wrote a simple code comparison.

Specifically not much to do introduction, online a lot about Nginx+lua+redis building high-concurrency application introduction. I'm using the Openresty+lua+redis.

The test results are first posted, and the machine is the new low-air--(1.3 GHz Intel Core i5, 4 GB @ DDR3) in 2013, with the command:

Ab-n 1000-c http://localhost:8880/

Openresty+lua+redis:concurrency Level:100time taken for tests:0.458 secondscomplete requests:1000failed Requests:0tot Al transferred:689000 byteshtml transferred:533000 bytesrequests per second:2183.67 [#/sec] (mean) time per request:45. 794 [MS] (mean) time per request:0.458 [MS] (mean, across all concurrent requests) Transfer rate:1469.29 [kbytes/sec] Rece Ived Golang+redis:concurrency Level:100time taken for tests:0.503 secondscomplete requests:1000failed requests:0total Trans ferred:650000 byteshtml transferred:532000 bytesrequests per second:1988.22 [#/sec] (mean) time per request:50.296 [MS] (mean) Time per request:0.503 [MS] (mean, across all concurrent requests) Transfer rate:1262.05 [Kbytes/sec] Received Lua Code:

--Redis configuration Local params = { Host = ' 127.0.0.1 ' , Port = 6379 , } Local Red = Redis : New () Local OK , Err = Red : Connect ( params . Host , params . Port ) if not OK Then Ngx . say ( " failed to connect: " , Err ) return End Local Position_key = Ngx . var . Position_key Local content = Red : Get ( Position_key ) Ngx . Print ( content ) Golang Code:

Package Main Import ( "FMT" "Github.com/garyburd/redigo/redis" "Log" "Net/http" "Time" ) func Getconn () ( Redis . Conn , Error ) { Conn , Err := Redis . Dialtimeout ( "TCP" , ": 6379" , 0 , 1 * Time . Second , 1 * Time . Second ) if Err != Nil { FMT . Println ( Err ) } return Conn , Err } func Indexhandler ( W http . Responsewriter , R * http . Request ) { Conn , Err := Getconn () if Err != Nil { http . Error ( W , Err . Error (), http . Statusinternalservererror ) return } result , Err := Conn . Do ( "Get" , "Content_1" ) if Err != Nil { http . Error ( W , Err . Error (), http . Statusinternalservererror ) return } FMT . fprintf ( W , "Hello,%q." , result ) } func Main () { http . Handlefunc ( "/" , Indexhandler ) Err := http . Listenandserve ( ": 8880" , Nil ) if Err != Nil { Log . Fatal ( "Listenandserve:" , Err . Error ()) } } After many times the test found that Nginx + LUA + Redis combination is really efficient, Golang + Redis scheme is actually not much difference. Compared to the whole system from development to deployment, Golang may be more appropriate, more in line with the development of the habit, after all, Nginx + LUA, this scenario development and testing is slightly awkward.

Supplemental Connection pool usage and test results

After the last test, I found that the code still has room for improvement, so I looked up how to use Redis connection pool in Golang (in fact, Redigo use), and how to use the Redis connection pool in Lua (in fact, the use of Rest.redis).

The results first:

Openresty + Lua + redisconcurrency level:100time taken for tests:0.284 secondscomplete requests:1000failed requests:0t Otal transferred:687000 byteshtml transferred:531000 bytesrequests per second:3522.03 [#/sec] (mean) time per Request:2 8.393 [MS] (mean) time per request:0.284 [MS] (mean, across all concurrent requests) Transfer rate:2362.93 [Kbytes/sec] Re ceived

See Golang again:

Golang + redisconcurrency Level:100time taken for tests:0.327 Secondscomplete requests:1000failed Requests:0total Tran sferred:650000 byteshtml transferred:532000 bytesrequests per second:3058.52 [#/sec] (mean) time per request:32.696 [ms ] (mean) time per request:0.327 [MS] (mean, across all concurrent requests) Transfer rate:1941.44 [Kbytes/sec] Received

Lua Code:

--Redis configuration Local params = { Host = ' 127.0.0.1 ' , Port = 6379 , } Local Red = Redis : New () Local OK , Err = Red : Connect ( params . Host , params . Port ) if not OK Then Ngx . say ( " failed to connect: " , Err ) return End Local Position_key = Ngx . var . Position_key Local content = Red : Get ( Position_key ) Ngx . Print ( content ) Local OK , Err = Red : set_keepalive ( 10000 , - ) if not OK Then Ngx . say ( " failed to set keepalive: " , Err ) return End

Golang Code:

Package Main Import ( "Flag" "FMT" "Github.com/garyburd/redigo/redis" "Log" "Net/http" "Runtime" "Time" ) var ( Pool * Redis . Pool Redisserver = Flag . String ( "Redisserver" , ": 6379" , "" ) ) func Indexhandler ( W http . Responsewriter , R * http . Request ) { T0 := Time . Now () Conn := Pool . Get () T1 := Time . Now () FMT . Printf ( "The call took%v to run.\n" , T1 . Sub ( T0 )) defer Conn . Close () result , Err := Conn . Do ( "Get" , "Content_1" ) if Err != Nil { http . Error ( W , Err . Error (), http . Statusinternalservererror ) return } FMT . fprintf ( W , "Hello,%q." , result ) } func Newpool ( Server string ) * Redis . Pool { return & Redis . Pool { Maxidle : 3 , IdleTimeout : - * Time . Second , Dial : func () ( Redis . Conn , Error ) { C , Err := Redis . Dial ( "TCP" , Server ) if Err != Nil { return Nil , Err } return C , Err }, Testonborrow : func ( C Redis . Conn , T Time . Time ) Error { _ , Err := C . Do ( "PING" ) return Err }, } } func Main () { Runtime . Gomaxprocs ( Runtime . numcpu ()) Flag . Parse () Pool = Newpool ( * Redisserver ) http . Handlefunc ( "/" , Indexhandler ) Err := http . Listenandserve ( ": 8880" , Nil ) if Err != Nil { Log . Fatal ( "Listenandserve:" , Err . Error ()) } }

In addition to the thread pool in Golang, the number of CPU cores is also set.

However, this test is not very rigorous, Redis,nginx,golang HTTP server,ab pressure measurement are in a machine, each other will have an impact. Interested can be deployed separately under test.

The above describes the Nginx+lua+redis, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.