Recent Bloggers

David H Nebinger

67 Mensagens
20 de Fevereiro de 2018

Jamie Sammons

Staff
11 Mensagens
19 de Fevereiro de 2018

Iacopo Colonnelli

2 Mensagens
17 de Fevereiro de 2018

Christian Klein

1 Mensagens
15 de Fevereiro de 2018

Jose M. Navarro

Staff
8 Mensagens
14 de Fevereiro de 2018

Sergio González

Staff
5 Mensagens
13 de Fevereiro de 2018

Minhchau Dang

Staff
15 Mensagens
31 de Janeiro de 2018

Koen Olaerts

3 Mensagens
30 de Janeiro de 2018

Fabian Larroca

1 Mensagens
25 de Janeiro de 2018

Vitaliy Koshelenko

3 Mensagens
12 de Janeiro de 2018
« Voltar

Liferay's Architecture: Caching (Part 1)

Company Blogs 21 de Janeiro de 2013 Por Jorge Ferrer Staff

Here I am again with another in the series about Liferay's architecture. If you haven't read them yet, the four previous entries covered: Overview, Services Layer, Web Services and Service Builder.

This time I'm going to cover a very important concept: caching. In today's web, it's impossible for a web application to provide even good enough performance in the web unless it has a well designed caching system. So what I'm going to cover here is not only useful to understand Liferay's architecture better but might also be beneficial for anyone writting Java web applications, specially if they are large.

Liferay is known to provide very good performance (check the Performance Whitepaper in the whitepapers section of this website for details) and its caching system is a key factor in achieving that performance. This caching system spans through all three layers. The following diagram shows the main caching components in each layer:

Let's cover each of them one by one, starting with the lower layer.

At the persistance layer Liferay relies on Hibernate to do most of its database access. Hibernate has two cache layers called Level 1 (L1) and Level 2 (L2). Level 1 is used to cache objects retrieved from the database within the current datababase session. In the case of Liferay a session is tied to an invocation to a service layer. So when the frontend layer (or a web service) invokes a service a db session is opened and reused until the service method returns. All operations performed until that point will share the L1 cache so the same object won't be retrieved twice from the database.

Hibernate's cache Level 2 is able to span across database sessions and stores database objects (Entity Cache) and results of queries (Query Cache). For example if any logic retrieves from the database all users that belong to a certain organization, the result will be stored in the cache. Note that what is really stored is the list of "references" to the actual objects which are stored in the Entity Cache. This is done to ensure that there aren't several copies of the same object in the cache.

Besides using Hibernate, Liferay's code also performs some complex database queries directly (although reusing the database conneciton). For these queries Liferay has its own Entity and Query cache. In fact thanks to the work of Shuyang these two caches are extremely efficient and as a result, for Liferay 6.2, we have decided to disable Hibernate's level 2 cache by default leaving Liferay's Query cache as responsible for that task to improve performance.

One final important aspect is that all of these caches use an underlying cache provider to manage the objects in memory. By default Liferay uses ehcache to do that, but it is also possible to change the caching provider through portal.properties.  The configuration is done within hibernate-clustered.xml which defines  and configures several cache areas.

Finetunning these configuration files is a very important task for any high-profile site. But don't try to do it based on guesses. You should always do it while running a performance test suite that can help you find bottlenecks and verify changes in the configuration.

Since I want to try and keep these blog entries shorter and more frequent, I'm dividing it in two parts. In the next one I'll cover the caching mechanisms of the services and the frontend layer.

Comentar
Respostas do tópico Autor Data
Great !! :-) !!! Please write more on Caching,... MANOVINAYAK AYYAPPAN 21 de Janeiro de 2013 20:47
Hi Jorge! Great article. I hope the next entry... Florencia Gadea 7 de Fevereiro de 2013 10:56
Una sugerencia.Este blog podria ser multidioma,... rafa sojo 28 de Fevereiro de 2013 04:01
Muy buen articulo! Todo queda muy claro.... Gerardo Travesedo 2 de Abril de 2013 10:24
I am currently facing some EhCache Replication... MANOVINAYAK AYYAPPAN 7 de Abril de 2013 19:45
Hi Manovinaya, as explained by Jorge: "Level 1... Mario Lavarreda 31 de Julho de 2013 14:01
Hi jorge, It is a great series on liferay... Ramkumar Chandran 23 de Julho de 2013 06:39
Great !!. Is there a part 2 of this Blog. Shiva Gokaram 3 de Dezembro de 2013 07:19
Not yet, sorry. I got too caught up with the... Jorge Ferrer 3 de Dezembro de 2013 09:22
Hi Jorge, waiting your next part about... Christophe Cariou 5 de Dezembro de 2013 00:52
Hi Jorge, Got a chance to write next series of... Vipin Bardia 2 de Abril de 2015 02:41
Hey Vipin, Sorry I got side track and never... Jorge Ferrer 7 de Abril de 2015 08:20
Sure :) Will wait for the article. Vipin Bardia 7 de Abril de 2015 10:53
Hi Jorge, you say: "One final important aspect... Marcello Torriani 15 de Julho de 2015 08:32
It is potentially possible, although you will... Jorge Ferrer 16 de Julho de 2015 00:55

Great !! :-) !!!

Please write more on Caching, as it is quite difficult to understand the bigger picture to start off. I have been struggling with EhCache for past 2 weeks.

Thanks
Postado em 21/01/13 20:47.
Hi Jorge!

Great article. I hope the next entry explains a lot more. What performance test suite do you use or recommend?
Postado em 07/02/13 10:56.
Una sugerencia.Este blog podria ser multidioma, o por lo menos tener una versión en castellano
Postado em 28/02/13 04:01.
Muy buen articulo! Todo queda muy claro. Gracias de parte de los que aun somos unos novatos!
Postado em 02/04/13 10:24.
I am currently facing some EhCache Replication issue with My 6.0.6 implementation.

I would like to know which of the following Cache would be used, for a custom entity object.
L1 or L2.

Say I created a Custom Entity Object and retrieve it using the CustomEntityLocalService from my front end, which Cache will be used L1 or L2.
Postado em 07/04/13 19:45.
Hi jorge,
It is a great series on liferay architecture
Could share some knowledge on Workflow Framework part of the liferay?
Postado em 23/07/13 06:39.
Hi Manovinaya, as explained by Jorge: "Level 1 is used to cache objects retrieved from the database within the current datababase session. In the case of Liferay a session is tied to an invocation to a service layer. So when the frontend layer (or a web service) invokes a service a db session is opened and reused until the service method returns. All operations performed until that point will share the L1 cache so the same object won't be retrieved twice from the database."

Ehcache is used to manage the 2nd level cache. Second level cache allows to mantain a object reference across multiple calls from your front end.
Postado em 31/07/13 14:01 em resposta a MANOVINAYAK AYYAPPAN.
Great !!.

Is there a part 2 of this Blog.
Postado em 03/12/13 07:19.
Not yet, sorry. I got too caught up with the 6.2 release.

But I plan to continue with this blog series pretty soon.
Postado em 03/12/13 09:22 em resposta a Shiva Gokaram.
Hi Jorge,

waiting your next part about caching.

In the meantime, can you give us some links where we can find explanations about the front end (mainly) an service layers caching mechanisms ?

For a proposal, I have to argue on the caching management in Liferay...

Thanks
Postado em 05/12/13 00:52.
Hi Jorge,

Got a chance to write next series of this article?
Postado em 02/04/15 02:41.
Hey Vipin,

Sorry I got side track and never found the time to continue.

I do plan to blog more about Liferay's architecture later this year but with a focus on the new modular architecture of Liferay 7. I hope you find that info useful as well.
Postado em 07/04/15 08:20 em resposta a Vipin Bardia.
Sure emoticon
Will wait for the article.
Postado em 07/04/15 10:53 em resposta a Jorge Ferrer.
Hi Jorge,
you say: "One final important aspect is that all of these caches use an underlying cache provider to manage the objects in memory. By default Liferay uses ehcache to do that, but it is also possible to change the caching provider through portal.properties. The configuration is done within hibernate-clustered.xml which defines and configures several cache areas."
But also that. "Besides using Hibernate, Liferay's code also performs some complex database queries directly (although reusing the database conneciton). For these queries Liferay has its own Entity and Query cache."
My question is:
is it possible to switch from ehcache implementation to another one both for hibernate and for liferay level cache?
Postado em 15/07/15 08:32.
It is potentially possible, although you will probably have to do a bit of research since it's not a common operation and thus it's not documented.

Out of curiosity, what implementation are you looking to use instead of ehcache?
Postado em 16/07/15 00:55 em resposta a Marcello Torriani.