60

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?

Henley
  • 21,258
  • 32
  • 119
  • 207

3 Answers3

57

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.
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • 7
    Also using lots of Java Memory makes the garbage colector take longer, so there is a limit to the say you can have a java process even if you have the RAM. memcache does not have a garbage collector that gets slower with more live data items. – Ian Ringrose Mar 29 '11 at 12:25
  • 7
    One could implement time-based invalidation in a Java memory implementation, so I wouldn't consider it an advantage of memcached over Java memory; it's more of an advantage of memcached over specific in-memory implementations. – Max Nanasy May 31 '13 at 21:02
  • @MaxNanasy Yes you can always implement something like LRU cache that works with Java memory. – Swapnil Aug 21 '14 at 18:36
31

I just made a benchmark between a concurrent hash map, memcached, and MySQL.

HashMap vs. memcached vs. 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/

Manuel_B
  • 535
  • 5
  • 10
  • 1
    Because somebody ask me here is a repository with the code: https://github.com/ManuelB/key-value-benchmark – Manuel_B Jun 19 '14 at 07:52
16

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 MapMaker, which can create a map that expires entries after reads and/or writes, and let's not forget things like OSCache and EHCache, not to mention distributed things like GigaSpaces XAP or Coherence).

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...)

Joseph Ottinger
  • 4,911
  • 1
  • 22
  • 23
  • What I basically want is a key-value map in memory that will expire keys as well as remove keys if the memory is low. I'm planning to manipulate this map using Java. Memcached sounds perfect for that purpose. The problem is I want to create a JAR, which I'm planning to distribute to other clients. Other people may not have memcached, or may not want to install memcached. So I want the capabilities of memcached w/o the need to install anything else. A Java version of memcached is what I'm after. – Henley Mar 29 '11 at 13:29
  • Sounds like you want a simple cache, not memcached - memcached is an API for connecting to an *external* server, so if you're talking about in-memory, memcached isn't your target. – Joseph Ottinger Mar 29 '11 at 14:41
  • Sorry for double comment - edit didn't work. What I would do, honestly, is create a custom cache, with a background thread that monitors system resources. If resources are low, expire the oldest entries; also, expire entries if they're past their expiry time. It would be pretty easy to do, and would have the benefit of meeting your requirements exactly. – Joseph Ottinger Mar 29 '11 at 14:43
  • Thanks. But that sounds like non-trivial work. That was why I was attracted to memcached =) I did some research and found out about QuickCached, which looks like an implementation of memcached. – Henley Mar 29 '11 at 14:47
  • **nod** fair enough. I'd think it'd take maybe ten minutes of heads-down coding to write a map like that, but that's probably more time than using a pre-written product would take. – Joseph Ottinger Mar 29 '11 at 14:58
  • 2
    @Hisoka: Google Guava's MapMaker provides soft keys (will be removed if memory is low) and time-based expiration, super easily. – user359996 Sep 29 '11 at 02:07
  • I dont need such a thing anymore hehe.. that idea was scratched ^_^ – Henley Oct 08 '11 at 04:10