Debasish specializes in leading delivery of enterprise scale solutions for various clients ranging from small ones to Fortune 500 companies. He is the technology evangelist of Anshin Software (http://www.anshinsoft.com) and takes pride in institutionalizing best practices in software design and programming. He loves to program in Java, Ruby, Erlang and Scala and has been trying desperately to get out of the unmanaged world of C++. Debasish is a DZone MVB and is not an employee of DZone and has posted 55 posts at DZone. You can read more from them at their website. View Full User Profile

Asynchronous Write Behinds and the Repository Pattern

02.03.2009
| 7997 views |
  • submit to reddit

The following is a typical implementation of service methods of the domain model of an application. The Repository is injected and is used to persist the domain model or lookup objects from the underlying store. The entire storage and the mechanics of the underlying retrieval is abstracted within the DAO / Repository layer.

public class RestaurantServiceImpl implements RestaurantService
{
@Autowired
public RestaurantServiceImpl(..)
{
//..
}
// injected
private final RestaurantRepository restaurantRepo;
public void storeRestaurants(List<Restaurant> restaurants)
{
restaurantRepo.store(restaurants);
}
}



In a typical layered architecture, the database often proves to be the hardest layer to scale. And in the above implementation, restaurantRepo.store() is a synchronous method that keeps you in abeyance till the data gets persisted across all the layers of your architecture down to the bits and pieces of the underlying relational store. Of course it can be any other store as well - after all, the repository is an abstraction, so it doesn't matter to the application whether you use a relational database, a native file system or a document database underneath. But you get the idea, synchronous communication with the database / hard disk often turns out to be the bottleneck here.

Terracotta provides a nice option of virtualizing your interaction with the database. Async tim (Terracotta Integration Module) provides asynchronous write behind to the database, while the application works on in-memory data structures. Terracotta offers network attached memory with transparent JVM clustering that allows data structures to be *declaratively* clustered. The value proposition here is that, the user can work on the object model, using POJOs, delegating the concerns of persistence to an asynchronous Terracotta process.

Here is an example of the above service extended to handle asynchronous write behinds ..

public class AsyncRestaurantServiceImpl extends RestaurantServiceImpl 
{
// need to be clustered
@Root
private final AsyncCoordinator<Restaurant> asyncCommitter =
new AsyncCoordinator<Restaurant>(new RestaurantAsyncConfig(),
new NeverStealPolicy<ExamResult>());
// dependency injected
private final RestaurantCommitHandler handler;
@Autowired
public AsyncRestaurantServiceImpl(..)
{
super();
asyncCommitter.start(handler, ..);
}
@Override
public void storeRestaurants(List<Restaurant> restaurants)
{
asyncCommitter.add(restaurants);
}
//.. other methods
}


The AsyncCoordinator<> is the agent that handles the persistence asynchronously in the background. The class RestaurantCommitHandler contains the actual code that writes the collection of Restaurants to the database. RestaurantCommitHandler implements ItemProcessor<> - instances of ItemProcessor gets bucketed and throttled asynchronously for database commits, while the application continues by adding the objects to be persisted to a POJO.

@Service
public class RestaurantCommitHandler implements ItemProcessor<Restaurant>
{
//..
}

Now, we can take this one step further. The Repository is supposed to abstract the handling of the storage and retrieval - why not abstract the asynchronous persistence within the repository itself and keep the service implementation clean. Then it becomes simply injecting the proper repository to enable asynchrony at the service layer ..

interface RestaurantRepository {
void store(List<Restaurant> restaurants);
}
class RestaurantRepositoryImpl implements RestaurantRepository {  public void store(..) 
{
//.. standard DAO based implementation
}
}
class AsyncRestaurantRepositoryImpl implements RestaurantRepository 
{
@Root
private final AsyncCoordinator<ExamResult> asyncCommitter =
new AsyncCoordinator<Restaurant>(new RestaurantAsyncConfig(),
new NeverStealPolicy<Restaurant>());
// dependency injected
private final RestaurantCommitHandler handler;
public AsyncRestaurantRepositoryImpl()
{
super();
asyncCommitter.start(handler, ..);
}
public void store(..)
{
asyncCommitter.add(restaurants);
}
//.. other methods
}

I have not yet used the above in any production application. But the idea of decoupling the main processing from the underlying database decreases the write latency of domain objects. And couple this idea with Terracotta's original value proposition of cluster-wide in-process distributed coherent caching, I think it can prove to be a really wicked cool platform for scaling out your application. The system of record (SOR) is now closer to the application, and the database can act as a snapshot for audit trails and reporting purposes. Of course this asynchronous write behind is not suitable for a plug-in into an existing architectural framework where you have lots of loosely coupled systems interconnected through databases. But I guess there can be many use cases for which this can be a viable solution.

However, looking at the current state of Terracotta async write behind framework, one area that concerns me is the lack of an out-of-the-box support for cases when the database may be down for an extended period. The framework leaves it to the client to implement any such failover support. The ItemProcessor is a non-clustered local instance - hence the user can very well catch the ProcessingException and act upon it according to business needs. Still it will be nice to have some support from the framework, where by the application can continue to run in-memory and later can sync up when the database comes up.

Would love to hear some real life stories from anyone with experience to share on usage of Terracotta Async module ..

From http://debasishg.blogspot.com/

Published at DZone with permission of Debasish Ghosh, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Michał Jankowski replied on Tue, 2009/02/03 - 4:42am

Interesting, but I'm kinda worried that the state of the "in-memory" objects may become lost on a server failure. It's also curious how to integrate this concept with the transactions. 'In-memory saved' state for me does not equal committed to DB as it may become lost in processing. Delicate business data should not be handled this way.

Michał Jankowski replied on Tue, 2009/02/03 - 4:43am

What are your thoughts on the topic ?

Daniel Gradecak replied on Wed, 2009/02/04 - 6:06am

Have you considered something like Javaspaces? Gigaspaces is a vendor and they do have "in memory" transactions and async writes to a data base.

Sergio Bossa replied on Wed, 2009/02/04 - 10:11am in response to: Michał Jankowski

If you cluster your in-memory store (AKA cache) with Terracotta, which is a good thing for several reasons, it will be transparently stored on-disk by Terracotta itself and will survive disk failures and alike.

Talking about transactions integration, it depends whether you want a strict ACID system, or a BASE one. The former enforces data consistency and requires you to code distributed transactions, the latter favors availability and performance over consistency, which is asynchronously guaranteed, so doesn't require the burden of distributed transactions.

Cheers,

Sergio B.

Michał Jankowski replied on Thu, 2009/02/05 - 4:40am

Thanks for replies. I've read a bit about Javaspaces. I must say it's an interesting concept which in a way resembles JBoss Cache to me.

Guru Prasad replied on Thu, 2009/02/05 - 7:57pm

What happens if this asynchronous store command thrws an exception .. so .. say how does the UI catch it and show it to the user ? :)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.