简体   繁体   English

多个客户端使用Redis缓存

[英]Multiple clients using redis cache

I have a question concerning redis in a distributed architecture. 我有一个关于分布式体系结构中的Redis的问题。

Assume I have n clients, either windows desktop applications or asp.net web/web api servers. 假设我有n个客户端,要么是Windows桌面应用程序,要么是asp.net Web / Web API服务器。

One of the clients, lets say client A, hits the cache for a data and has a miss (the data is not in the cache). 客户端之一(假设客户端A)命中高速缓存以获取数据并丢失(数据不在高速缓存中)。 The client then starts to get the real data (from lets say a database) and then sets it in the cache when it's done. 然后,客户端开始获取真实数据(例如从数据库中获取),然后在完成后将其设置在缓存中。

Client B comes along and wants the same data, does a fetch to the cache and since it's a miss, does the same processing. 客户端B出现并想要相同的数据,对缓存进行访存,并且由于未命中,因此进行了相同的处理。

Is there a way for Client B to ...(N) not to do the processing (ie go to the database) until the data is in the cache and fetch the data from the cache instead when it's available? 客户端B是否有办法...(N)直到数据在高速缓存中之后才进行处理(即转到数据库),而是在可用时从高速缓存中获取数据?

I understand that on a single app (or web server), using threads it's easy to check that, but in a distributed architecture? 我知道在单个应用程序(或Web服务器)上,使用线程很容易进行检查,但是在分布式体系结构中呢?

Is this also a correct way of thinking as well? 这也是一种正确的思维方式吗? for the wait process that is If so then could Client A put a flag somewhere stating that he's loading Data X and that all other clients should wait until he's done? 等待过程是否是这样?如果是,那么客户A可以在某处放置一个标记,表明他正在加载数据X,而所有其他客户都应该等待,直到完成为止?

Otherwise, the idea then would be something along the lines of : 否则,该想法将类似于以下内容:

Client A requests Data X 客户端A请求数据X
Miss in cache 缓存中未命中
Processes Data X 处理数据X
Looks if Data X is now in cache 查看Data X现在是否在缓存中
If not, add Data X to cache, otherwise, use it and don't store it in cache 如果不是,则将数据X添加到缓存中,否则,请使用它并且不要将其存储在缓存中

Thanks! 谢谢!

As Kevin said, it's called cache stampede. 正如Kevin所说,这被称为缓存踩踏。 One of the best documents to do with this problem I have read is Using memcached: How to scale your website easily (comes from Josef Finsel): 解决此问题的最佳文档之一是使用memcached:如何轻松扩展您的网站 (来自Josef Finsel):

What we need in this instance is some way to tell our program that another program is working on fetching the data. 在这种情况下,我们需要以某种方式告诉程序另一个程序正在获取数据。 The best way to handle that is by using another memcached entry as a lock. 处理此问题的最佳方法是使用另一个内存缓存条目作为锁。

When our program queries memcached and fails to find data, the first thing it attempts to do is to write a value to a specific key. 当我们的程序查询memcached并找不到数据时,它试图做的第一件事就是将值写入特定的键。 In our example where we are using the actual SQL request for the key name we can just append ":lock" to the SQL to create our new key. 在我们使用实际的SQL请求作为键名的示例中,我们可以在SQL后面附加“:lock”以创建新的键。

What we do next depends on whether the client supports returning success messages on memcached storage commands. 接下来的操作取决于客户端是否支持通过memcached存储命令返回成功消息。 If it does, then we attempt to ADD the value. 如果是这样,那么我们尝试添加该值。 If we are the first one to attempt this then we'll get a success message back. 如果我们是第一个尝试此操作的人,那么我们将返回一条成功消息。 If the value exists then we get a failure indication and we know that another process is trying to update the data and we wait for some predetermined time before we try to get the data again. 如果该值存在,那么我们将获得失败指示,并且我们知道另一个进程正在尝试更新数据,并且我们等待了预定的时间,然后才尝试再次获取数据。

When the process that's updating the cache is done, it deletes the lock key. 完成更新缓存的过程后,它将删除锁定键。

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

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