Skip to content

使用 Redis 快取

乾太 edited this page Aug 23, 2021 · 1 revision

使用 Redis 快取

由於我們將系統架構改為隊列(Queue)的方式,Laravel 的隊列(Queue)功能允許你將一個耗時的任務(Job)延遲處理,例如像寄送 e-mail,這會使得你的應用程式對網頁請求有更快的反應,隊列(Queue)知道這個任務(Job)應該是被放到隊列(Queue)裡,而不是直接執行,所以我們嘗試將隊列放入 Redis 快取當中做測試。

1. 使用者建立訂單

首先從 10 個連線 300 次連續點擊開始。

This is ApacheBench, Version 2.3 <$Revision: 1874286 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking laravel-typical-high-load-exam.herokuapp.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Finished 300 requests

Server Software:        nginx
Server Hostname:        laravel-typical-high-load-exam.herokuapp.com
Server Port:            80

Document Path:          /testing/v2/shopping
Document Length:        9408 bytes

Concurrency Level:      10
Time taken for tests:   65.356 seconds
Complete requests:      300
Failed requests:        0
Total transferred:      3182100 bytes
HTML transferred:       2822400 bytes
Requests per second:    4.59 [#/sec] (mean)
Time per request:       2178.542 [ms] (mean)
Time per request:       217.854 [ms] (mean, across all concurrent requests)
Transfer rate:          47.55 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:      209  215   3.8    214     232
Processing:   355 1909 191.1   1937    2350
Waiting:      355 1184 490.1   1276    1967
Total:        575 2124 191.0   2153    2562

Percentage of the requests served within a certain time (ms)
  50%   2153
  66%   2157
  75%   2160
  80%   2161
  90%   2166
  95%   2171
  98%   2174
  99%   2179
 100%   2562 (longest request)

再來測試 100 個連線 600 次連續點擊,我們可以發現到與原本的「v2」版本其實沒有差很多。

This is ApacheBench, Version 2.3 <$Revision: 1874286 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking laravel-typical-high-load-exam.herokuapp.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Finished 600 requests

Server Software:        nginx
Server Hostname:        laravel-typical-high-load-exam.herokuapp.com
Server Port:            80

Document Path:          /testing/v2/shopping
Document Length:        9408 bytes

Concurrency Level:      100
Time taken for tests:   128.119 seconds
Complete requests:      600
Failed requests:        0
Total transferred:      6364200 bytes
HTML transferred:       5644800 bytes
Requests per second:    4.68 [#/sec] (mean)
Time per request:       21353.167 [ms] (mean)
Time per request:       213.532 [ms] (mean, across all concurrent requests)
Transfer rate:          48.51 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:      207  212   3.5    210     226
Processing:   290 19271 4655.1  21018   21433
Waiting:      280 10510 6078.9  10429   21053
Total:        499 19483 4655.2  21230   21649

Percentage of the requests served within a certain time (ms)
  50%  21230
  66%  21240
  75%  21247
  80%  21251
  90%  21268
  95%  21278
  98%  21291
  99%  21293
 100%  21649 (longest request)

最後測試 300 個連線 1500 次連續點擊,與原本的「v2」版本其實沒有差很多,可能在隊列的改善版本已經解決了大多數的問題,畢竟隊列(Queue)功能允許你將一個耗時的任務(Job)延遲處理,也就是將最主要的寫入問題給延遲處理了,那麼回應(Response)的部分就會與初始無異。

This is ApacheBench, Version 2.3 <$Revision: 1874286 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking laravel-typical-high-load-exam.herokuapp.com (be patient)
Completed 150 requests
Completed 300 requests
Completed 450 requests
Completed 600 requests
Completed 750 requests
Completed 900 requests
Completed 1050 requests
Completed 1200 requests
Completed 1350 requests
Completed 1500 requests
Finished 1500 requests

Server Software:        nginx
Server Hostname:        laravel-typical-high-load-exam.herokuapp.com
Server Port:            80

Document Path:          /testing/v2/shopping
Document Length:        9408 bytes

Concurrency Level:      300
Time taken for tests:   305.240 seconds
Complete requests:      1500
Failed requests:        0
Total transferred:      15910500 bytes
HTML transferred:       14112000 bytes
Requests per second:    4.91 [#/sec] (mean)
Time per request:       61047.954 [ms] (mean)
Time per request:       203.493 [ms] (mean, across all concurrent requests)
Transfer rate:          50.90 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:      192  203  25.9    202    1201
Processing:   285 54534 14461.9  60448   62125
Waiting:      285 30309 17584.1  30333   61186
Total:        486 54736 14462.2  60650   62326

Percentage of the requests served within a certain time (ms)
  50%  60650
  66%  60667
  75%  60679
  80%  60688
  90%  61604
  95%  61610
  98%  61618
  99%  61620
 100%  62326 (longest request)

2. 查詢訂單內容

最後我們測試查詢訂單的部分,測試 300 個連線 1500 次連續點擊,由於我們已經把快取改由 Redis 做處理,平均每秒可回應要求(Requests per second)也著落於接近每秒 5 個回應。

This is ApacheBench, Version 2.3 <$Revision: 1874286 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking laravel-typical-high-load-exam.herokuapp.com (be patient)
Completed 150 requests
Completed 300 requests
Completed 450 requests
Completed 600 requests
Completed 750 requests
Completed 900 requests
Completed 1050 requests
Completed 1200 requests
Completed 1350 requests
Completed 1500 requests
Finished 1500 requests

Server Software:        nginx
Server Hostname:        laravel-typical-high-load-exam.herokuapp.com
Server Port:            80

Document Path:          /testing/v2/order/76c399c1-042a-46e9-81f3-75845368ca24
Document Length:        16341 bytes

Concurrency Level:      300
Time taken for tests:   304.656 seconds
Complete requests:      1500
Failed requests:        0
Total transferred:      26310000 bytes
HTML transferred:       24511500 bytes
Requests per second:    4.92 [#/sec] (mean)
Time per request:       60931.115 [ms] (mean)
Time per request:       203.104 [ms] (mean, across all concurrent requests)
Transfer rate:          84.34 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:      193  202   1.4    202     215
Processing:   462 54487 14426.0  60533   60994
Waiting:      261 30229 17522.8  30181   60353
Total:        663 54689 14426.0  60736   61204

Percentage of the requests served within a certain time (ms)
  50%  60736
  66%  60740
  75%  60753
  80%  60760
  90%  60772
  95%  60779
  98%  60785
  99%  60786
 100%  61204 (longest request)
Clone this wiki locally