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

Elvia Ahern/June 13 at 9:07 PM

Make Your WordPress Sites up to 10x Faster In 30 Seconds With The Cache That Beats All Competitors !! Speed Up Your WP Sites Before The July 2018 Google Speed Slap... Speed is a Traffic Killer... More Than 50% of Visitors Leave... If a Site Takes 3+ Seconds To Load + 79% of Visitors Who Have Trouble loading a Page Won't Return ** Here's What You're Getting Today: ** - BANDWIDTH REDUCTION Bad Bot Blocking Hotlink Blocking Smart Cache Clearing** Smart Cache Preloading** - CACHE FEATURES Gzip Compression & Minification Javascript Defer Feature Page & Post Caching Enable/Disable Caching on single Pages/Posts Fonts Optimisation Cloudflare, Multisite, Ecom & Page Builder Friendly Cache Preloading Newbie Friendly ** GET ACCESS NOW ==> https://jvz4.com/c/1014397/301007 Use "5off" for $5 EXTRA OFF ** GET FREE BONUSES ** - FREE BONUS 1 - WP Optimiser WP Optimiser is a combination of quick fixes & diagnostics that give you advice on how to fix your site speed issues - more importantly it covers all 6 Primary site optimisation factors - at best our competitors cover just 2 factors & charge more. - FREE BONUS 2 - WP Easy Pages OK it's no secret that WordPress is natively slow - the WP framework makes this worse - which is why it's not best fit for building landing pages - FREE BONUS 3 - Conversion Boost 2.0 Creating split test pages & personalized pages is a pain in WordPress you literally have to duplicate & edit each page one by one. This puts your site at risk of getting a duplicate content penalty ** GET ACCESS NOW ==> https://jvz4.com/c/1014397/301007 Use "5off" for $5 EXTRA OFF =============================================== - IF YOU DO NOT WANT TO RECEIVE THIS MESSAGE !! - SEND EMAIL WITH YOUR SITE THAT YOU DO NOT WANT TO BE CONTACTED ==> unsubscribe_for@myself.com <== ===============================================
User

Natasha Spruson/June 16 at 9:53 PM

The Best Way Legit To Make Money Online in 20018 !! “METHOD TESTED BY ME” You can see there are number of ways legit to make Money Online . I will explain you each of the method so that you can make a big income from internet. Make $200/day New Fresh Easy Method !! Case Study Reveals A Brand New, Newbie-Friendly Method For Making $243.17 Per Day With FREE Traffic... ** Here's What You're Getting Today: ** - This method is brand new - You’ve never seen anything like this before - ANYONE can use this to get results - You don’t need an email list - We’ll show you how to do this with FREE traffic - There’s no product creation required - It only takes a few minutes to set this up and you can make money from day #1 - This method fixes what’s BROKEN with other methods - You’ll be blown away when you see what we’re doing - It works for newbies... ** GET ACCESS NOW ==> https://forms.aweber.com/form/65/303412665.htm =============================================== - IF YOU DO NOT WANT TO RECEIVE THIS MESSAGE !! - SEND EMAIL WITH YOUR SITE THAT YOU DO NOT WANT TO BE CONTACTED ==> unsubscribe_your_site@mail.com <== ===============================================
User

Ola Durand/June 23 at 11:30 PM

Congratulations Admin jonathanweyermann.com I’m talking about an incredible top secret system that is turning just a few lucky, every day people into millionaires with the help of one of the largest websites in the world. This offer is only available for a limited period of time so make sure you click the link below and get signed up immediately before someone else takes this opportunity. The Amazon Code - GUARANTEED: $500 - Your Online Job for jonathanweyermann.com Click Here To Watch The Private Video ==> http://bit.ly/GET_ACCESS_AZCode Regards, =============================================== - IF YOU DO NOT WANT TO RECEIVE THIS MESSAGE !! - SEND EMAIL WITH YOUR SITE THAT YOU DO NOT WANT TO BE CONTACTED ==> unsubscribe_your_site@mail.com <== ===============================================
User

Camilla Boxer/June 24 at 12:42 AM

The 2 Week Diet is an extreme rapid weight loss system that can help you lose up to 16 pounds of pure body fat in just 2 weeks! Get your personalized diet plan for 2 week weight loss, our 14 day diet, and how to lose weight in 2 weeks from our official website. WHAT TAKES MOST DIETS 2-3 MONTHS, THE 2 WEEK DIET ACHIEVES IN ONLY 14 DAYS, INCLUDING… DON'T MISS OUT ==> http://bit.ly/The_2_Week_Diet_Ok =============================================== - IF YOU DO NOT WANT TO RECEIVE THIS MESSAGE !! - SEND EMAIL WITH YOUR SITE THAT YOU DO NOT WANT TO BE CONTACTED ==> unsubscribe_your_site@mail.com <== ===============================================
User

Earl Napier/July 4 at 3:18 PM

The 2 Week Diet is an extreme rapid weight loss system that can help you lose up to 16 pounds of pure body fat in just 2 weeks! Get your personalized diet plan for 2 week weight loss, our 14 day diet, and how to lose weight in 2 weeks from our official website. WHAT TAKES MOST DIETS 2-3 MONTHS, THE 2 WEEK DIET ACHIEVES IN ONLY 14 DAYS, INCLUDING… DON'T MISS OUT ==> http://bit.ly/The_2_Week_Diet_Ok
User

Tiara Louise/July 8 at 11:55 AM

Discover the Secret West African Red Tea - Proven to Stop Hunger Cravings in Their Tracks & Help You Shed One Pound of FAT every 72 hours?! The Red Tea Detox is a new rapid weight loss system that can help you lose 14 pounds of pure body fat in just 14 days! It involves drinking a special African blend of red tea to help you lose weight fast! Try the recipe today ==> http://bit.ly/Red_Tea_Detox_2018
User

Nam Strayer/July 12 at 12:40 AM

NEW! CONTROVERSIAL Software Exploits $12.3 MILLION LOOPHOLE & Makes Us $519/DAY... You can use the Software to build your own Profiteering Website in under 5 Minutes. Watch the DEMO on this private page == >> http://bit.ly/Five_Minute_Profit_Sites
User

Liza Matra/July 16 at 5:16 PM

Make 16,000 Projects With Step By Step Plans... even if you don't have a large workshop or expensive tools! Each of the 16,000 projects are detailed enough to leave nothing to guesswork yet simple enough for beginners. It contains complete instructions from start to finish, leaving absolutely no guesswork. Here's what I'm talking about == >> http://bit.ly/Largest_Woodworking_Plans
User

Monique Coolidge/July 18 at 12:06 AM

Make 16,000 Projects With Step By Step Plans... even if you don't have a large workshop or expensive tools! Each of the 16,000 projects are detailed enough to leave nothing to guesswork yet simple enough for beginners. It contains complete instructions from start to finish, leaving absolutely no guesswork. Here's what I'm talking about == >> http://bit.ly/Largest_Woodworking_Plans
User

Tesha Desaillly/July 21 at 9:43 AM

Bacon & Butter: The Ultimate Ketogenic Diet Cookbook Now! Discover 148 amazing ketogenic recipes inside this special edition of our New York Times Bestseller… plus more. GET NoW ==>> http://bit.ly/Free_Bacon_Butter
User

Antonio Niland/July 24 at 9:12 AM

TURN $1 INTO $5, $10 … EVEN $50 ALL DAY LONG Free Video Shows How To Make 5-50X ROI In The Untapped Crypto Market With This CRYPTOCURRENCY SOFTWARE ==>> https://forms.aweber.com/form/07/178256307.htm
User

Everette Lamington/July 26 at 9:49 AM

The WORLD's MOST POWERFUL LINK BUILDING SOFTWARE Money Robot Submitter is the most powerful SEO automation tool designed to publishing your content and backlinks to thousands of websites. Read More ==> https://www.moneyrobot.com/Axyy
User

Inge Dodd/August 2 at 11:10 PM

The web’s most advanced private proxy service! Test our proxies for your purpose before ordering… TEST NEW FREE ==>> http://bit.ly/Test_Proxies
User

Jayson Negron/August 16 at 5:33 PM

Discover The Secret 3-Step Amazon Formula & Start Earning Affiliate Commissions On Demand jonathanweyermann.com 1.Instantly deploy your new profitable, hands-free Amazon affiliate store. 2.Easily discover the most profitable and viral products to promote. 3.Press one button to build a site that will generate viral traffic and sales on autopilot. GET FREE ACCES ==> http://bit.ly/GET_Amazon_Formula SEND EMAIL WITH YOUR SITE ==> unsubscribe_your_site@mail.com <==
User

Nida Breinl/August 22 at 2:01 PM

How To DOUBLE Your Monthly Profits & Leverage ANY Online Video For Your Own Campaigns Never-before-seen tech turns the world of online video into your PERSONAL content machine GET INSTANT ACCESS ==>> https://jvz7.com/c/1014397/299121 [[[ UNSUBSCRIBABLE ]]] SEND EMAIL WITH YOUR SITE LINK ==> unsubscribe_your_site@mail.com
User

Robby Brockman/September 21 at 11:21 PM

Free Chat with Girls & Boys - Live sexy web cams and chat... Enjoy Free Sex Streams and Live Adult Chat with horny cam girls and boys on XXL Cams Club! Join now and watch free sex shows and recorded sex performance wherever you want! FREE LIVE SEX CAMS ==> http://bit.ly/Free_Live_Sex_Cams
User

Stefan Latour/September 22 at 12:21 AM

Girl Live Cams: Meet the Most Desirable Adult Models | LiveJasmin Find the sweetest, sexiest, hottest, flirtiest, the most versatile cam girls in the world of cybersex! Sign up for 10 free teasers with the best female models! CHOOSE THE MODEL ==> http://bit.ly/LiveJasmin_Cams
User

Valentina Chisolm/September 28 at 10:26 AM

INTRODUCING -> BODYBLAST CLEANSE All Natural Formula for Gentle, Powerful Results Our bodies are constantly bombarded by environmental pollutants, toxins, poor quality food, and stress, which take a heavy toll on our digestive systems. Our diets are full of processed, refined foods, tainted with pesticides, hormones, and antibiotics, which can collect in the bloodstream and tissues, causing us to feel tired, function ineffectively, or become overweight. The key to an effective weight loss program is to cleanse your body from the inside. TRY NOW ==> http://bit.ly/detox-bodyblast
User

Ida Fitts/September 30 at 11:13 AM

Why our software is the World's most powerful link building software ? Our Software supports UNLIMITED website platforms. You can submit your backlinks/content to following main platforms with support built... [GET FREE TRIAL] ==> http://www.moneyrobotsubmiter.club
User

Penni Money/October 3 at 10:22 PM

Why our software is the World's most powerful link building software ? With Money Robot Submitter 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. [GET FREE TRIAL] ==> http://bit.ly/BEST_SEO_RESULT ================================ Want to change how you receive these emails ? UNSUBSCRIBE == > http://bit.ly/Unsubscribable_Site Kind Regards, Axyy Splaiul Independentei, Sectorul 3, 030099
User

Eugene Puglisi/October 4 at 3:14 PM

I Will Promote Your Business Via 60,000,000 Real People From Worldwide To Any Niche For Your Business By Contacting Site Webmasters In Your Niche... So you can contact the right audience with offers, driving traffic to your website: - Show your business/services/video’s to a targeted audience. - Requesting link exchanges from other sites in a similar niche. - Contacting webmasters about potentially advertising on their sites - Research and information requests from any targeted web sources - Sending out guest posting and submission requests - Getting in contact with potential affiliates to show your services or products - Contacting a targeted audience to submit press releases - Locating the right people to review your product and services GET NOW ==>> http://bit.ly/Promote_Your_Business =================================================== Want to change how you receive these emails ? UNSUBSCRIBE == > http://bit.ly/Unsubscribable_Site Kind Regards, Axyy Splaiul Independentei, Sectorul 3, 030099
User

Carlos Helvey/October 15 at 6:19 PM

You need Website Hosting Services, Cloud VPS or Dedicated Servers ?? Founded in 2013, Whoinc has grown to become one of the largest web hosting providers in the US, serving over 10,000 customers and hosting in excess of 12,000 websites across our network. Our services: - Cloud Cpanel Hosting - Dedicated Hosting - VPS Hosting - SSL Certificates - SEO (Performance Based) and Much More... We Provide Secure & Reliable Servers! http://bit.ly/2CkBbKb Let us know if you are interested! Lenny Ruiz
User

Lesley Tulk/October 19 at 9:16 AM

Why our software is the World's most powerful link building software ? With Money Robot Submitter 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. [GET FREE TRIAL] ==> http://bit.ly/MRS_Trial =================================================== Regards, Lesley Tulk Great Britain, NA, Carbost, Iv47 0nj, 9 Greyfriars Road Want To Stop Getting Emails ? Report SPAM & UNSUBSCRIBE == > http://bit.ly/Unsubscribable_Site
User

Russell Kifer/October 20 at 10:16 PM

I Will Promote Your Business By Contacting Site Webmasters (CONTACT FORMS AT SITES) Via 60,000,000 Sites From Worldwide To Any Niche ... So you can contact the right audience with offers, driving traffic to your website: - Show your business/services/video’s to a targeted audience; - Requesting link exchanges from other sites in a similar niche; - Contacting webmasters about potentially advertising on their sites; - Sending out guest posting and submission requests; - Getting in contact with potential affiliates to show your services or products; - Contacting a targeted audience to submit press releases; - Locating the right people to review your product and services. GET Visitors/Clicks ==>> http://bit.ly/2NLBJuB =================================================== Regards, Russell Kifer Denmark, REGION SJALLAND, Karise, 4653, Plouggardsvej 10 Want To Stop Getting Emails ? Report SPAM & UNSUBSCRIBE == > http://bit.ly/Unsubscribable_Site
User

Marvin Folk/October 27 at 9:46 AM

See A Family of Artists... TWO HEARTS I ONE CANVAS - John and Elli Milan A husband and wife team who create their artwork together. Each piece is a celebration of two hearts collaborating with the same spirit. Follow Milan Art Gallery on Instagram and Youtube for beautiful art that will inspire you! http://bit.ly/Follow_Milan_Art_Gallery https://www.instagram.com/milanart.gallery/?hl=en https://www.youtube.com/channel/UCmuGaR5nFqvI0c6zX60lJDA
User

Keeley Herman/November 5 at 12:20 AM

I Will Promote Your Business By Contacting Site Webmasters (CONTACT FORMS AT SITES) Via 60,000,000 Sites From Worldwide To Any Niche ... So you can contact the right audience with offers, driving traffic to your website: - Show your business/services/video’s to a targeted audience; - Requesting link exchanges from other sites in a similar niche; - Contacting webmasters about potentially advertising on their sites; - Sending out guest posting and submission requests; - Getting in contact with potential affiliates to show your services or products; - Contacting a targeted audience to submit press releases; - Locating the right people to review your product and services. GET Visitors/Clicks ==>> http://bit.ly/2CWw4jz =================================================== Want To Stop Getting Emails ? Report SPAM & UNSUBSCRIBE == > http://bit.ly/Unsubscribable_Site
User

Nam Ng/November 7 at 9:24 PM

GET Complete Web Hosting, Web Design, and SEO Services for jonathanweyermann.com Ourweb provides a level of hosting support that is unmatched in the industry. Our services are designed for both beginners and experts alike. All hosting accounts come with CPanel, Installatron, unlimited email accounts, unmetered bandwidth, and unlimited subdomains. After 19 years in business we have perfected the art of offering reliable web hosting services on top of the line equipment to ensure maximum uptime and stability. Top notch support is also one of our main goals, we’re always here to help you 24x7x365! We offer a 14 day money back guarantee so that you can test our hosting service risk free so you can see what type of reliable quality hosting we can deliver to you and your visitors! GET HOSTING - > http://bit.ly/ourwebnet
User

Larae Tengan/November 7 at 9:46 PM

I Will Provide Organic Traffic By Keyword From Google, Bing, Yahoo Daily - (Low Bounce Rate). Why Should You Buy Organic Traffic? SERP Improvement - Drive organic traffic/visitors is essential nowadays for Google, Yahoo & Bing to rank your website, Search Engines Ranking would improve, Very good for improving offsite SEO, More Recognition, SEO Lethal Weapon, Loved by Search Engines, Algorithm Adaptive, Increase Ranking, Worldwide Visitors Get Revenue from CPM - 100% safe for AdSense, Publishers by setting Ads to your website. Traffic - comes from Worldwide with 50%+ from USA & other Tier 1 Countries. Keywords - Choose up to 3 Long or Short Tail Keywords (We suggest indexed), We only support English language. GET NoW ==>> http://bit.ly/Get_Organic_Traffic Want To Stop Getting Emails ? Report SPAM & UNSUBSCRIBE == > http://bit.ly/Unsubscribable_Site
User

Betsey Bertles/November 17 at 12:58 AM

I Will Promote Your Business By Contacting Site Webmasters (CONTACT FORMS AT SITES) Via 10,000,000 Sites From Worldwide To Any Niche ... So you can contact the right audience with offers, driving traffic to your website: - Show your business/services/video’s to a targeted audience; - Requesting link exchanges from other sites in a similar niche; - Contacting webmasters about potentially advertising on their sites; - Sending out guest posting and submission requests; - Getting in contact with potential affiliates to show your services or products; - Contacting a targeted audience to submit press releases; - Locating the right people to review your product and services. GET Visitors/Clicks ==>> http://bit.ly/GET_Quality_Traffic =================================================== Want To Stop Getting Emails ? Report SPAM & UNSUBSCRIBE == > http://bit.ly/Unsubscribable_Site
User

Dick Cosby/December 8 at 8:52 PM

Hi there jonathanweyermann.com You want quality traffic that Buys From You or Converts into Highly Responsive Subscribers ? I Will Promote Your Business In Worldwide To Any Niche ... - I search in google after the keyward offered by you in order to find sites; - I will ask the site administrator if he is interested in your offer; - The process continues on many sites until you get all the vizits promises. So you can contact the right audience with offers, driving traffic to your website: - Show your business/services/video’s to a targeted audience; - Requesting link exchanges from other sites in a similar niche; - Contacting webmasters about potentially advertising on their sites; - Sending out guest posting and submission requests; - Getting in contact with potential affiliates to show your services or products; - Contacting a targeted audience to submit press releases; - Locating the right people to review your product and services. I can send your offer on: - Country Desired (only the country you specify) - 80% TIER 1 Countries Guaranteed (USA, CA, GB, AU, NZ, IE) - Niche Desired(only the keywords you specify) We help you grow your business because we understand that our business growth directly depends on our ability to GROW YOUR BUSINESS. IF YOU'RE INTERESTED, CONTACT ME ==> high_quality_solo_ad@mail.com Regards, Axyy Want To Stop Getting Emails ? Report SPAM & UNSUBSCRIBE == > http://bit.ly/Unsubscribable_Site