使用memcached和MySQL等rdbms时的缓存一致性 [英] Cache consistency when using memcached and a rdbms like MySQL
问题描述
本学期我上了数据库课,我们正在研究如何维护RDBMS与诸如memcached之类的缓存服务器之间的缓存一致性.在存在竞争条件时会出现一致性问题.例如:
I have taken a database class this semester and we are studying about maintaining cache consistency between the RDBMS and a cache server such as memcached. The consistency issues arise when there are race conditions. For example:
- 假设我从缓存中执行了
get(key)
,并且有一个缓存未命中.因为遇到高速缓存未命中,所以我从数据库中获取数据,然后对高速缓存执行put(key,value)
. - 但是,可能会发生争用情况,其他用户可能会删除我从数据库中提取的数据.在我对缓存执行
put
之前,可能会发生这种删除.
- Suppose I do a
get(key)
from the cache and there is a cache miss. Because I get a cache miss, I fetch the data from the database, and then do aput(key,value)
into the cache. - But, a race condition might happen, where some other user might delete the data I fetched from the database. This delete might happen before I do a
put
into the cache.
因此,理想地,不应将put
进入高速缓存,因为数据库中数据的存在时间越来越长.
Thus, ideally the put
into the cache should not happen, since the data is longer present in the database.
如果缓存条目具有TTL,则缓存中的条目可能会过期.但是,仍然存在一个窗口,其中缓存中的数据与数据库不一致.
If the cache entry has a TTL, the entry in the cache might expire. But still, there is a window where the data in the cache is inconsistent with the database.
我一直在寻找有关此类问题的文章/研究论文.但是,我找不到任何有用的资源.
I have been searching for articles/research papers which speak about this kind of issues. But, I could not find any useful resources.
推荐答案
本文为您提供了有关Facebook如何(尝试)保持缓存一致性的有趣注释:
This article gives you an interesting note on how Facebook (tries to) maintain cache consistency : http://www.25hoursaday.com/weblog/2008/08/21/HowFacebookKeepsMemcachedConsistentAcrossGeoDistributedDataCenters.aspx
这是本文的要点.
- 我将名字从"Jason"更新为"Monkey"
- 我们在加利福尼亚的master数据库中写入"Monkey",然后从加利福尼亚的内存缓存中删除我的名字,但从弗吉尼亚州删除
- 有人去了我在弗吉尼亚州的个人资料
- 我们在内存缓存中找到了我的名字,并返回"Jason"
- 复制赶上了,我们用我的名字"Monkey"更新了从数据库. 我们还从弗吉尼亚州内存缓存中删除了我的名字,因为该缓存对象出现在复制流中
- 其他人进入了我在弗吉尼亚州的个人资料
- 我们在内存缓存中找不到我的名字,所以我们从奴隶那里读取并获得猴子"
- I update my first name from "Jason" to "Monkey"
- We write "Monkey" in to the master database in California and delete my first name from memcache in California but not Virginia
- Someone goes to my profile in Virginia
- We find my first name in memcache and return "Jason"
- Replication catches up and we update the slave database with my first name as "Monkey." We also delete my first name from Virginia memcache because that cache object showed up in the replication stream
- Someone else goes to my profile in Virginia
- We don't find my first name in memcache so we read from the slave and get "Monkey"
这篇关于使用memcached和MySQL等rdbms时的缓存一致性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!