AuthorsGalder Zamarreño , Manik Surtani , Pete Muir , Vlastimil Eliáš
Target Audience
This guide shows you how to get started using Infinispan. It covers the common uses of Infinispan, and shows you how to get off the ground. It also introduces to some of the essential concepts of Infinispan.
Under development
This guide is still under development. The content you will find here is correct, but there are some missing sections.
Introduction
This guide will walk you through downloading, installing and running Infinispan for the first time. It will then introduce to some of the key features of Infinispan.
Infinispan can be used in a variety of runtimes:
-
Java SE, started by your application
-
an application server which provides Infinispan as a service (such as JBoss AS)
-
bundled as a library in your application, deployed to an application server, and started on by your application (for example, you could use Infinispan with Tomcat or GlassFish)
Infinispan offers four modes of operation, which determine how and where the data is stored:
-
Local, where entries are stored on the local node only, regardless of whether a cluster has formed. In this mode Infinispan is typically operating as a local cache
-
Invalidation, where all entries are stored into a cache store (such as a database) only, and invalidated from all nodes. When a node needs the entry it will load it from a cache store. In this mode Infinispan is operating as a distributed cache, backed by a canonical data store such as a database
-
Replication, where all entries are replicated to all nodes. In this mode Infinispan is typically operating as a data grid or a temporary data store, but doesn't offer an increased heap space
-
Distribution, where entries are distributed to a subset of the nodes only. In this mode Infinispan is typically operating as a data grid providing an increased heap space
Invalidation, Replication and Distribution can all use synchronous or asynchronous communication.
Infinispan offers two access patterns, both of which are available in any runtime:
-
Embedded into your application code
-
As a Remote server accessed by a client (REST, memcached or Hot Rod)
This guide will introduce to each of the runtime options, access patterns and modes of operations by walking you through simple applications for each. All these applications are available in the Infinispan Quickstart distribution.
Downloading and installing Infinispan
To run the Infinispan, you'll need
-
Java 1.6
-
Maven 3, if you wish to use the quickstart examples or create a new project using the Infinispan archetype
-
the Infinispan distribution zip, if you wish to use Infinispan in server mode, or want to use the jars in an ant project
-
the Infinispan Quickstart zip, if you want to follow along with the projects discussed in the guide
If you already have any of these pieces of software, there is no need to install them again!
Choose your Java runtime, and follow their installation instructions. For example, you could choose one of:
Follow the official Maven installation guide if you don't already have Maven 3 installed. You can check which version of Maven you have installed (if any) by running mvn --version. If you see a version newer than 3.0.0, you are ready to go.
You can also deploy the examples using your favorite IDE. We provide instructions for using Eclipse only.
Finally, download Infinispan and the Infinispan Quickstart from the download page.
Infinispan in action - GUIDemo
TODO
Using Infinispan as an embedded cache in Java SE
Running Infinispan in embedded mode is very easy. First, we'll set up a project, and then we'll run Infinispan, and start adding data.
embedded-cache quickstart
All the code discussed in this tutorial is available in the embedded-cache quickstart.
Creating a new Infinispan project
The only thing you need to set up Infinispan is add it's dependencies to your project. If you are using Maven (or another build system like Gradle or Ivy which can use Maven dependencies), then this is easy. Just add:
pom.xml snippet
013. ~ This software is distributed in the hope that it will be useful,
014. ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
015. ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016. ~ Lesser General Public License for more details.
017. ~
to your <dependencies> section of the POM. You'll need to substitute ${infinispan.version} for the version of Infinispan you wish to use.
Which version of Infinispan should I use?
We recommend using the latest final version of Infinispan. All releases are displayed on the downloads page.
You'll also need to enable the JBoss Maven repository. We recommend adding a profile:
pom.xml snippet
022. -->
023. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
024. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
025. <modelVersion>4.0.0</modelVersion>
026. <groupId>org.infinispan.quickstart</groupId>
027. <artifactId>embedded-cache-quickstart</artifactId>
028. <version>7.0.0-SNAPSHOT</version>
029.
030. <name>Infinispan Embedded Cache Quickstart</name>
031. <description>This quickstart demonstrates embedded cache running on a single node in Java SE.</description>
032.
033. <url>http://jboss.org/infinispan</url>
034. <licenses>
035. <license>
036. <name>GNU Lesser General Public License</name>
037. <url>http://www.gnu.org/copyleft/lesser.html</url>
038. <distribution>repo</distribution>
039. </license>
040. </licenses>
041.
042. <properties>
043. <!-- Explicitly declaring the source encoding eliminates the following
044. message: -->
045. <!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered
046. resources, i.e. build is platform dependent! -->
047. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
048.
049. <infinispan.version>7.1.0.Final</infinispan.version>
050. </properties>
051.
052. <dependencies>
053. <dependency>
054. <groupId>org.infinispan</groupId>
055. <artifactId>infinispan-embedded</artifactId>
056. <version>${infinispan.version}</version>
057. </dependency>
058. </dependencies>
059.
060. <build>
Alternatively, you can use the POM from the quickstart that accompanies this tutorial.
If you are using Ant, or another build system which doesn't provide declarative dependency management, then the Infinispan distribution zip contains a lib/ directory. Add the contents of this to the build classpath.
Running Infinispan on a single node
In order to run Infinispan, we're going to create a main method in the Quickstart class. Infinispan comes configured to run out of the box; once you have set up your dependencies, all you need to do to start using Infinispan is to create a new cache manager and get a handle on the default cache.Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/embedded-cache/src/main/java/Quickstart.java status code: 404.
We now need a way to run the main method! If you are using Maven, the best approach is to copy all the project dependencies to a directory, and at the same time compile the java classes from our project:
$> mvn clean compile dependency:copy-dependencies -DstripVersion
Having done that, we can run the main method:
$> java -cp target/classes/:target/dependency/* Quickstart
You should see Infinispan start up, and the version in use logged to the console.
Congratulations, you now have Infinispan running as a local cache!
Use the default cache
Infinispan exposes a Map-like, JSR-107-esque interface for accessing and mutating the data stored in the cache. For example:Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/embedded-cache/src/main/java/DefaultCacheQuickstart.java status code: 404.
Infinispan offers a thread-safe data-structure:Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/embedded-cache/src/main/java/DefaultCacheQuickstart.java status code: 404.
By default entries are immortal but you can override this on a per-key basis and provide lifespans.Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/embedded-cache/src/main/java/DefaultCacheQuickstart.java status code: 404.
Use a custom cache
Each cache in Infinispan can offer a different set of features (for example transaction support, different replication modes or support for eviction), and you may want to use different caches for different classes of data in your application. To get a custom cache, you need to register it with the manager first:Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/embedded-cache/src/main/java/CustomCacheQuickstart.java status code: 404.
The example above uses Infinispan's fluent configuration, which offers the ability to configure your cache programmatically. However, should you prefer to use XML, then you may. We can create an identical cache to the one created with a programmatic configuration:
infinispan.xml
24. <infinispan
25. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
26. xsi:schemaLocation="urn:infinispan:config:7.0 http://www.infinispan.org/schemas/infinispan-config-7.0.xsd"
27. xmlns="urn:infinispan:config:7.0">
28.
29. <cache-container default-cache="default">
30. <local-cache name="xml-configured-cache">
31. <eviction strategy="LIRS" max-entries="10" />
32. </local-cache>
33. </cache-container>
We then need to load the configuration file, and use the programmatically defined cache:Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/embedded-cache/src/main/java/XmlConfiguredCacheQuickstart.java status code: 404.
Using Infinispan as an embedded data grid in Java SE
Clustering Infinispan is simple. Under the covers, Infinispan uses JGroups as a network transport, and JGroups handles all the hard work of forming a cluster.
Sharing JGroups channels
By default all caches created from a single CacheManager share the same JGroups channel and multiplex RPC messages over it. In this example caches 1, 2 and 3 all use the same JGroups channel:
EmbeddedCacheManager cm = // get CacheManager from somewhere
Cache<Object, Object> cache1 = cm.getCache("replSyncCache");
Cache<Object, Object> cache2 = cm.getCache("replAsyncCache");
Cache<Object, Object> cache3 = cm.getCache("invalidationSyncCache");
clustered-cache quickstart
All the code discussed in this tutorial is available in the clustered-cache quickstart.
Running Infinispan in a cluster
It is easy set up a clustered cache. This tutorial will show you how to create two nodes in different processes on the same local machine. The quickstart follows the same structure as the embedded-cache quickstart, using Maven to compile the project, and a main method to launch the node.
If you are following along with the quickstarts, you can try the examples out. First, compile the project:
$> mvn clean compile dependency:copy-dependencies -DstripVersion
The quickstart contains two examples of a clustered cache, one in replication mode and one distribution mode. To run the replication mode example, we need to launch both nodes from different consoles. For the first node:
$> java -cp target/classes/:target/dependency/* org.infinispan.quickstart.clusteredcache.replication.Node0
And for the second node:
$> java -cp target/classes/:target/dependency/* org.infinispan.quickstart.clusteredcache.replication.Node1
You should see JGroups and Infinispan start up on both consoles, and after about 15s the cache entry log message appear on the console of the first node.
To run the distribution mode example, we need to launch three nodes from different consoles. For the first node:
$> java -cp target/classes/:target/dependency/* org.infinispan.quickstart.clusteredcache.distribution.Node0
And for the second node:
$> java -cp target/classes/:target/dependency/* org.infinispan.quickstart.clusteredcache.distribution.Node1
And for the third node:
$> java -cp target/classes/:target/dependency/* org.infinispan.quickstart.clusteredcache.distribution.Node2
You should see JGroups and Infinispan start up on both consoles, and after about 15s see the 10 entries added by third node distributed to the first and second nodes.
clustered-cache quickstart architecture
Logging changes to the cache
An easy way to see what is going on with your cache is to log mutated entries. An Infinispan listener is notified of any mutations:
LoggingListener.java
18. * You should have received a copy of the GNU Lesser General Public
19. * License along with this software; if not, write to the Free
20. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
22. */
23. package org.infinispan.quickstart.clusteredcache.util;
24.
25. import org.infinispan.notifications.Listener;
26. import org.infinispan.notifications.cachelistener.annotation.CacheEntryCreated;
27. import org.infinispan.notifications.cachelistener.annotation.CacheEntryModified;
28. import org.infinispan.notifications.cachelistener.annotation.CacheEntryRemoved;
29. import org.infinispan.notifications.cachelistener.annotation.TopologyChanged;
30. import org.infinispan.notifications.cachelistener.event.CacheEntryCreatedEvent;
31. import org.infinispan.notifications.cachelistener.event.CacheEntryModifiedEvent;
32. import org.infinispan.notifications.cachelistener.event.CacheEntryRemovedEvent;
33. import org.infinispan.notifications.cachelistener.event.TopologyChangedEvent;
Listeners methods are declared using annotations, and receive a payload which contains metadata about the notification. Listeners are notified of any changes. Here, the listeners simply log any entries added or removed.
The replication mode example contains two nodes, each of which are started in a separate process. The nodes are very simple, Node0 starts up, registers a listener that logs any changes, and waits for the cluster to form. Node1 starts up, waits for the cluster to form, and then adds an entry. The interesting work happens in the common super class, examined in Configuring a replicated data-grid.
Waiting for the cluster to form
Infinispan only replicates data to nodes which are already in the cluster. If a node is added to the cluster after an entry is added, it won't be replicated there. In order to see replication take effect, we need to wait until Both nodes make use of the utility class ClusterValidation, calling it's waitForClusterToForm to achieve this. We won't dig into how this works here, but if you are interested take a look at the code.
The distribution mode example contains three nodes, each of which are started in a separate process. The nodes are very simple, Node0 and Node1 start up, register listeners that logs any changes, and wait for the cluster to form. Node2 starts up, waits for the cluster to form, and then adds 20 entries. Each entry get's distributed to it's owners, and you will see some entries add on Node0 and some on Node1. You'll notice that Node2 gets notified of all adds - this is just because it is the node which adds the entry, it doesn't reflect that the fact that all these entries are stored there! The interesting work happens in the common super class, examined in Configuring a distributed data-grid.
Configuring the cluster
First, we need to ensure that Infinispan is cluster aware. Infinispan provides a default configuration for a clustered cache:Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/clustered-cache/src/main/java/org/infinispan/quickstart/clusteredcache/replication/AbstractNode.java status code: 404.
GlobalConfiguration.getClusteredDefault() is a quick way to get a preconfigured, cluster-aware GlobalConfiguration and can be used as a starting point to fine tuning the configuration.
Tweaking the cluster configuration for your network
Depending on your network setup, you may need to tweak your JGroups set up. JGroups is configured via an XML file; the file to use can be specified via the GlobalConfiguration:Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/clustered-cache/src/main/java/org/infinispan/quickstart/clusteredcache/replication/AbstractNode.java status code: 404.
The JGroups documentation provides extensive advice on getting JGroups working on your network. If you are new to configuring JGroups, you may get a little lost, so you might want to try tweaking these configuration parameters:
TODO - add more tips!
You can also configure the JGroups properties to use in Infinispan's XML configuration:Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/clustered-cache/src/main/resources/infinispan-replication.xml status code: 404.
Configuring a replicated data-grid
In replicated mode, Infinispan will store every entry on every node in the grid. This offers high durability and availability of data, but means the storage capacity is limited by the available heap space on the node with least memory.
The cache should be configured to work in replication mode (either synchronous or asynchronous), and can otherwise be configured as normal. For example, if you want to configure the cache programatically:Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/clustered-cache/src/main/java/org/infinispan/quickstart/clusteredcache/replication/AbstractNode.java status code: 404.
You can configure an identical cache using XML:
cfg.xml:Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/clustered-cache/src/main/resources/infinispan-replication.xml status code: 404.Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/clustered-cache/src/main/java/org/infinispan/quickstart/clusteredcache/replication/AbstractNode.java status code: 404.
Configuring a distributed data-grid
In distributed mode, Infinispan will store every entry on a subset of the nodes in the grid (controlled by the parameter numOwners, which controls how many owners each entry will have). Compared to replication, distribution offers increased storage capacity, but with reduced availability (increased latency to access data) and durability. Adjusting the number of owners allows you to obtain the trade off between space, durability and availability.
Infinispan also offers a topology aware consistent hash which will ensure that the owners of entries are located in different data centers, racks and nodes to offer improved durability in case of node or network outages.
The cache should be configured to work in distibuted mode (either synchronous or asynchronous), and can otherwise be configured as normal. For example, if you want to configure the cache programatically:Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/clustered-cache/src/main/java/org/infinispan/quickstart/clusteredcache/distribution/AbstractNode.java status code: 404.
You can configure an identical cache using XML:
cfg.xml:Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/clustered-cache/src/main/resources/infinispan-distribution.xml status code: 404.Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/clustered-cache/src/main/java/org/infinispan/quickstart/clusteredcache/replication/AbstractNode.java status code: 404.
Creating your own Infinispan project
TODO
Using Infinispan as a second level cache for Hibernate
TODO
Accessing an Infinispan data grid remotely
Using Hot Rod to access an Infinispan data-grid
TODO
Using REST to access an Infinipsan data-grid
TODO
Using memcached to access an Infinispan data-grid
TODO
Using Infinispan in JBoss AS 7
TODO
Using Infinispan in servlet containers (such as Tomcat or Jetty) and other application servers (such as GlassFish)
TODO
Monitoring Infinispan