REST API 缓存,我应该使用反向代理还是 memcache(d)?
我有一个 REST API,我想在其中缓存索引 (GET /foo) 和读取操作 (GET /foo/1) 的 JSON 响应,以显着提高性能。当资源上有 POST 或 PUT 时,索引和读取结果的缓存条目需要过期,因此不会提供旧内容。
这种情况最好使用 Squid / Varnish 等反向代理来完成,还是选择 memcache(d)?
I have a REST API where I would like to cache the JSON response of the index (GET /foo) and the read actions (GET /foo/1) to significantly increase the performance. When there is a POST or a PUT on a resource the cache entries for the index and read results need to be expired, so no old content is served.
Is this a scenario that's best done with a Reverse proxy like Squid / Varnish or would you choose memcache(d)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用位于 HTTP 层的反向代理更加透明。这意味着可以通过网络查看正在发生的情况。不好的是,其中很少有支持缓存经过身份验证的响应,因此如果您的资源需要身份验证,它们的效率可能会降至 0。当完全不相关的资源 B (
/foo/1
) 被修改时,反向代理通常也不会自动使资源 A (/foo
) 过期。这是正确的行为,您必须以某种方式添加到您的解决方案中。如果使用 memcached,这两个问题都可以解决,因为它没有透明度要求。
Using a reverse proxy that sits on the HTTP layer is more transparent. That means that it's possible to see what's going on over the wire. The bad thing is that few of these support caching authenticated responses, so their efficiency may drop to 0 if your resources require authentication. Reverse proxies also don't usually automatically expire resource A (
/foo
) when this completely unrelated resource B (/foo/1
) is modified. That's correct behaviour that you'd have to add to your solution somehow.Both of these problems can be solved if you use memcached, since it doesn't have the transparency requirement.
我会选择像 varnish 这样的反向代理,因为您可以在不涉及缓存逻辑的情况下实现(和测试)您的服务,并将缓存添加为单独的层。您可以升级/重新启动您的服务,同时 varnish 为 GET 请求提供旧结果(非常适合可用性),并且可以轻松地在 varnish 中设置规则,以根据特定的 GET/POST 操作使现有缓存结果无效(清除)。
I would go for a reverse proxy like varnish because you can implement (and test) your service without involving cache logic, and add caching as a separate layer. You can upgrade/restart your service while varnish serves old results for GET request (great for availability), and it's easy to setup rules in varnish to invalide (purge) existing cache results based on specific GET/POST actions.
如果你想使用分布式内存memcached是一个很好的解决方案。 https://github.com/cpatni/middleman 是一个反向代理,它使用 memcached 进行缓存。
If you want to employ distributed memory memcached is a good solution. https://github.com/cpatni/middleman is a reverse proxy which uses memcached for caching.