With a distributed cache, a subset of the cache is kept locally while the rest is held remotely.

  • In a get operation, if the entry is not available locally, the remote cache will be used and and the entry is added to local cache.
  • In a put operation, both the local cache and remote cache are updated. Other nodes in the cluster also need to be notified to invalidate their local cache as well.

What's a simplest way to achieve this if you implemented it yourself, assuming that nodes are not aware of each other.

Edit My current implementation goes like this:

  • Each cache entry contains a time stamp.
  • Put operation will update local cache and remote cache
  • Get operation will try local cache then remote cache
  • A background thread on each node will check remote cache periodically for each entry in local cache. If the timestamp on remote is newer overwrite the local. If entry is not found in remote, delete it from local.

3 Answers 3


The problem you may want to focus is when to send local caches messages carrying updates of remote caches. On one hand, you could send a message for each changed item, to all other caches. This ensure timely updates but there can be lots of update messages. On the other hand, the local cache can check whether an item is valid just before using it (and after a given timeframe from the same request), asking the remote cache. As another option, you could send updates on several changed items periodically.

The best strategy depends on the system you are going to build. The strategy could be a balance of several factors, such as frequency of updates, traffic generated by update messages, overhead of updating, criticality of missing updates, etc.

Do items of the cache vary frequently with respect to their access? How many local caches do you have? Does the change come from each local cache, a few of these, all, or from the remote cache?

  • Thanks, your answer let me to think more about my question. I am looking for a implementation where nodes are not aware of each other and can only check remote cache for validity.
    – ltfishie
    Commented Feb 9, 2012 at 14:45
  • This seems more a centralized repository (with cache), look for the Repository (or Blackboard) architectural pattern, e.g. in the book by Buschmann et al., Pattern-Oriented Software Architecture Volume 1: A System of Patterns.
    – emit
    Commented Feb 9, 2012 at 15:05
  • Thanks for your answer. I eventually implemented a background thread that would sync local cache with remote cache periodically and it works great.
    – ltfishie
    Commented Feb 19, 2012 at 4:15

Unfortunately I think unless there's a single synchronization point, you can't guarantee the cache to have the most fresh data.

However, if you create the single point, you'd end up with performance issue because everything has to go there first and if it is a distributed system it will be a network call and performance bottleneck.

A better way around this is to push back to the analysts stating that if they have a requirement to have the latest data possible when the data can change then caching is not an option. Instead faster network and hardware is needed, this is many factors cheaper than getting a developers to code some custom caching solution that does not really work anyway.

  • Thanks for your answer. Its not an actual project, I am trying to learn how distributed cache works.
    – ltfishie
    Commented Feb 19, 2012 at 5:38

Well, I see that you are looking for Memcached. This product makes things exactly as you wish, is free and stable. Used by Wikipedia and others. If you want to do your own, you can take a look to it's documentation where the way it works is explained.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.