Eviction and Hazelcast

Eviction

 

This article briefly describes "what is eviction?" then evaluates eviction capabilities of Hazelcast.

 


What is eviction?
It is a common practice to cache commonly accessed data in memory.
But as memory is still an expensive resource, you probably want to limit the size of your cache. 
When your cache reaches the limit, you have to remove (evict) some objects to get a place for new objects.

Hazelcast and Eviction
You can use Hazelcast as distributed cache for your application.
Although distributed systems have advantage on memory, you still need to think about eviction, how to put limit on cache size.

Hazelcast is powerful with its eviction capabilities; you can set global eviction policy also set lifetime for values seperately on maps.
Let's try and test Hazelcast eviction.

1- Simple Java Project

What is most charming about Hazelcast is its simplicity. 
Distributing data over nodes seem so complex but using Hazelcast is not different than using a simple StringUtils library.
So just create a Java Project in your ide and add hazelcast.jar to your classpath.
Or if you use maven:

2- Let's code

Let's fill our distributed map with default hazelcast settings.

And the output:
Current Map Size:1000
Current Map Size:2000
Current Map Size:3000
Current Map Size:4000
Current Map Size:5000
Current Map Size:6000
java.lang.OutOfMemoryError

 

In default configuration hazelcast do not have eviction policy, as you see cache grows without limit. That is not the case we favour.

 

3. Global Eviction Policy: Limit Map Size

 

What we want to limit the maximum size of the map, so eviction should occur when size reaches the limit.
So we should add needed configuration to hazelcast.xml. You can either copy hazelcast-default.xml and put as hazelcast.xml to your classpath or you may specify config file location in code as follows.

So here the config part we changed: 

We set the max size 3000 for maps, and as policy we have set Least Recently Used items to be evicted.
You may choose LFU (Least Frequently Used) depending on your scenario.
Also we set 25 as eviction percentage, so when the map size reaches limit, 25% of items will be removed from Map.

Now execute the same just with the custom hazelcast.xml.
Here is the output:
Current Map Size:1000
Current Map Size:2000
Current Map Size:3000
Current Map Size:2313
Current Map Size:2329
Current Map Size:2333
Current Map Size:2334
Current Map Size:2335
...............................
As you see hazelcast starts eviction when size reaches 3000. 

4. Global Eviction Policy: Limit Entry Life Span
Now we will set maximum seconds to live for each entries in the map.
We will make the following changes to hazelcast.xml

Also let's track the time in our test code.

Here is the output:
Current Map Size:1000 Seconds:3
Current Map Size:2000 Seconds:6
Current Map Size:3000 Seconds:9
Current Map Size:3085 Seconds:13
Current Map Size:3052 Seconds:16
Current Map Size:2992 Seconds:19
Current Map Size:3024 Seconds:23
Current Map Size:2883 Seconds:26
We see that hazelcast starts to evict entries older than 10 seconds.

5. Limiting Life Span of Specific Entries
Besides setting global lifespan we can also set different life span values for each entries.
For this purpose we can use default hazelcast xml but change the code.
In this test we will set 10 seconds time-to-live for half of the entries.

And the output:

Current Map Size:1000 Seconds:3
Current Map Size:2000 Seconds:6
Current Map Size:3000 Seconds:9
Current Map Size:3539 Seconds:13
Current Map Size:4025 Seconds:16
Current Map Size:4493 Seconds:19
Current Map Size:4938 Seconds:23

So half of entries are started to be evicted after tenth seconds.

To sum-up we have covered the different eviction capabilities supported by hazelcast, in the simplest manner..