Memcache vs Java Memory
CachingMemoryMemcachedRedisCaching Problem Overview
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?
Caching Solutions
Solution 1 - Caching
Advantages of Java memory over memcache:
- Java memory is faster (no network).
- Java memory won't require serialization, you have Java objects available to you.
Advantages of memcache over Java memory:
- It can be accessed by more than one application server, so your cache will be shared among all your app servers.
- It can be accessed by a variety of different servers, so long as they all agree on the key scheme and the serialization.
- It will discard expired cache values, so you get time-based invalidation.
Solution 2 - Caching
I just made a benchmark between a concurrent hash map, memcached, and MySQL.
Here are the results:
Type Insert Lookup Remove
ConcurrentHashMap 264ms 93ms 82ms
Memcached 6549ms 5976ms 4900ms
Mysql 55754ms 26002ms 57899ms
A thread pool was used for this benchmark.
Some more information can be found here: http://www.incentergy.de/2013/12/big-data-architecture-patterns-for-performance/
Further the following cache might be an alternative to memcached: https://code.google.com/p/kitty-cache/
Solution 3 - Caching
It depends on what you're wanting. An in-memory map will be faster; data expiry isn't really an issue (see: Google Guava's http://guava-libraries.googlecode.com/svn/tags/release08/javadoc/com/google/common/collect/MapMaker.html">MapMaker</a>;, which can create a map that expires entries after reads and/or writes, and let's not forget things like http://www.opensymphony.com/oscache/">OSCache</a> and http://ehcache.org/">EHCache</a>;, not to mention distributed things like http://gigaspaces.com/">GigaSpaces XAP or http://www.oracle.com/technetwork/middleware/coherence/overview/index.html">Coherence</a>;).
The caching projects (XAP, OSCache, EhCache, Coherence, etc) can distribute cache entries, so you get natural sharding and other facilities; Coherence can manage transactions and write-through, and XAP is actually designed to serve as a system of record (where writing to it gets synchronized and replicated, such that you are using an in-memory data grid as your actual data storage mechanism rather than using a database.)
Memcached is... well, you can access a memcached server instance from a series of machines. Memcached as an API is simply a key/value store, and distribution is entirely accomplished on the client side. It's certainly got the basics, I guess, and it's definitely got multiple language APIs, but it's really pretty limp otherwise.
(BTW, GigaSpaces has a Memcached layer, so you could theoretically use memcached as a system of record...)