stacks-archive / blockstack.org-api

The proxy API for blockstack.org
5 stars 2 forks source link

Speed up blog load by caching rss endpoint #1

Open larrysalibra opened 6 years ago

larrysalibra commented 6 years ago

From a discussion in slack, the blog loads very slowly because of various reasons:

I added cache headers to the endpoints in this api:

Vanquish:blockstack-site-api larry$ curl -I https://blockstack-site-api.herokuapp.com/v1/blog-rss
HTTP/1.1 200 OK
Connection: keep-alive
Server: gunicorn/19.6.0
Date: Thu, 12 Oct 2017 18:47:05 GMT
Content-Type: text/xml; charset=utf-8
Content-Length: 74441
Cache-Control: public, max-age=300
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: HEAD, GET
Access-Control-Max-Age: 21600
Via: 1.1 vegur

I don't have write access to this repo so the code is only in the heroku repo.

Then next step is to put a caching CDN in from of it.

We can use cloudflare for this.

We need to do the following:

  1. Add a custom domain to the heroku app heroku domains:add site-api.blockstack.org
  2. Add the target domain that heroku tool provides to cloudflare (site-api.blockstack.org.herokudns.com)
  3. Set a cloudflare page rule that enables caching of everything for the target domain in 2.
  4. Update the URL with the new URL in blockstack.org
  5. Update the rewriting logic in the blog endpoint app to use the new domain

These are the settings for the cloudflare page rule:

screen shot 2017-10-13 at 2 56 33 am

tagging @shea256 @jackzampolin @kantai

larrysalibra commented 6 years ago

I set up an endpoint for this for testing. It's proxied through cloudflare and a digital ocean server (in singapore) before hitting heroku. This isn't what we'd want to do. We'd want to add a custom domain directly to heroku.

https://blockstack-site-api.nametiles.co/v1/blog-rss

Some benchmarks:

1 order of magnitude improvement in load speed on that results in cache hit.

Interestingly, heroku's routing is so bad from here that the cache miss is still about 200 ms faster than loading directly from heroku.

Benchmarks:

Single request, no cdn:

$ ab https://blockstack-site-api.herokuapp.com/v1/blog-rss
This is ApacheBench, Version 2.3 <$Revision: 1796539 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking blockstack-site-api.herokuapp.com (be patient).....done

Server Software:        gunicorn/19.6.0
Server Hostname:        blockstack-site-api.herokuapp.com
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128
TLS Server Name:        blockstack-site-api.herokuapp.com

Document Path:          /v1/blog-rss
Document Length:        74441 bytes

Concurrency Level:      1
Time taken for tests:   2.066 seconds
Complete requests:      1
Failed requests:        0
Total transferred:      74759 bytes
HTML transferred:       74441 bytes
Requests per second:    0.48 [#/sec] (mean)
Time per request:       2066.105 [ms] (mean)
Time per request:       2066.105 [ms] (mean, across all concurrent requests)
Transfer rate:          35.34 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:      835  835   0.0    835     835
Processing:  1231 1231   0.0   1231    1231
Waiting:      613  613   0.0    613     613
Total:       2066 2066   0.0   2066    2066

Single request, CDN cache miss:

$ ab https://blockstack-site-api.nametiles.co/v1/blog-rss
This is ApacheBench, Version 2.3 <$Revision: 1796539 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking blockstack-site-api.nametiles.co (be patient).....done

Server Software:        cloudflare-nginx
Server Hostname:        blockstack-site-api.nametiles.co
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,ECDHE-ECDSA-CHACHA20-POLY1305,256,256
TLS Server Name:        blockstack-site-api.nametiles.co

Document Path:          /v1/blog-rss
Document Length:        74441 bytes

Concurrency Level:      1
Time taken for tests:   1.967 seconds
Complete requests:      1
Failed requests:        0
Total transferred:      75106 bytes
HTML transferred:       74441 bytes
Requests per second:    0.51 [#/sec] (mean)
Time per request:       1966.726 [ms] (mean)
Time per request:       1966.726 [ms] (mean, across all concurrent requests)
Transfer rate:          37.29 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       51   51   0.0     51      51
Processing:  1916 1916   0.0   1916    1916
Waiting:     1883 1883   0.0   1883    1883
Total:       1967 1967   0.0   1967    1967

Single request, CDN cache hit:

$ ab https://blockstack-site-api.nametiles.co/v1/blog-rss
This is ApacheBench, Version 2.3 <$Revision: 1796539 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking blockstack-site-api.nametiles.co (be patient).....done

Server Software:        cloudflare-nginx
Server Hostname:        blockstack-site-api.nametiles.co
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,ECDHE-ECDSA-CHACHA20-POLY1305,256,256
TLS Server Name:        blockstack-site-api.nametiles.co

Document Path:          /v1/blog-rss
Document Length:        74441 bytes

Concurrency Level:      1
Time taken for tests:   0.077 seconds
Complete requests:      1
Failed requests:        0
Total transferred:      75102 bytes
HTML transferred:       74441 bytes
Requests per second:    12.97 [#/sec] (mean)
Time per request:       77.095 [ms] (mean)
Time per request:       77.095 [ms] (mean, across all concurrent requests)
Transfer rate:          951.32 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       56   56   0.0     56      56
Processing:    21   21   0.0     21      21
Waiting:       10   10   0.0     10      10
Total:         77   77   0.0     77      77

100 requests, max 10 at a time, no CDN:

$ ab -n 100 -c 10 https://blockstack-site-api.herokuapp.com/v1/blog-rss
This is ApacheBench, Version 2.3 <$Revision: 1796539 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking blockstack-site-api.herokuapp.com (be patient).....done

Server Software:        gunicorn/19.6.0
Server Hostname:        blockstack-site-api.herokuapp.com
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128
TLS Server Name:        blockstack-site-api.herokuapp.com

Document Path:          /v1/blog-rss
Document Length:        74441 bytes

Concurrency Level:      10
Time taken for tests:   21.035 seconds
Complete requests:      100
Failed requests:        1
   (Connect: 0, Receive: 0, Length: 1, Exceptions: 0)
Non-2xx responses:      1
Total transferred:      7401832 bytes
HTML transferred:       7370165 bytes
Requests per second:    4.75 [#/sec] (mean)
Time per request:       2103.503 [ms] (mean)
Time per request:       210.350 [ms] (mean, across all concurrent requests)
Transfer rate:          343.63 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:      668  770 116.2    701    1030
Processing:   972 1263 1040.4   1053   11333
Waiting:      531  777 1079.8    599   11333
Total:       1644 2033 1077.5   1779   12276

Percentage of the requests served within a certain time (ms)
  50%   1779
  66%   2036
  75%   2146
  80%   2151
  90%   2458
  95%   2587
  98%   2919
  99%  12276
 100%  12276 (longest request)

100 requests, max 10 at a time, CDN cache hit:

ab -n 100 -c 10 https://blockstack-site-api.nametiles.co/v1/blog-rss
This is ApacheBench, Version 2.3 <$Revision: 1796539 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking blockstack-site-api.nametiles.co (be patient).....done

Server Software:        cloudflare-nginx
Server Hostname:        blockstack-site-api.nametiles.co
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,ECDHE-ECDSA-CHACHA20-POLY1305,256,256
TLS Server Name:        blockstack-site-api.nametiles.co

Document Path:          /v1/blog-rss
Document Length:        74441 bytes

Concurrency Level:      10
Time taken for tests:   1.523 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      7510200 bytes
HTML transferred:       7444100 bytes
Requests per second:    65.68 [#/sec] (mean)
Time per request:       152.252 [ms] (mean)
Time per request:       15.225 [ms] (mean, across all concurrent requests)
Transfer rate:          4817.13 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       33   78  29.1     71     145
Processing:    18   55  29.0     47     172
Waiting:        5   19  18.3     12     125
Total:         61  133  40.1    128     314

Percentage of the requests served within a certain time (ms)
  50%    128
  66%    138
  75%    153
  80%    160
  90%    186
  95%    203
  98%    238
  99%    314
 100%    314 (longest request)