Urbanleg
Urbanleg

Reputation: 6542

Cache in a distributed web application - complex queries use case

We are developing a distributed web application (3 tomcats with a load balancer).

Currently we are looking for a cache solution. This solution should be cluster safe ofcourse. We are using spring, jpa (mysql)

We thought about the following solution :

Create a cache server that runs a simple cache and all DB operations from each tomcat will be delegated to it. (dao layer in web app will communicate with that server instead of accessing DB itself). This is appealing since the cache on the cache server configuration can be minimal.

What we are wondering about right now is:

If a complex query is passed to the cacheServer (i.e. select with multiple joins and where clauses) how exactly the standard cache form (map) can handle this? does it mean we have to single handedly implement a lookup for each complex query and adjust it to map search instead of DB?

P.S - there is a possibility that this architecture is flawed in its base and therefore a weird question like this was raised, if that's the case please suggest an alternative.

Best,

Upvotes: 2

Views: 267

Answers (2)

Viorel Vesa
Viorel Vesa

Reputation: 310

If I understand correctly, you are trying to implement a method cache, using as a key the arguments of your DAO methods and as value, the resulted object/list.

This should work, but your concern about complex queries is valid, you will end up with a lot of entries in your cache. For a complex query you would hit the cache only if the same query is executed exactly with the same arguments as the one in the cache. You will have to figure out if it is useful to cache those complex queries, if there is a chance they will be hit, it really depends on the application business logic.

Another option would be to implement a cache with multiple levels: second level cache and query cache, using ehcache and big memory. You might find this useful: http://ehcache.org/documentation/integrations/hibernate

Upvotes: 1

Lesto
Lesto

Reputation: 2299

mySql already come with a query cache, see http://dev.mysql.com/doc/refman/5.1/en/query-cache-operation.html

Upvotes: 1

Related Questions