Mindmajix

Configuring hibernate cache

The hibernate cache container is a key element of your configuration because it handles the data tier which is the backend of every application. As you probably know, JBoss uses hibernate as default JPA provider, so the concepts described in this chapter apply both for hibernate applications (configured to run on JBoss AS) and for JPA-based
applications.

Hibernate caches are conceptually different from the session-based caches because they are based on a different assumption, that is you have a permanent storage for your data (the database files), so it’s not necessary to replicate or distribute copies of the Entities across the cluster in order to achieve high availability. You just need to inform your nodes when data has been modified, so it needs to be invalidated.

If a cache is configured for invalidation rather than replication, every time data is changed in a cache, other caches in the cluster receive a message informing them that their data is now stale and should be evicted from memory.

Screenshot_47

Screenshot_48

The benefit of this is twofold: network traffic is minimized as invalidation messages are very small compared to replicating updated data, and also that other caches in the cluster look up modified data in a lazy manner, only when needed.

Now let’s see in practice how the mechanism works: whenever an new entity or collection is read from database, it’s only cached locally in order to reduce intra-cluster traffic:
<cache-container name=”hibernate” default-cache=”local-query”>
<local-cache name=”local-query”>
<eviction strategy=”LRU” max-entries=”10000″/>
<expiration max-idle=”100000″/>
</local-cache>
</cache-container>
The local-query cache is configured by default to store up to 10000 entries in a LRU vector. Each entry will be evicted from the cache automatically if it has been idle for 100 seconds.

Once that a cache entity is updated, your cache will send a message to other members of the cluster telling them that the entity has been modified. Here, the invalidationcache comes into play:
<invalidation-cache mode=”SYNC” name=”entity”>
<eviction strategy=”LRU” max-entries=”10000″/>
<expiration max-idle=”100000″/>
</invalidation-cache>

By default invalidation uses the same eviction and expiration settings as for local query caching, that is, the maximum number of entries are 10,000 and the idle time before expiration is 100 seconds.

The invalidation too can be synchronous (SYNC) or asynchronous (ASYNC), and just as in the case of replication, synchronous invalidation blocks until all caches in the cluster receive invalidation messages and have evicted stale data while asynchronous invalidation works in a fire-and-forget mode, where invalidation messages are
broadcast but doesn’t block and wait for responses.

By default, entities and collections are configured to use READ_COMMITTED as cache isolation level. It would, however, make sense to configure REPEATABLE_READ if the application evicts/clears entities from the hibernate session and then expects to repeatedly re-read them in the same transaction. If you really need to use REPEATABLE_READ, you can simply configure entities or collections to use “entityrepeatable”
cache.

<invalidation-cache mode=”SYNC” name=”entity”>
. . . . . . .

<locking isolation=”REPEATABLE_READ”/>
</invalidation-cache>

The last piece of code contained in the Infinispan subsystems concerns with the timestamp cache. The timestamp cache keeps track of the last update timestamp for each table (this timestamp is updated for any table modification).

This cache is strictly connected with the query cache, which is used to store the result set of a query made against the database. We will discuss more about query cache in the section named “Entity clustering”; however in short, if the query cache is enabled, any time a query is issued, the query cache is checked before issuing for a query. If the
timestamp of the last update on a table is greater than the time the query results were cached, then the entry is removed and the lookup is a miss.

<replicated-cache mode=”ASYNC” name=”timestamps”>
<eviction strategy=”NONE”/>
</replicated-cache>

By default, timestamps cache is configured with asynchronous replication as clustering mode. Local or invalidated cluster modes are not allowed, since all cluster nodes must store all timestamps. As a result, no eviction/expiration is allowed for timestamp caches either.

Using replication for hibernate cache

There are situations when you could desire to replicate your entity cache across other cluster nodes, instead of using local caches and invalidation. This can be true when the following conditions are met:

  • Queries executed are quite expensive.
  • Queries are likely to be repeated in different cluster nodes.
  • Queries are unlikely to be invalidated out of the cache (Hibernate must invalidate query results from the cache any time any instance of one of the entity classes involved in the query’s WHERE clause changes.)

In order to switch to a replicated cache, you have to configure your default-cache attribute as follows:

<cache-container name=”hibernate” default-cache=”replicated-cache”>
. . . . .
</cache-container>

Advanced Infinispan configuration

Infinispan has, however, wealth of options available to further customize your cache. In this section, we will discuss customizing the thread configuration and the default transport configuration.

Configuring Infinispan threads Just as for the JGroups transport, you can externalize your Infinispan thread
configuration, moving it into the thread pool subsystem. The following thread pools can be configured on a cache-container basis:

Screenshot_49

Customizing the thread pool can be advantageous in some cases, for example, if you plan to apply a cache replication algorithm, then it could be worthy to choose the number of threads used for replicating data. In the following example, we are externalizing the thread pools of the web’s cache-container by defining up to twentyfive threads for transporting data across other nodes and five threads for replicating data.

<subsystem xmlns=”urn:jboss:domain:infinispan:1.0″ default-cachecontainer=”
cluster”>
<cache-container name=”web” default-cache=”repl” listenerexecutor=”
infinispan-listener” eviction-executor=”infinispaneviction”
replication-queue-executor=”infinispan-repl-queue”>
<transport executor=”infinispan-transport”/>
</cache-container>
</subsystem>
. . . . . . .
<subsystem xmlns=”urn:jboss:domain:threads:1.0″>
<thread-factory name=”infinispan-factory” priority=”1″/>

<bounded-queue-thread-pool name=”infinispan-transport”/>
<core-threads count=”1″/>
<queue-length count=”100000″/>
<max-threads count=”25″/>
<thread-factory name=”infinispan-factory”/>
</bounded-queue-thread-pool>
<bounded-queue-thread-pool name=”infinispan-listener”/>
<core-threads count=”1″/>
<queue-length count=”100000″/>
<max-threads count=”1″/>
<thread-factory name=”infinispan-factory”/>
</bounded-queue-thread-pool>
<scheduled-thread-pool name=”infinispan-eviction”/>
<max-threads count=”1″/>
<thread-factory name=”infinispan-factory”/>
</scheduled-thread-pool>
<scheduled-thread-pool name=”infinispan-repl-queue”/>
<max-threads count=”5″/>
<thread-factory name=”infinispan-factory”/>
</scheduled-thread-pool>
</subsystem>

Configuring Infinispan transport

The Infinispan subsystem uses the JGroups subsystem to provide the foundation for the network transport of cache data. By default, cache containers use the default-stack, which is defined into the JGroups subsystem.

<subsystem xmlns=”urn:jboss:domain:jgroups:1.0″ default-stack=”udp”>
</subsystem>
You can, however, choose a different transport for each cache container, for example, if you wanted to use TCP as transport for the web cache container:
<cache-container name=”web” default-cache=”repl”>
<transport stack=”tcp”/>
</cache-container>
The default UDP transport is usually suitable for large clusters or if you are using replication or invalidation as it minimizes opening too many sockets.

The TCP stack performs better for smaller clusters, in particular, if you are using a distribution, as TCP is more efficient as a point-to-point protocol.


 

0 Responses on Configuring hibernate cache"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.