aspose file tools*
The moose likes Web Services and the fly likes Stateless web service and caching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Stateless web service and caching" Watch "Stateless web service and caching" New topic
Author

Stateless web service and caching

John Landon
Ranch Hand

Joined: Sep 25, 2008
Posts: 227
Hi,
I built a stateless web service. Each time a client hits it, the service hits the database, retrieves thousand records but returns only 1 to the client. Then the next time the client asks for a record the same thing happens.
So the first time the service hits the db I want to cache the results and then just retrieve records from the cache when needed (after checking if this record is still is valid in the db).
The problem is that the service is stateless. So if I do something like hashmap it'll reinitialize each time.
Any ideas?

Thanks.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Any ideas?

Change the logic in the service so that it will only return 1 record to client application. Remove the retrieval of "thousand records."
John Landon
Ranch Hand

Joined: Sep 25, 2008
Posts: 227
that's obvious but I do not have control over the retrieving code.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Originally posted by John Landon:
The problem is that the service is stateless. So if I do something like hashmap it'll reinitialize each time.
Any ideas?


Service Statelessness Principle: "Services minimize resource consumption by deferring the management of state information when necessary."

You are already observing this principle because the query result is managed in the database - not inside the service. The nice thing about this arrangement is that any Tomcat instance that the client request comes in on has access to this information. Now you already ruled out narrowing the query. Now with a corporate grade DB like Oracle you could get your DBAs to cache the result set so that the query wouldn't have to be reprocessed - you are still down the transmission cost (and from your reaction I don't think that your RDBMS is that beefy either).

So if you can't defer state management to the database were can you defer it to? Metro includes the com.sun.xml.ws.developer.StatefulWebServiceManager for this reason - but your aren't using Glassfish/Metro - you are using Tomcat. Is there anything of this nature in Tomcat? Have a look at Tomcat: Sharing session data between contexts HOW-TO: Using a "shared" Context to store session data. The ServletContext is available from the javax.xml.ws.handler.MessageContext

Just know what you are getting into, how you are compromising the "minimizing of resource consumption" (e.g. when exactly do you delete that HashMap? ***), how you are compromising scalability. And you still need to reliably generate a "correlation identifier" to correlate between two related client requests so that you can retrieve that clients "state".

*** Store a single ClientStateManager with a constant sessionid. The StateManager manages "ClientState"s (a (property) java.util.Map implementation?) with an expiry time. Each time a ClientState is added (retrieved) check and dump all expired "ClientState"s first (last).
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
I built a stateless web service. Each time a client hits it, the service hits the database, retrieves thousand records but returns only 1 to the client.


that's obvious but I do not have control over the retrieving code.


As you state, the service retrieves "thousand records." This sounds like logic of the service. Are you stating that you do not have control over the web service you built?

Please describe what you mean by "retrieving code."
John Landon
Ranch Hand

Joined: Sep 25, 2008
Posts: 227
I do not have control over the hql that accesses the database.
I just call a method that gives me records. I cannot modify the hql it uses.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Sounds pretty radical dude!

So, the query is always going to fetch "thousand records." The logic in the service (that you control) reads the "thousand records" and then returns one to the client application.

Sounds like you could build a cache and place it in front of the web service. Recode the service logic to always get data from the cache.
Service logic should never execute the query directly.

Design the cache to maintain healthy data (at all times).

Sounds like fun. Good luck!
John Landon
Ranch Hand

Joined: Sep 25, 2008
Posts: 227
Originally posted by James Clark:
Sounds pretty radical dude!

So, the query is always going to fetch "thousand records." The logic in the service (that you control) reads the "thousand records" and then returns one to the client application.

Sounds like you could build a cache and place it in front of the web service. Recode the service logic to always get data from the cache.
Service logic should never execute the query directly.

Design the cache to maintain healthy data (at all times).

Sounds like fun. Good luck!

That's my original question!
HOW do I do this cache that is always gonna be available for all instances of the webservice?
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Originally posted by John Landon:
HOW do I do this cache that is always gonna be available for all instances of the webservice?


You did notice my above post about using the ServletContext? ServletContext is shared over the entire web application (should have been called ApplicationContext) - so all instances of the web service should have access to the same data in the same server instance.


Originally posted by John Landon:
I cannot modify the hql it uses.


The freedom of Hibernate turned to tyranny?

Or is it that you choose to not take control of the query process?

Chapter 13.4. DML-style operations
Chapter 15. Criteria Queries
Chapter 16. Native SQL
Chapter 17. Filtering Data

In any case you would be well advised to hide the access of this data behind a Strategy interface. That way you can first implement a FullDataAccess "Strategy" which does everything the slow way but you know is correct. You can then try your hand at a NarrowDataAccess "Strategy" that actually uses some of the more advanced Hibernate features or even a CachedDataAccess "Strategy". That way you can always easily switch back to the known FullDataAccess "Strategy" when you optimized version doesn't behave as expected.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
HOW do I do this cache that is always gonna be available for all instances of the webservice?


How is a single database always available for an application? The database and the code that is used to manage it are separate from the application that stores and retrieves data.

The cache or data storage will be a resource that the web service connects to get data. It will NOT be something that is coded in the business logic of the web service. It will NOT be a class variable in the implementation class for the web service. It should definately not be something stored in the class of a web container, unless that is all you have to work with. As mentioned, there are various "context" objects that can hold a reference to the cache, if this is the case.

Below is a example of what a cache class might look like.



This class will hold the "thousand records". This class will become a "managed resource" in the application server and will be accessed via JNDI. However you deploy the web service, you will need to create and initialize this class. This class will always be alive in the JVM, for as long as the service is available.

The logic in the service, instead of connecting to a DBMS, will connect to this cache. The "thousand records" will be in the cache. The cache will have to contain logic to periodically refresh itself, maybe every 10 minutes or so. This is the price you must pay for not being able to change the crazy query of "thousand records."
[ December 11, 2008: Message edited by: James Clark ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stateless web service and caching