简体   繁体   English

Rails的页面缓存与HTTP反向代理缓存

[英]Rails' page caching vs. HTTP reverse proxy caches

I've been catching up with the Scaling Rails screencasts. 我一直在追赶Scaling Rails的截屏视频。 In episode 11 which covers advanced HTTP caching (using reverse proxy caches such as Varnish and Squid etc.), they recommend only considering using a reverse proxy cache once you've already exhausted the possibilities of page, action and fragment caching within your Rails application (as well as memcached etc. but that's not relevant to this question). 第11集中 ,它涵盖了高级HTTP缓存(使用反向代理缓存,如Varnish和Squid等),一旦你已经用尽了Rails应用程序中的页面,动作和片段缓存的可能性,他们建议只考虑使用反向代理缓存(以及memcached等,但这与这个问题无关)。

What I can't quite understand is how using an HTTP reverse proxy cache can provide a performance boost for an application that already uses page caching. 我不太明白的是,使用HTTP反向代理缓存可以为已经使用页面缓存的应用程序提供性能提升。 To simplify matters, let's assume that I'm talking about a single host here. 为了简化问题,让我们假设我在这里谈论一个主机。

This is my understanding of how both techniques work (maybe I'm wrong): 这是我对两种技术如何工作的理解(也许我错了):

  • With page caching the Rails process is hit initially and then generates a static HTML file that is served directly by the Web server for subsequent requests, for as long as the cache for that request is valid. 通过页面缓存,Rails进程最初被命中,然后生成一个静态HTML文件,该文件由Web服务器直接为后续请求提供服务,只要该请求的缓存有效。 If the cache has expired then Rails is hit again and the static file is regenerated with the updated content ready for the next request 如果缓存已过期,则再次命中Rails并重新生成静态文件,并为更新的内容准备好下一个请求

  • With an HTTP reverse proxy cache the Rails process is hit when the proxy needs to determine whether the content is stale or not. 使用HTTP反向代理缓存,当代理需要确定内容是否陈旧时,会触发Rails进程。 This is done using various HTTP headers such as ETag , Last-Modified etc. If the content is fresh then Rails responds to the proxy with an HTTP 304 Not Modified and the proxy serves its cached content to the browser, or even better, responds with its own HTTP 304. If the content is stale then Rails serves the updated content to the proxy which caches it and then serves it to the browser 这是使用各种HTTP标头完成的,例如ETagLast-Modified等。如果内容是新鲜的,那么Rails使用HTTP 304 Not Modified响应代理,代理将其缓存的内容提供给浏览器,甚至更好,响应它自己的HTTP 304.如果内容是陈旧的,那么Rails将更新的内容提供给代理,后者将其缓存,然后将其提供给浏览器

If my understanding is correct, then doesn't page caching result in less hits to the Rails process? 如果我的理解是正确的,那么页面缓存不会导致对Rails进程的点击次数减少吗? There isn't all that back and forth to determine if the content is stale, meaning better performance than reverse proxy caching. 没有来回确定内容是否陈旧,这意味着比反向代理缓存更好的性能。 Why might you use both techniques in conjunction? 为什么你可以结合使用这两种技术?

You are right. 你是对的。

The only reason to consider it is if your apache sets expires headers. 考虑它的唯一原因是你的apache设置是否过期。 In this configuration, the proxy can take some of the load off apache. 在此配置中,代理可以从apache中获取一些负载。

Having said this, apache static vs proxy cache is pretty much an irrelevancy in the rails world. 话虽如此,apache静态与代理缓存在rails世界中几乎是无关紧要的。 They are both astronomically fast. 它们都是天文学上快速的。

The benefits you would get would be for your none page cacheable stuff. 您将获得的好处是您的无页面可缓存的东西。

I prefer using proxy caching over page caching (ala heroku), but thats just me, and a digression. 我更喜欢在页面缓存(ala heroku)上使用代理缓存,但这只是我和一个题外话。

A good proxy cache implementation (eg, Squid, Traffic Server) is massively more scalable than Apache when using the prefork MPM. 当使用prefork MPM时,良好的代理缓存实现(例如,Squid,Traffic Server)比Apache更具可扩展性。 If you're using the worker MPM, Apache is OK, but a proxy will still be much more scalable at high loads (tens of thousands of requests / second). 如果您正在使用worker MPM,那么Apache就可以了,但是在高负载(数万个请求/秒)下,代理仍然可以更加可扩展。

Varnish for example has a feature when the simultaneous requests to the same URL (which is not in cache) are queued and only single/first request actually hits the back-end. 例如,当对同一URL(不在高速缓存中)的同时请求排队并且只有单个/第一个请求实际到达后端时,Varnish具有一个功能。 That could prevent some nasty dog-pile cases which are nearly impossible to workaround in traditional page caching scenario. 这可以防止在传统的页面缓存场景中几乎不可能解决的一些讨厌的狗堆案例。

Using a reverse proxy in a setup with only one app server seems a bit overkill IMO. 在只有一个应用服务器的设置中使用反向代理似乎有点矫枉过正IMO。 In a configuration with more than one app server, a reverse proxy (eg varnish, etc.) is the most effective way for page caching. 在具有多个app服务器的配置中,反向代理(例如清漆等)是页面缓存的最有效方式。

Think of a setup with 2 app servers: 想想有2个应用服务器的设置:

  • User 'Bob'(redirected to node 'A') posts a new message, the page gets expired and recreated on node 'A'. 用户'Bob'(重定向到节点'A')发布新消息,页面过期并在节点'A'上重新创建。

  • User 'Cindy' (redirected to node 'B') requests the page where the new message from 'Bob' should appear, but she can't see the new message, because the page on node 'B' wasn't expired and recreated. 用户'Cindy'(重定向到节点'B')请求显示来自'Bob'的新消息的页面,但是她看不到新消息,因为节点'B'上的页面未过期并重新创建。

This concurrency problem could be solved with a reverse proxy. 这种并发问题可以通过反向代理来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM