Memcache vs Java Memory

Henley Chiu picture Henley Chiu · Mar 29, 2011 · Viewed 24.2k times · Source

Simple, probably dumb question: Suppose I have a Java server that stores in memory commonly used keys and values which I can query (let's say in a HashMap)

What's the difference between that and using Memcache (or even Redis)? They both store things in memory. Is there a benefit to one or the other? Does Memcache leaves less of a memory footprint? Can store more in less memory? Faster to query? No difference?

Answer

Ned Batchelder picture Ned Batchelder · Mar 29, 2011

Advantages of Java memory over memcache:

  1. Java memory is faster (no network).
  2. Java memory won't require serialization, you have Java objects available to you.

Advantages of memcache over Java memory:

  1. It can be accessed by more than one application server, so your cache will be shared among all your app servers.
  2. It can be accessed by a variety of different servers, so long as they all agree on the key scheme and the serialization.
  3. It will discard expired cache values, so you get time-based invalidation.