Thank you for your responses.
In the last several days we have gotten a much better understanding of
memcache in general and how we use it.
A little more background to help you understand the current state:
We have 4 live servers that are all supposed to be identical. Our
code is in PHP. We have high user traffic and any user logging onto
our site at any given moment could be sent to any one of the 4 servers
and it shouldn't matter which one, they should all look the same.
Based on my understanding of the benefits of memcache as a distributed
caching system, we are not using it in this way. The current
implementation sets up memcache like this:
class MyMemcache extends Memcache {
public function MyMemcache( $environment = null) {
if (empty($environment)) {
$environment = ENVIRONMENT_CODE;
}
switch ($environment) {
case ENV_DEV:
$this->addServer('localhost', 11211);
break;
case ENV_PROD:
// base
$this->addServer('localhost', 11211);
break;
default:
throw new Exception("Unknown environment
$environment");
}
}
I saw in previous code that there were more servers added in the case
ENV_PROD but they were removed at some point. So basically we use
memcache as a local cache. This is how we use memcache:
public function getCacheableActivityByID($id) {
$key = ActivityManager::getActivityCacheKey($id);
$cached = $this->memcache->get($key);
if( !empty($cached )) {
return $this->refreshDynamicData($cached);
}
$activity = $this->getNonDeletedActivityByID($id); //fetches
from DB
$this->memcache->set($key, $activity, 0,
ACTIVITY_EXPIRY_SECS);
return $activity;
}
public function invalidateCachedActivity($activityID) {
$key = ActivityManager::getActivityCacheKey($activityID);
$this->memcache->delete($key);
}
We set the key to expire after 6 hours so currently we know that the
maximum time a key could have inconsistent data is 6 hours but we
would like it to be updated as soon as there is a DB write. We know
that this problem of inconsistent data would be solved if we used
memcache as it is supposed to be used by adding all the servers using
the addServer function, however we are hesitant to do so because of
the time lag that would be caused by a client having to get data from
another server, the reason we are running 4 identical servers is to
have quick response to many client machines.
Based on all of this we are leaning towards a solution of notifying
all servers of an update. In order not to impact response time and
not to have these servers bogged down in notifications, the best
solution might be one with a master server that notifies the other
servers to invalidate cache on a DB write.
Please let me know if we went wrong in our understanding somewhere.
Any tips or thoughts are greatly appreciated.
Thanks.
On Feb 3, 10:02 pm, Jason Sirota <[email protected]> wrote:
> On Thu, Feb 3, 2011 at 9:10 AM, Dustin <[email protected]> wrote:
>
> > On Feb 3, 2:32 am, Margalit Silver <[email protected]> wrote:
> > > Our system has 4 live servers on a load balancer in an Amazon Cloud.
>
> > > We see in our code that memcache is deleted when a write is done but it
> > > seems that it is only done for that one server, we want that it should be
> > > cleared for that one piece of data on all servers.
>
> > If you are using memcached effectively, a given piece of data will
> > only exist on one server. That's how you achieve massive scale in a
> > caching layer.
>
> > You can get a quick overview here: http://memcached.org/about
>
> Margalit,
>
> Can you give us some more information about how your architecture is set up?
> You say you have 4 live servers on an amazon cloud.
>
> What client are you using to access memcached?
> Can you share your memcache configuration section?
> Can you share a snippet of code that accesses memcached?
>
> As Dustin says, data is not supposed to exist on more than one server, so
> something else may be going on.
>
> Jason