Rails page caching for improved performance

Posted by Jonathan Weyermann on January 30, 2018 at 12:00 AM
Rails cache broken

In order to reduce the load on your server and allow more concurrent users to hit your website, you may use caching. There are many types of caching in Ruby on Rails, some of which are discussed in the guides. Some require the use of a dedicated caching service such as AWS's Elasticache (memcache or redis). The minimum cost of one of those instances is the same as a t2 micro. If you are only running a simple Rails app, this service may not be worth it, especially since there is a simple method of caching that doesn't cost anything extra and provides an immense speed boost in many situations. This is described in the Rails guides as page caching, and while it was removed the Rails core since Rails 4, there is still a handy gem that provide the same functionality called actionpack-page_caching. This caching creates a html only snapshot of your rails page, and allows it to be rendered by only Nginx or Apache, which should be much faster. I personally installed in on my own rails blog (this site) using the instructions and here are the results (I did this test with Apache Bench -> I ran  


ab -n 1000 -c 100 http://localhost:3000/

where 3000 is the local reverse forwarding port of my Nginx server

Before this change, here are the results


Server Software:        nginx/1.10.2
Server Hostname:        localhost
Server Port:            3000

Document Path:          /
Document Length:        17147 bytes

Concurrency Level:      100
Time taken for tests:   130.231 seconds
Complete requests:      1000
Failed requests:        98
(Connect: 0, Receive: 0, Length: 98, Exceptions: 0)
Non-2xx responses:      98
Total transferred:      16733866 bytes
HTML transferred:       15933070 bytes
Requests per second:    7.68 [#/sec] (mean)
Time per request:       13023.087 [ms] (mean)
Time per request:       130.231 [ms] (mean, across all concurrent requests)
Transfer rate:          125.48 [Kbytes/sec] received

Connection Times (ms)
min  mean[+/-sd] median   max
Connect:        0    2   4.2      0      16
Processing:    60 12789 22374.0   5491   85057
Waiting:       52 12764 22355.9   5484   85054
Total:         63 12790 22373.3   5491   85057

Percentage of the requests served within a certain time (ms)
50%   5491
66%   5742
75%   6160
80%   7752
90%  45290
95%  82171
98%  83974
99%  84495
100%  85057 (longest request)


The results after applying the caching are here 

Server Software:        nginx/1.10.2
Server Hostname:        localhost
Server Port:            3000

Document Path:          /
Document Length:        189 bytes

Concurrency Level:      100
Time taken for tests:   25.383 seconds
Complete requests:      1000
Failed requests:        46
(Connect: 0, Receive: 0, Length: 46, Exceptions: 0)
Non-2xx responses:      954
Total transferred:      1283196 bytes
HTML transferred:       969068 bytes
Requests per second:    39.40 [#/sec] (mean)
Time per request:       2538.254 [ms] (mean)
Time per request:       25.383 [ms] (mean, across all concurrent requests)
Transfer rate:          49.37 [Kbytes/sec] received

Connection Times (ms)
min  mean[+/-sd] median   max
Connect:        0   56 169.4      0     986
Processing:    11 2036 5814.8     62   23053
Waiting:       11 2035 5813.1     61   23053
Total:         11 2093 5982.0     62   23611

Percentage of the requests served within a certain time (ms)
50%     62
66%     70
75%     78
80%     82
90%  18281
95%  18288
98%  22617
99%  23073
100%  23611 (longest request)


As you can see, the requests per second went from 7.68/second to  almost 39.4/second, most requests were much faster, and there were fewer failing requests.

In theory, the gem suggests that you have to call expire_page when updating a page, for instance when a comment is added or when a post is updated, but I've found that this seems to be unnecessary. The gem seems to know on it's own when to invalidate the page and reload it.

I would not expect this gem to provide much of a performance speedup if each user has a login area or a customized browsing experience. A new page would be generated each time it was different that a previously cached page. However, it's absolutely brilliant for a blog where each user sees that same content. I suspect that it's limited usefulness in many rails applications is the reason it was removed from the base install in Rails 4. Nevertheless, In the correct use case the performance gain is massive.

User

Lavern Kaplan/August 9 at 5:04 PM

Hi! You Need Leads, Sales, Conversions, Traffic for jonathanweyermann.com ? Will Findet... I WILL SEND 5 MILLION MESSAGES VIA WEBSITE CONTACT FORM Don't believe me? Since you're reading this message then you're living proof that contact form advertising works! We can send your ad to people via their Website Contact Form. IF YOU ARE INTERESTED, Contact us => lisaf2zw526@gmail.com Regards, Kaplan
User

Gus Kinney/August 9 at 5:24 PM

Hello ** I Will Provide BEST SEO Strategy for 2019 ** Looking to boost organic traffic to the pages that really matter? Everyone’s SEO goal is the same: To rank for keywords that drive traffic and revenue. Boost your Google ranking, get more traffic and more sales for jonathanweyermann.com COMPARE PACKAGES => http://bit.ly/Best_SEO_Strategy
User

Adeline Corrigan/September 21 at 11:56 PM

Hello Tired of Waiting FOREVER to earn a profit online? I’m talking about a new way to generate quick traffic and sales in ANY niche with ZERO video/website creation, ZERO paid advertising/SEO. I’ve recently tried this product but quickly decide to write this review because I know this is exactly what you guys are looking for. No Previous Skills Or Experience Required. You can literally be a COMPLETE Newbie and Get RESULTS with just 5 minutes of actual “work”.. SEE FULL REVIEW: http://tinyurl.com/y6bu4jn8 And Of Course, You Also Have A 30-DAY, YOUR-MONEY Back GUARANTEE Once you Join TODAY, You'll Also GET AMAZING BONUSES Regards, TrafficJacker
User

Fern Lamington/October 2 at 6:11 PM

Hello, My name is Fern Lamington, and I'm a SEO Specialist. I just checked out your website jonathanweyermann.com, and wanted to find out if you need help for SEO Link Building ? Build unlimited number of Backlinks and increase Traffic to your websites which will lead to a higher number of customers and much more sales for you. Rank for your keywords: SEE FOR YOURSELF==> http://bit.ly/Best_SEO_Strategy Do not forget to read Review to convince you, is already being tested by many people who have trusted it !! Kind Regards, Fern Lamington ! Business Development Manager UNSUBSCRIBE==> http://bit.ly/Unsubscribe_Seo
User

Vernita Duncan/October 7 at 1:20 PM

Hello, Hope you are doing great. My name is rnd_fname% Duncan, I want to offer you: A CLEVER STEP-BY-STEP METHOD TO ALMOST INSTANT ONLINE FLIPS Flip Magic is the ultimate formula for online profits. It is carefully designed video training modules will walk you through the whole process inside the Members Area. The vendors will reveal their 5 minute Copy & Paste trick, which makes everything easy. Even a complete newcomer can do this – they’ll take you by the hand and walk you through the whole system. ==> http://bit.ly/Flip_Magic Kind Regards, rnd_fname% Duncan
User

Cathy Cooch/October 13 at 1:55 PM

Hello, My name is Cathy Cooch, I want to know if: You Need Leads, Sales, Conversions, Traffic for your site jonathanweyermann.com ? I will Find Leads that Buy From You ! I will Promote Your Business In Any Country To Any Niche ! SEE FOR YOURSELF==> http://bit.ly/Promote_Very_Efficiently Do not forget to read Review to convince you, is already being tested by many people who have trusted it !! Kind Regards, Cathy Cooch UNSUBSCRIBE==> http://bit.ly/Unsubscribe_Sales
User

Vera Pickel/October 17 at 5:12 AM

Good day I just checked out your website jonathanweyermann.com and wanted to find out if you need help for SEO Link Building ? If you aren't using SEO Software then you will know the amount of work load involved in creating accounts, confirming emails and submitting your contents to thousands of websites. With THIS SOFTWARE the link submission process will be the easiest task and completely automated, you will be able to build unlimited number of links and increase traffic to your websites which will lead to a higher number of customers and much more sales for you. IF YOU ARE INTERESTED, We offer you 7 days free trial ==> http://bit.ly/Robot_Submitter Kind Regards, Vera Pickel ! Business Development Manager
User

Wayne Franklyn/October 18 at 6:56 AM

Hello, TIRED OF WAITING FOREVER TO EARN A PROFIT ONLINE ? My name is Wayne Franklyn and I’m talking about a new way to generate quick traffic and sales in ANY niche with ZERO video/website creation, ZERO paid advertising/SEO. I’ve recently tried this product but quickly decide to write this review because I know this is exactly what you are looking for your jonathanweyermann.com No Previous Skills Or Experience Required. You can literally be a COMPLETE Newbie and Get RESULTS with just 5 minutes of actual “work”.. SEE FULL REVIEW: https://is.gd/XE5tAf Kind Regards, Wayne Franklyn