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.

Reply via email to