11

I need to store session state for a web application that is deployed on multiple servers. I Want to avoid sticky sessions or doing session replication as I want the application to be self-contained and not rely on servlet container specific configurations.

So logically the only option left is to manage the session somewhere else and the logical direction is with a database that all servers share. But that will become a bottleneck (and a single point of failure), plus a relational database isn't really designed to store session data. A distributed NoSQL solution would be a better choice but still that will be another component to manage.

So I was thinking at a fast and distributed Java embeddable cache library that replicates the data across all nodes so that no matter which server the client reaches to have access to the latest state.

Hazelcast seems to be a good choice but I would just be needing a Map without any other features from Hazelcast so it seems a little bit to much for just a distributed container of key-value pairs.

But other than that I can't seem to find any other library to suit the need. Ideally it should be an open source library, something that I can freely use in a commercial product.

Any ideas?

  • I have used EHCache but I guess it is not lighter than Hazelcast... – Nicolas Raoul Jul 16 '14 at 5:49
  • @Nicolas Raoul: Thanks for the suggestion but I'm looking for an open source library (I've updated my question). As far as I know distributed EHCache means Terracotta and BigMemory Max which are licensed. – JohnDoDo Jul 16 '14 at 6:50
  • "Terracotta actively develops, maintains, and supports Ehcache as a professional open source project available under an Apache 2 license." ehcache.org – Nicolas Raoul Jul 16 '14 at 8:32
  • @Nicolas Raoul: that's not what I meant. It is my understanding that the distributed capabilities of EHCache are not available in the open source distribution, e.g. ehcache.org/documentation/get-started/about-distributed-cache. Am I wrong? – JohnDoDo Jul 16 '14 at 9:58
  • EHCache is embedded in the open source product Alfresco, providing distributed cache features. So I am pretty sure the distributed capabilities of EHCache are available in the open source distribution too. That page is pretty misleading, they are trying hard to sell the non-open source version, I would say. – Nicolas Raoul Jul 16 '14 at 13:45
1

Ehcache and Guava caches are pretty popular and useful in-process caches. If using an external process for the cache is possible, Memcached and Redis suit this purpose well.

  • As far as I know Guava Cache is not distributed. Redis is a NoSQL solution written in C and can't be embedded in a Java application (you can embed a java client but not the Redis installation) while Memcached does not work the way I want (which is to have the same data on all the nodes, i.e. replication). – JohnDoDo Jul 23 '14 at 14:29
0

Full disclosure: I work for Alachisoft.

You could check NCache for .Net or TayzGrid for Java distributed cache. Both projects are open source.

Your requirement is to get;

fast and distributed Java embeddable cache library that replicates the data across all nodes so that no matter which server the client reaches to have access to the latest state.

This is what these both freely provide. Use it for what you need it for and leave out the other features.

Brief code overview from the linked blog

import com.alachisoft.TayzGrid.web.caching.Cache;
    import com.alachisoft.TayzGrid.runtime.*;

    public class MyCachingSample {
        private String url = "dbc:msql://20.200.20.1:1114/MyDatabase";


        public Employee GetEmployee(String empId) throws Exception {
            Employee emp = null;
            try {
                Cache cache = TayzGrid.initializeCache("MyDistributedCache");
                emp = (Employee) cache.get(empId);
                //If key not found in cache, Load from the database
                if (emp == null) {
                    //Your logic goes here
                    ResultSet rs = stmt.executeQuery("SELECT * FROM Employee WHERE EmpId ='" + empId + "'");
                    if (rs.next()) {
                        emp = new Employee();
                        emp.EmpId = rs.getString("EmpId");

                        //Cache key for future use
                        cache.insert(empId, emp, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default);
                    }
                }
            } catch (Exception exp) {
                throw exp;
            }
            //return the required object
            return emp;
        }
    }

You'll have to write a simple session provider for yourself to support sticky sessions though or you could buy NCache or TayzGrid to get these features. TayzGrid or NCache can also and is being used like either a distributed cache or an In-Memory Data Grid or sometimes as an In-Memory Key Value Store. It completely depends what your needs are.

If you need support or are planning to use it for an enterprise, then you can always get the paid licenses. It is a more feature rich version and provides support. We have over 10 years of industry experience hence its stable for a full production environment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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