Getting Started with Terracotta Toolkit – Part 3 (Clustered Map)

Did you ever wanted to have a Map which can be clustered across JVM, without doing much work for replicating data, ensuring coherence of data? Terracotta Toolkit allows you to fullfill your dream, Use the same Map API's you have been using for long and see your data being clustered transparently with Terracotta. The fun begins, when you add new clients and they have the data with them without any additional programming.

Lets start with some general information about Toolkit. Refer Toolkit javadoc for more details here

As you can see in the figure above, we have different JVM's, each having a Map instance used by Application. Now if we were to share the same instance across app's running in different nodes, we would have to write a lot of infrastructure code to replicate data, keep data coherent etc.

Clustered Map

The figure above depicts a Clustered Map. The Map instance is visible to App's across JVM's as if they are accessing a local instance. You can achieve this in a few lines, without worrying about the infrastructure code. Lets see how we can achieve this in few simple steps.

Steps to creare Clustered Map

Initialize Toolkit

Get a Map reference

Use the Map

Initializing the Toolkit

Before any operations can be performed using Toolkit, it needs to be initialized first. The initialization code is very simple, just single line

Get/Create the Map

The code above creates a Clustered Map. There is no more infrastructure code that you have to write. This gives us a Clustered Map reference. The best part is, there are no new API's to be learned, we continue to use the same old Map API's.

Its a no-brainer class. Just a simple class for demonstration purpose.

Just to keep life simple, we are going to create a function which populates the Map, and the Nodes can then consume the populated data. Consume here would mean just call a get() and print. In real life scenario, you can mutate the data, and the updated data shall be visible across the cluster.

I can get the code to work properly, except for the very big problem that when I have 2 clients ( 2 separate jvms ) changes to the clustered map on one client or NOT visible to the other. It appears that they each jvm has it’s own copy, though they are calling it by the same name.
Interestingly enough, the type of map returned from the getMap(name) call is a ConcurrentDistributedMap rather than a ConcurrentDistributedServerMap, which is what I expected.
I am using an out of box terracotta 3.4.0 server.

Just a quick question in terms of clustering a map, what is actually the difference between cluster a map using Terracotta toolkit or using terracotta POJO (configuring tc-xml, locking and all those things)?

in short:
I have a map example running fine when server and client are on localhost

when I tried to run the client on a different machine, toolkit connection gets established fine, i can see it in the dev console too.
when i try to put any thing in the map, i get log line saying
WARN – We couldn’t load configuration data from the server at ‘localhost:9510’; retrying. (Error: Connection refused.)
and the operation fails.