Nginx + lua + redis

Source: Internet
Author: User
: This article mainly introduces nginx + lua + redis. For more information about PHP tutorials, see. Recently, nginx + lua + redis is used as a system to support high-concurrency and high-traffic applications. During development, I suddenly thought about whether golang could achieve the same effect. So I wrote a simple code comparison.

I will not introduce it more. I will introduce nginx + lua + redis to build highly concurrent applications on the Internet. I am using openresty + lua + redis.

Paste the test results first. the machine is the new low-configuration air (2013 GHz Intel Core i5, 4 GB 1.3 MHz DDR3) released in 1600. the command is as follows:

AB-n 1000-c 100 http: // localhost: 8880/

Openresty + lua + redis: Concurrency Level: 100 Time taken for tests: 0.458 secondsComplete requests: 1000 Failed requests: 0 Total 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, average SS all concurrent requests) Transfer rate: 1469.29 [Kbytes/sec] committed Ed Golang + redis: Concurrency Level: 100 Time taken for tests: 0.503 secondsComplete requests: 1000 Failed requests: 0 Total transferred: 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, average SS all concurrent requests) Transfer rate: 1262.05 [Kbytes/sec] committed Ed Lua code:

-- Configure localparams for redis = {host = '123. 0.0.1 ', port = 6379,} localred = redis: new () localok, err = red: connect (params. host, params. port) ifnotokthenngx. say ("failed to connect:", err) returnendlocalposition_key = ngx. var. position_keylocalcontent = red: get (position_key) ngx. print (content) Golang code:

Packagemainimport ("fmt" "github.com/garyburd/redigo/redis" log "" net/http "" time ") funcgetConn () (redis. conn, error) {conn, err: = redis. dialTimeout ("tcp", ": 6379", 0, 1 * time. second, 1 * time. second) iferr! = Nil {fmt. Println (err)} returnconn, err} funcindexHandler (whttp. ResponseWriter, r * http. Request) {conn, err: = getConn () iferr! = Nil {http. Error (w, err. Error (), http. StatusInternalServerError) return} result, err: = conn. Do ("get", "content_1") iferr! = Nil {http. error (w, err. error (), http. statusInternalServerError) return} fmt. fprintf (w, "Hello, % q", result)} funcmain () {http. handleFunc ("/", indexHandler) err: = http. listenAndServe (": 8880", nil) iferr! = Nil {log. Fatal ("ListenAndServe:", err. Error ())}} After multiple stress tests, we found that the combination of nginx + lua + redis is indeed efficient, and the golang + redis solution is actually not much inferior. Compared with the entire system from development to deployment, golang may be more suitable and more suitable for development. after all, nginx + lua is somewhat awkward for development and testing.

Supplement connection pool usage and test results

After the last test, I thought there was still room for improvement in this code. so I checked how to use the redis connection pool in golang (in fact, it is the use of redigo ), and how to use the redis connection pool in lua (actually rest. redis ).

Result 1:

Openresty + lua + redisConcurrency Level: 100 Time taken for tests: 0.284 secondsComplete requests: 1000 Failed requests: 0 Total transferred: 687000 bytesHTML transferred: 531000 bytesRequests per second: 3522.03 [#/sec] (mean) Time per request: 28.393 [MS] (mean) Time per request: 0.284 [MS] (mean, average SS all concurrent requests) Transfer rate: 2362.93 [Kbytes/sec] committed Ed

Let's look at golang again:

Golang + redisConcurrency Level: 100 Time taken for tests: 0.327 secondsComplete requests: 1000 Failed requests: 0 Total transferred: 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, average SS all concurrent requests) Transfer rate: 1941.44 [Kbytes/sec] committed Ed

Lua code:

-- Configure localparams for redis = {host = '123. 0.0.1 ', port = 6379,} localred = redis: new () localok, err = red: connect (params. host, params. port) ifnotokthenngx. say ("failed to connect:", err) returnendlocalposition_key = ngx. var. position_keylocalcontent = red: get (position_key) ngx. print (content) localok, err = red: set_keepalive (10000,100) ifnotokthenngx. say ("failed to set keepalive:", err) returnend

Golang code:

Packagemainimport ("flag" "fmt" "github.com/garyburd/redigo/redis" "log" "net/http" "runtime" "time") var (pool * redis. poolredisServer = flag. string ("redisServer", ": 6379", "") funcindexHandler (whttp. 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) deferconn. close () result, err: = conn. do ("get", "content_1") iferr! = Nil {http. error (w, err. error (), http. statusInternalServerError) return} fmt. fprintf (w, "Hello, % q", result)} funcnewPool (serverstring) * redis. pool {return & redis. pool {MaxIdle: 3, IdleTimeout: 240 * time. second, Dial: func () (redis. conn, error) {c, err: = redis. dial ("tcp", server) iferr! = Nil {returnnil, err} returnc, err}, TestOnBorrow: func (credis. conn, ttime. time) error {_, err: = c. do ("PING") returnerr },}} funcmain () {runtime. GOMAXPROCS (runtime. numCPU () flag. parse () pool = newPool (* redisServer) http. handleFunc ("/", indexHandler) err: = http. listenAndServe (": 8880", nil) iferr! = Nil {log. Fatal ("ListenAndServe:", err. Error ())}}

In addition to the thread pool, golang also sets the number of cpu cores.

However, this test is not very rigorous. redis, nginx, golang http server, and AB stress tests are all on one machine, which may affect each other. If you are interested, you can deploy and test it separately.

The above introduces nginx + lua + redis, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.