Over a million developers have joined DZone.

More data to your Mule with Riak!

DZone's Guide to

More data to your Mule with Riak!

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

Originally authored by David Dossot

In the new enterprise, the “one big database” paradigm is being progressively eroded as it becomes more apparent that the ACID qualities of traditional SQL databases are not always needed, and can actually get in the way of massive scalability. In order to respond to the imperative of cloud deployments, new data stores have emerged.

One of them is Riak, a highly-available, fault-tolerant and scalable distributed key-value store built by Basho Technologies, Inc. on the design principles laid out in the famous Amazon Dynamo paper. One of the key features of Riak is that it “embraces failure” in the sense that, instead of sweeping under the carpet the problematic scenarios (like net-splits), it provides developers with the tools needed to deal with these issues as part of the normal operation mode instead of a leaving them to a (seldom addressed) failure mode. As such, data in Riak is said to be “eventually consistent” because it is only when all the pending replications have been done and the related conflicts have been resolved that data is consistent.

The new Riak Connector for Mule allows you to interact with this data store from your Mule applications, opening the door to new integration scenarios. Let’s review an example of such a scenario.

For this example, we will use the proverbial shopping cart scenario. It is proverbial because it’s the scenario for which Amazon Dynamo was originally built. Therefore it fits Riak perfectly because:

  • It doesn’t have the need for strong data consistency: a customer has the chance to review her cart before validating the order. Discrepancies could surely be annoying but, until the order is placed, data for the shopping cart can still be in a state of flux.

  • It is fairly easy to merge different versions of the shopping cart. Indeed, as data gets replicated across a Riak cluster, conflicts can occur where several versions of the same data exist. In the example of a shopping cart, we can resolve such conflicts by doing a union of the different versions, using the highest quantity for items in common.

In our example, Mule will be used to expose an internal cart management API. It will take care of automatically resolving conflicts using the defined policy so end-users of the API will not have to deal with that. It will also take care of serializing and deserializing cart objects to and from JSON. This is illustrated in the following diagram:

In this Mule application, a private flow is dedicated to retrieving shopping carts and deserializing them to their object representations (as illustrated in UML in the above diagram). This flow configuration is shown hereafter:

<flow name="retrieveShoppingCart" processingStrategy="synchronous">
    <riak:fetch bucketName="shoppingCarts" key="#[cartId]"
        resolver-ref="shoppingCartResolver" />

    <transformer ref="jsonToShoppingCart" />

As you see, there’s not much there. The bulk of the work is done by the “shoppingCartResolver”, which takes care of implementing the conflict resolution policy we’ve talked about earlier. To reiterate, this policy states that conflicting shopping carts are merged by unioning their items and picking the item with the highest quantity if there are two of the same. We use MEL to write this policy, as shown in the following code sample:
<spring:bean name="shoppingCartResolver"
    init-method="initialise" p:deserializer-ref="jsonToShoppingCart"
    <spring:property name="expression">
            resolvedCartItems = [:];

            foreach (cart : siblings) {
                foreach (item : cart.items) {
                    existingItem = resolvedCartItems[item.sku];
                    if (existingItem == null || existingItem.quantity < item.quantity) {
                        resolvedCartItems.put(item.sku, item);

          resolved = siblings[0];
          resolved.items = resolvedCartItems.values();

Because we have configured serializers on the resolver, the conflicting siblings we have to resolve are actually ShoppingCart instances. Without such serializers, our resolvers would have to deal with raw IRiakObject instances, which could be needed if the conflict resolution strategy requires dealing with low level Riak objects, like vector clocks.

You may be thinking that we’re painting a rosy picture here and that there are in fact tons of extra required configuration. But that is not the case! The following configuration snippet shows all the supporting configuration that is necessary to support the above configuration:

    <riak:http-client-configuration url="http://localhost:8098/riak" />

<json:json-to-object-transformer name="jsonToShoppingCart"
    returnClass="com.acme.ShoppingCart" />

<json:object-to-json-transformer name="objectToJson" />

As you can see, this is basic connectivity and transformer configuration. It is good to note that the connector supports both Riak’s HTTP and Protobuf transports and also clustered client configuration.

If you’ve been reluctant to bring Riak in your IT landscape for fear of integration issues, now is the time to give the Riak Connector for Mule a try! Enjoy and, as always, your feedback is very appreciated.

With SnapLogic’s integration platform you can save millions of dollars, increase integrator productivity by 5X, and reduce integration time to value by 90%. Sign up for our risk-free 30-day trial!


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}