In any distributed system a constant challenge is coping with changes in state. This is because a state change made in one part of the system does not instantly affect state in another part of the system.

Coping with Eventual Consistency

Steffan Westcott -  03 Nov, 2014

In any distributed system a constant challenge is coping with changes in state. This is because a state change made in one part of the system does not instantly affect state in another part of the system. In general terms, at any instant a view of mutable state is not guaranteed to be consistent with all prior updates. However, it is a reasonable assumption that the view will eventually become consistent.Cheddar is a Java framework for writing distributed enterprise applications on Amazon Web Services (AWS), following Domain-Driven Design (DDD)principles. It includes integration with native services such as DynamoDB, a distributed data persistence store. Cheddar simplifies implementation of API methods that would otherwise fail due to eventual consistency effects.

If at first you don’t succeed, try, try again

Cheddar tackles eventual consistency issues by retrying failed API methods. Using it is as simple as adding an annotation to the method implementation:

@Retryable
public String addUser(String firstName) {
// Generate random userId
// Write new user to database
return userId;
}

Cheddar supports AWS DynamoDB for persistence. DynamoDB is a distributed system and exhibits eventual consistency effects. For example, the content of a PutItem request is not necessarily observable by a GetItem request issued shortly afterwards.

Suppose a client of our application adds a user then immediately renames her:

String userId = userResource.addUser(“Sally”);
userResource.editUserDetails(userId, “Mary”);

 

editUserDetails() may fail at the first attempt as although Sally’s userId is known, it may not be possible to read Sally’s user details from the database immediately, instead throwing a NonExistentItemException. With the @Retryable annotation, Cheddar will retry the method if an exception is thrown. As we know Sally’s user details will eventually appear in the database, the method will succeed in a future attempt.

Using @Retryable in Cheddar

@Retryable is an annotation provided by Cheddar and is intended for use by API methods in Cheddar applications. These API methods are command service and query service methods exposed in the application layer of a Cheddar application. @Retryable has parameters to control the maximum number of attempts, the time delay between attempts, and other features.

Using @Retryable simplifies application development with Cheddar when dealing with eventual consistency. To retry methods that fail due to eventual consistency effects, simply annotate the method implementation. Take a look at Cheddar for details on this and other features for implementing DDD applications for AWS.

Subscribe to this blog

Use of this website and/or subscribing to this blog constitutes acceptance of the travel.cloud Privacy Policy.

Comments