Skip to main content

Cache configuration and sharing in the cluster

Info

The page describes how caches of Mercury DB (HgDB) 3.0 are configured (Ehcache 3), which of them are shared between the nodes of a cluster (Terracotta), how a client can limit refreshing the caches in a request, and how the server can hand over ready objects and Lucene documents between nodes.

Refreshing caches by layer​

The context field cacheUsage set to REFRESH makes a service skip the cached data and read it again. Caches work in several layers, so the context parameter cache.refresh.layers (in requestProperties) says which layers the client refreshes. See Context.

Configuration files​

FilePurpose
ehcacheDefault.xmlThe configuration used by the server: the connection to Terracotta, all regions, the templates and the expiry.
ehcacheCommon.xmlA smaller set of the basic regions (the core cache, the Hibernate regions and the transaction caches) with the same connection to Terracotta.

The files are in the server configuration directory (IBPM/mercury/config). The file that is used is given by the cache.configPath parameter in mercury.properties. The regions that are shared in the cluster are defined in both files with the same sizes, except CaseMetadataCache, CaseObjectMapCacheShared and CaseMapperShared, which are only in ehcacheDefault.xml.

Caches shared in the cluster​

A cache is shared between the nodes only when it has a clustered resource in <eh:resources>: <terracotta:clustered-dedicated unit="MB">N</terracotta:clustered-dedicated>. The element <terracotta:clustered-store consistency="strong"/> alone only sets the consistency and does not make the cache shared. heap stays a local layer at the client.

RegionContentClustered sizeExpiry
default-update-timestamps-regionTimes of the last change of tables (Hibernate). A write on one node makes the query results cached on the other nodes out of date. Consistency eventual (see Failure of a node and the consistency of regions).4 MBnone
CaseGlobalTransactionCacheCases in active global transactions (used, among others, by the index monitor).64 MB86400 s
CaseMetadataCacheMetadata of case types (TypeCase) by identifier.64 MB3600 s
CaseObjectMapCacheSharedReady case objects, selectively (see below).128 MB3600 s
CaseMapperSharedReady Lucene documents handed over between nodes (see below).128 MB600 s

All other caches (results of searches, HTTP responses, security context, objects of the local cache CaseObjectMapCache and the like) are local to a node.

The same size on every node

The same region alias must have the identical clustered-dedicated size in all configurations: server, user profiles, test and Docker files. A different size ends the start of the application with InvalidServerStoreConfigurationException. After changing the size, delete the region from the Terracotta server (destroyCache, no clients attached) or restart the server.

The sum of the clustered sizes of all regions (here 388 MB) must fit in the server resource primary-server-resource (tc-config.xml). The server JVM option -XX:MaxDirectMemorySize should be greater than the sum of the off-heap resources.

Example of a clustered region (ehcacheDefault.xml)
<eh:cache alias="CaseMetadataCache">
<eh:key-type>java.lang.String</eh:key-type>
<eh:value-type>pl.slawas.common.cache.ehcache.Element</eh:value-type>
<eh:expiry>
<eh:ttl unit="seconds">3600</eh:ttl>
</eh:expiry>
<eh:resources>
<eh:heap unit="entries">1000</eh:heap>
<terracotta:clustered-dedicated unit="MB">64</terracotta:clustered-dedicated>
</eh:resources>
<terracotta:clustered-store consistency="strong"/>
</eh:cache>

Values kept in clustered caches must be serializable.

Terracotta server​

The server must match the Ehcache client (ehcache-clustered 3.10.8, Terracotta 5.9.5) and the names in the configuration: server default-server, port 9510 and the resource primary-server-resource (address in the XML: terracotta://<host>:9510/default-server). Data of the caches are kept off-heap and do not survive a restart of the server, which is intended.

Delay of Terracotta and the choice of regions​

Every operation on a clustered region costs a round trip to the Terracotta server. A write of a case makes a dozen or so such operations (Hibernate timestamps of tables), so the delay decides on the cost. Measurements of the Java client:

ClientWriteRead (by another client)
Linux (container) → Linux server0.55–0.88 ms0.26–0.37 ms
Windows → Linux or Windows server12.5–14.0 ms12.8–15.6 ms

The delay of 13–15 ms comes from the Java client on Windows (it does not depend on the server or on the size of an entry). Production environments run on Linux. Measure the delay from the application node before you switch the application to the server.

Failure of a node and the consistency of regions​

A problem of one node must not slow down the others. The other nodes work harder when a node is missing, but they must not wait for it. Two settings decide how a clustered cache behaves when a node does not answer.

Consistency of a region​

The element <terracotta:clustered-store consistency="..."> sets how a write to a clustered region reaches the other nodes:

ConsistencyA writeFailing node
strongWaits until every client that holds the key in its local heap confirms invalidating it.A frozen or cut-off node blocks the writes of the other nodes until the Terracotta server drops its connection (see the lease below).
eventualReturns at once. The other nodes drop their copy later.No influence on the other nodes. A node may read a stale entry for a short time.

Use strong where nodes coordinate through the region (CaseGlobalTransactionCache and similar). The region default-update-timestamps-region uses eventual. It only holds the times of the last change of tables. The cost of eventual is a short window in which another node may return an outdated result of a cached query. The next invalidation closes the window.

Region with eventual consistency (ehcacheDefault.xml, ehcacheCommon.xml)
<eh:cache alias="default-update-timestamps-region">
<eh:key-type>java.lang.String</eh:key-type>
<eh:value-type>pl.slawas.common.cache.ehcache.Element</eh:value-type>
<eh:resources>
<eh:heap unit="entries">1000</eh:heap>
<terracotta:clustered-dedicated unit="MB">4</terracotta:clustered-dedicated>
</eh:resources>
<terracotta:clustered-store consistency="eventual"/>
</eh:cache>
Changing the consistency of an existing region

The Terracotta server keeps the consistency together with the region. After changing it, destroy the region on the server (no clients attached) or restart the server. Otherwise a node ends its start with InvalidServerStoreConfigurationException. Change the value in all configurations at the same time.

Lease of the connection to the server​

The Terracotta server treats a client as alive as long as the client renews the lease of its connection. A client that is frozen (a long garbage collection, a stopped process) or cut off by the network (without closing the connection) holds the lease. A write in a strong region waits for it until the lease expires. The default length is 150 s, so every write to such a region can wait about 10 s and the whole stall lasts over two minutes.

The parameter lease-length in the server configuration (tc-config.xml) shortens this time. The delivered configurations (ehcache/conf/1, 2 and 3 of the Docker kit) set 20 s:

tc-config.xml
<plugins>
<service xmlns:lease="http://www.terracotta.org/service/lease">
<lease:connection-leasing>
<lease:lease-length unit="seconds">20</lease:lease-length>
</lease:connection-leasing>
</service>
<config>
<ohr:offheap-resources>
<ohr:resource name="primary-server-resource" unit="MB">1024</ohr:resource>
</ohr:offheap-resources>
</config>
</plugins>

The lease must be longer than the longest expected pause of the application node (for example a long garbage collection). A node paused for longer than the lease is disconnected by the server. The client reconnects by itself, without restarting the application. In the test (a pause of 40 s with a lease of 20 s) the node returned to the shared region after the pause, the first read returned no value (a cache miss, the data is then read from the database) and the following reads returned current values, with no stale entries and no errors. The node loses the content of its local heap layer on the way. Keep the pause of the JVM below the lease anyway: during the pause the other nodes wait for the lease to expire (a strong region), and the paused node itself serves no requests.

Measured behavior​

The test used one Terracotta server (5.5.1, Ehcache 3.6.1), a client that writes the same key every second and a second client that holds the key in its heap. Time of a write when the second client fails:

Failure of the second clientstrong, lease 150 sstrong, lease 20 seventual
the process is killed (the connection closes)no influence (6 ms)–no influence
a pause of 10 sone write of 9.7 s––
a frozen JVM (long GC)every write 10 s, about 140 s in totalabout 20 s5 ms
a cut network (packets dropped)like a frozen JVMabout 20 s4 ms

A real failure of a node (the process or the container dies) does not block the others: on a three-node cluster the stop of node2 did not change the time of writes on node1 (0.12–0.16 s).

Templates and expiry of dynamic caches​

Caches created while the system works (values of dictionary attributes, security context and the like) are created from the template default of ehcacheDefault.xml. The template uses the expiry policy CustomExpiryPolicy, so the time to live (TTL) set in the parameter definition (ParamDefinition.timeToLiveSeconds) or in the parameter cache.timeToLiveSeconds is respected; the default is 86400 s. A read of an entry does not extend its life.

Caches of values of dictionary attributes are created from the template attribute-values (local, with the same expiry policy). Sharing them in the cluster requires adding a clustered resource to this template.

Sharing ready case objects​

The transformation of a case into an object (MrcObject) costs 3–5 ms on average and about 60 ms for a very large case (e.g. a service order with 28 properties). A read from a clustered cache costs the delay of Terracotta, so it pays only for heavy cases. Therefore the cache CaseObjectMapCacheShared is used selectively:

  • an object goes to the shared cache only when its measured transformation cost exceeds the threshold,
  • a lookup in the shared cache is made only for cases and types that turned out heavy on this node (the cost of a type is learned as an exponential average),
  • a write is asynchronous,
  • an entry is valid only for the same modification time of the case and the same context (decoding method, formats, level, language and time zone).
ParameterDefaultDescription
mercury.cache.caseObject.shared.enabledtruefalse turns the shared cache of case objects off. It also stays off when the region is not clustered.
mercury.cache.caseObject.shared.minCostMs25The cost of transforming a case (ms) from which the object goes to the shared cache. On Linux (delay below 1 ms) a lower value of about 5 is reasonable.

The parameters are set in mercury.properties.

Handing Lucene documents over between nodes​

Mapping a case to a Lucene document costs about 5 ms. Every node of the cluster indexes the same changes, so without sharing every node maps the same cases. The node of the index secretary can instead hand the ready documents over to the other nodes through the region CaseMapperShared. See Handing documents over between nodes.

ParameterDefaultDescription
mercury.cache.luceneDocument.shared.enabledtruefalse turns the handover of documents off. It also stays off when the region CaseMapperShared is not clustered.
mercury.cache.luceneDocument.shared.writerThreads4The number of threads that write the documents to the region CaseMapperShared (1-32). Every write to the clustered region waits for the answer of the Terracotta server, so one thread would limit the publication of a big package.
mercury.cache.luceneDocument.shared.notifyBeforeWritetruetrue: the secretary notifies the other nodes after publishing the documents and before its own write to the index. false: after its own write and commit (the former order).

Monitoring​

The shared caches log to the categories org.mercury.caches.core.SharedCaseObjectStore and org.mercury.lucene.handoff.SharedLuceneDocumentStore. At the level INFO the server reports starting a mechanism and a change of the class of a type (heavy or not). At the level DEBUG it reports each hit, miss and skip. The level can be changed at runtime (Changing logging levels at runtime). The secretary and the receiving nodes also log the breakdown of the time of every bigger package of changes (the phases of the update, the time of the writes to the region, hits and misses of the cache); see Performance of the index update.