内存缓存,锁定和竞争条件 [英] Memcached, Locking and Race Conditions

查看:84
本文介绍了内存缓存,锁定和竞争条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试在写入数据库时​​更新Memcached对象,以避免在插入/更新后必须从数据库中读取它们.

We are trying to update memcached objects when we write to the database to avoid having to read them from database after inserts/updates.

对于我们的论坛帖子对象,我们有一个ViewCount字段,其中包含查看帖子的次数.

For our forum post object we have a ViewCount field containing the number of times a post is viewed.

我们担心通过更新memcached对象来引入竞争条件,因为同一帖子可能同时在服务器场中的另一台服务器上查看.

We are afraid that we are introducing a race condition by updating the memcached object, as the same post could be viewed at the same time on another server in the farm.

任何想法如何处理这类问题-似乎需要某种锁定,但如何在服务器场中的服务器之间可靠地进行锁定呢?

Any idea how to deal with these kind of issues - it would seem that some sort of locking is needed but how to do it reliably across servers in a farm?

推荐答案

如果您要处理的数据不一定需要进行实时更新,那么对我来说,视图计数是一个它们中的一个,然后您可以向存储在内存缓存中的对象添加一个expires字段.

If you're dealing with data that doesn't necessarily need to be updated realtime, and to me the view count is one of them, then you could add an expires field to the objects that are stored in memcache.

一旦发生过期,它将返回数据库并读取新值,但是在此之前,它将保持不变.

Once that expiration happens, it'll go back to the database and read the new value, but until then it will leave it alone.

当然,对于新帖子,您可能希望更频繁地进行更新,但是您可以对此进行编码.

Of course for new posts you may want this updated more often, but you can code for this.

Memcache仅将对象的一个​​副本存储在其实例之一中,而不是在许多实例中,因此我不必担心对象锁定或任何事情.那是数据库要处理的,而不是缓存.

Memcache only stores one copy of your object in one of its instances, not in many of them, so I wouldn't worry about object locking or anything. That is for the database to handle, not your cache.

Memcache无法保证当您从各种服务器上进行设置时,数据不会被破坏.

Memcache offers no guarantee that when you're getting and setting from varied servers that your data won't get clobbered.

来自内存缓存文档:

  • 一系列命令不是原子的.如果对某个项目发出获取",对数据进行操作,然后希望将其设置"回memcached,则不能保证您是唯一处理该值的过程.同时,您可能最终会覆盖其他设置的值.

比赛条件和陈旧数据

在设计应用程序以缓存数据时,要记住的一件事是如何处理竞争条件和偶尔的过时数据.

One thing to keep in mind as you design your application to cache data, is how to deal with race conditions and occasional stale data.

说您缓存了最新的五个注释,以显示在应用程序的边栏中.您决定只需要每分钟刷新一次数据.但是,您忘记记住该侧边栏显示每秒渲染50次!因此,一旦60秒滚动并且缓存过期,突然有10多个进程运行相同的SQL查询以重新填充该缓存.每次缓存过期时,都会导致SQL流量突然爆发.

Say you cache the latest five comments for display on a sidebar in your application. You decide that the data only needs to be refreshed once per minute. However, you neglect to remember that this sidebar display is renderred 50 times per second! Thus, once 60 seconds rolls around and the cache expires, suddenly 10+ processes are running the same SQL query to repopulate that cache. Every time the cache expires, a sudden burst of SQL traffic will result.

更糟糕的是,您有多个进程在更新同一数据,而错误的进程最终导致了对高速缓存的约会.然后,您将获得陈旧的过时数据.

Worse yet, you have multiple processes updating the same data, and the wrong one ends up dating the cache. Then you have stale, outdated data floating about.

应该注意在填充或重新填充缓存时可能出现的问题.请记住,检查memcached,获取SQL并将其存储到memcached中的过程根本不是原子的!

One should be mindful about possible issues in populating or repopulating our cache. Remember that the process of checking memcached, fetching SQL, and storing into memcached, is not atomic at all!

这篇关于内存缓存,锁定和竞争条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆