0

Please don't mark this question as a duplicate. I read the previous questions, but I am still unable to understand it.

I am currently into a project designed in Java which uses MongoDB for persistence. But due to some performance issues with it, I am asked to use Memcached. But I am unable to figure out how can Memcached help me in doing this.

While surfing, I got more confused because of caching services like Memcache and Memcached. Can someone please explain me how are these different and why does PHP comes into the answer in some questions when Memcached is asked.

I request all to answer clearly and let me know with an example how could I use Memcached into my project. What is Memcache, Memcached, Jcache and SpyMemcached?

If possible, please provide a link to complete Memcached example somewhere.

Java Manz
  • 41
  • 2
  • 5

1 Answers1

8

Memcache and Memcached are the same thing, the "correct name" being Memcached ( http://memcached.org/ ).

JCache is the name of a standard Java API (JSR 107 - https://jcp.org/en/jsr/detail?id=107 ) that provides a generic API to interact with caching layer/solutions. (get/set/remove data from a Key/Value cache to simplify)

So you really want to use a caching layer at the top of MongoDB in your Java application you have to:

  1. Install Memcached somewhere on your infrastructure (if not install already, you can test it quickly with telnet. The default port is 11211, so you can run telnet localhost 11211 to see if it is working.

  2. You have to use a JCache implementation for Memcached, for example this one: https://github.com/toelen/spymemcached-jcache This will allow you to store and get data into a Memcached process running somwhere in your infrastructure.

    Since you are talking about JCache, you are Using Java, it is also possible to use Java based cache that will work in your JVM Directly without having a 3rd party cache/process (memcached). You can find many of them, it could be for example eHCache, JBoss Cache, and most of them expose their API using the standard JCache API.

  3. Now you need to code your Data Access layer to get the data out of MongoDB and set them into the Cache using JCache API. IN this code you will have to check if a data is in the cache, if not query the data from MongoDB, and set it in the cache and use it. Be careful about the eviction strategy.

This document about using JCache in Google App Engine documentation is interesting to see the "pseudo code" https://developers.google.com/appengine/docs/java/memcache/usingjcache (your code will be different but it should help you to see what you have to do in your code.)

The reason why you often see Memcached and PHP together is just because Memcached is the most common caching layer for PHP application, with many many API/FWK that are using this. In Java we have many options, from a pure Java layer to Memcached or other...

However, this is the "overall" approach, but before doing this I would check "why" you are saying that MongoDB is slow, and solve the issue.

Tug Grall
  • 3,410
  • 1
  • 14
  • 16
  • Thanks @Tug for the answer. It is quite clear to me now. You documented all the doubts in point... That's great. Thanks again. I am saying MongoDB is slow because in our application, presently we are hitting the DB multiple times even if we know that the data in the database did not change. Its not correct to say that Mongo is slow. My implementation is wrong. I assume in case I use a caching mechanism, it would be real fast. – Java Manz Aug 26 '14 at 14:40
  • 1
    So overall, do you mean that Memcached is a concept, and JCache implementations like SpyMemCached can help me implement this concept. Am I correct? – Java Manz Aug 26 '14 at 14:43
  • 2
    Hmm not exactly "Caching" is the concept: you can the data in RAM close to your application to avoid call to the persistence layer. Memcached is an implementation of a cache (and more since it is also the protocol). JCache is a "client" API for Caches, "any cache". SpyMemcahed, is the Java client of Memcached based on the Memcached protocol, and spymemcached-jcache is the "implementation" of JCache for the Memcached protocol (in fact a wrapper of the spymemached library). On the principle, since JCache is a standard API, you can use any caching layer under the cover. – Tug Grall Aug 26 '14 at 15:38
  • 1
    On the other topic, so if you know your implementation is wrong, may be it is better to fix that first... Adding a caching layer, will make the application very fast for sure, but you will have to deal with a new set of issues: validity/invalidity of the data in the cache. How long can you keep them in the cache, who is responsible of removing the data from it, what happen when you update the date, etc etc Caching is not a silver bullet it comes with its own concerns to deal with. – Tug Grall Aug 26 '14 at 15:40
  • Thanks @Tug for the suggestion. I will surely look into it. Your answer was really helpful. – Java Manz Aug 27 '14 at 17:46