Feature Comparison: Redis vs Ehcache

Both Redis and Ehcache are strong, mature, and popular solutions for the issue of caching in Java. That said, it's understandable that you might have trouble choosing between Ehcache and Redis. In this article, we'll compare the features of Redis and Ehcache so that you can understand which tool is the right fit for your needs.

What is Redis?

Redis is an open-source, in-memory data structure project that can be used as a key-value database, a cache, and a message broker. It includes support for many different abstract data structures, such as strings, lists, and maps. There are a number of options, such as Redisson, for developers who want to use Redis with the Java programming language.

What is Ehcache?

Ehcache is an open-source distributed cache in Java. It includes a number of useful features such as REST and SOAP APIs, as well as memory and disk stores. Ehcache is often used to integrate with other Java frameworks such as Spring, Hibernate and MyBatis.

Redis vs Ehcache Comparison

In this section, we will compare and contrast the features of Ehcache vs. Redis using the Redisson framework.

Distributed collections
Redis + Redisson EhCache

Both Redis and Ehcache include support for distributed Maps in Java. However, Redis also supports many other distributed collections that Ehcache does not, including:

  • Multimap
  • Set
  • List
  • Queue
  • Deque
  • SortedSet
  • ScoredSortedSet
  • PriorityQueue
  • PriorityDeque
  • DelayedQueue
Distributed locks and synchronizers
Redis + Redisson EhCache

Both Redis and Ehcache include support for two different distributed locks in Java: Lock and ReadWriteLock. In addition, Redis supports four other objects from the java.util.concurrent package:

  • Semaphore
  • CountDownLatch
  • FairLock
  • MultiLock

Distributed objects
Redis + Redisson EhCache
Object holder
Reliable Publish/Subscribe
Id Generator

Ehcache does not include any support for distributed objects in Java. On the other hand, Redis has the following distributed objects available for developers:

  • ObjectHolder
  • AtomicLong
  • Publish/Subscribe
  • AtomicDouble
  • Geospatial
  • BitSet
  • BloomFilter
  • BinaryStream
  • HyperLogLog
Advanced cache support
Redis + Redisson EhCache
JCache API
JCache API with near cache (up to 45x faster)
Near Cache
Read-through strategy support
Write-through strategy support
Write-behind strategy support

Both Ehcache and Redis include several features for advanced caching. For example, both solutions support the read-through, write-through, and write-behind caching strategies. In addition, both include support for the standard JCache API.

However, there is one place where Ehcache falls short: the "near cache" feature. Ehcache does not include support for the near cache, which is a local cache that stores frequently accessed data from the heap memory. Redis, meanwhile, enjoys full near cache support.

API architecture
Redis + Redisson EhCache
Asynchronous interface
Reactive stream interface
RxJava2 interface

Redis includes several important components of an API architecture: support for asynchronous interfaces, reactive streams, and RxJava2. However, these features are entirely missing from Ehcache.

Distributed services
Redis + Redisson EhCache

Another issue with Ehcache is that it lacks support for distributed services. These features are present in Redis in the form of:

  • ExecutorService
  • MapReduce
  • SchedulerService
  • RemoteService
  • LiveObjectService
Integration with frameworks
Redis + Redisson EhCache
Spring Cache
Spring Cache with near cache (up to 45x faster)
Hibernate Cache
Hibernate Cache with near cache (up to 5x faster)
MyBatis Cache
MyBatis Cache with near cache (up to 45x faster)
Tomcat Session Manager
Spring Session

Both Redis and Ehcache stand out for their integrations with other Java frameworks. For example, both solutions include support for Spring caching, Hibernate caching, and the Tomcat Session Manager.

However, Redis wins by a nose here, due to its additional support for the Spring Session API for managing user session information.

Redis + Redisson EhCache
SSL support

Data security should be a paramount concern when building a cache in Java. Only Redis includes built-in support for both authentication and SSL; Ehcache provides support for neither.

Custom data serialization
Redis + Redisson EhCache
JSON codec
JDK Serialization
Avro codec
Smile codec
CBOR codec
MsgPack codec
Kryo codec
FST codec
LZ4 compression codec
Snappy compression codec

Both Ehcache and Redis include basic support for data serialization with the JDK serialization API. However, this is all that Ehcache provides, while Redis goes much further. Redis includes support for the following data serialization codecs:

  • JSON
  • FST
  • JDK Serialization
  • CBOR
  • MsgPack
  • Kryo
  • LZ4 compression
  • Snappy compression
Stability and ease of use
Redis + Redisson EhCache
Fully-managed services support
(AWS Elasticache, Azure Cache...)
Large memory amount handling open-source version has limitations

Finally, only Redis (and not Ehcache) can work with cloud platforms such as Amazon Web Services ElastiCache and Microsoft Azure Cache out of the box. These platforms have their own closed-source implementations of Redis that can be used as a fully managed service. In addition, Redis is automatically capable of handling large amounts of memory, which poses issues for the open-source version of Ehcache.

Similar articles