Choosing DropWizard to deliver content within your Grails projects

In our current project, we’re using DropWizard alongside Grails. It has become a fairly significant and useful tool for building simple content delivery layers.

In this post, I will try to share some of the rationale for choosing DropWizard to serve content and how it fits within a Groovy / Grails ecosystem.

Our Current Project Architecture

dropwizard

We’re working on a project that will replace significant portions of an existing website. The site will have a lot of editorial content that will be aggregated with some external services provided by a few other teams.

We have a CMS component that is written in Grails and AngularJS. This component is responsible for generating static html pages into a MongoDB database. We then use DropWizard to serve these static html pages, uploaded images and do image resizing.

We also have another DropWizard component that aggregates feeds provided by other services and serves them to the requesting browser. This layer is a pure REST beast and only talks JSON.

Going Outside of Grails

Our default architecture would have been to write the entire thing in Grails. However, we were hesitant about choosing Grails due to our past experience with the framework.

I’ve been working with Grails for about five years. While it is a tremendous framework with a lot of power, there are a few areas where it sort of falls apart, specially when delivering content. We wanted a light, simple, reliable layer to push content — and Grails simply isn’t it.

One of the main problems with Grails today is that it almost encourages you to build up a large monolithic application. This creates a lot of problems when you try to scale parts of the application. What you want to do is to break down your application into smaller, more manageable components that can be replaced or swapped out when better technologies come along. ( For a good primer on the topic, check out Chris Richardson’s talk on Decomposing Web Applications for Scalability ).

For our application, we really wanted to separate the layer that is responsible for editing and writing the data with the layer that is responsible for serving it. Grails is a perfect fit for a CMS — it has many GORM helpers for writing/validating content written to a database and has Security Plugins up the wazoo. However, Grails is very heavy and sometimes problematic for simply serving out content.

Grails, in its current form, encourages you to use server side gsp files using flaky tools like Resources or Platform core plugin. Spring also adds a lot of weight and memory requirements that is unnecessary for a light, simple REST API. Deployment is pretty heavyweight as you need to setup a tomcat instance.

Benefits of DropWizard

While searching for viable alternatives, we stumbled upon DropWizard – a toolkit created by Coda Hale based on Java, Jersey and Jackson that has as its mission the ability to serve a ton of requests. As we investigated and spiked DropWizard, I fell in love with a few of it’s features:

Java Based – means you can easily add it to your ecosystem. It also means that you can use all the Groovy tools you know and love. The core is just a Jar file.

REST / JSON – Dropwizard core comes setup with Jackson and Jersey. This encourages an approach where you end up writing REST endpoints that serve up JSON. In our current projects, our Grails apps serve out both JSON or HTML content. This then causes problems when trying to test components in isolation. By enforcing JSON only, you are able to more easily integrate with tools like Yeoman or AngularJS since you can then mock up the backend via the tools provided by the frameworks.

Simple Deployment – Dropwizard encourages you to build fat jars that are self contained. This is very useful for building small, light services that don’t need a container to be set up. It also means that adding more instances can be done fairly easily.

No Magic – The plugin mechanism in Grails is really nice. But often it introduces bugs and often does a lot of metaclass magic that adds a lot of complexity. DropWizard encourages a NoSpring approach where you have to hand wire your dependencies and add them to your services. This gives you a lot of control over how your components interact and makes it easier to test since there are no magic methods that appear out of nowhere.

Health Checks – DropWizard has a mechanism called Heath Checks. These are essentially little tests that are ran when your service is running and can be used to verify that it is working properly when in production. There is a grails plugin called Health Control that does the same thing. I think Health Checks highlight a significant difference between Grails – a general purpose framework – and DropWizard – a framework originally intended to be used to do lots of REST calls.

Configuration Validation – Another DropWizard goodie is the built in ability to quickly switch configurations and validate each field of a service configuration. This becomes pretty important as it means that all configurations are externalized and validated. Configuration values can also be overriden by system properties and on app launch. There is a grails plugin called validated-config that provides this mechanism.

Built-in Metrics – DropWizard comes bundled with the Metrics library. This allows you to annotate service calls and performance metrics will be automatically calculated. This is very useful for services and is a feature we have yet to explore further.  There is also a grails plugin.

Test Friendly – Since DropWizard is very lightweight, tests run incredibly fast. Also, the way in which different components hook into each other declaratively meant that you could easily use Spock to mock out various components. DropWizard is significantly easier to test than a full Grails app given the limited scope of its functionality. 

FYI , the name DropWizard is based on this comic strip.

Moving DropWizard to a Groovy world

The original DropWizard project is written in Java and Maven. Most of the examples and sample code in their site is in Java. However, the ideas are easily transferrable to Groovy.

We use the fatjar plugin for gradle for assembling the jar file and extended an original gradle file found on Github to support building and running our DropWizard app.

When we started the project, we migrated the DropWizard sample POM into Gradle and added support for Spock testing. I’ve outlined our changes to support Spock in this previous blog post. This was perhaps the biggest hurdle into moving the Java app into one that supported Groovy.

We did face a significant issue when trying to bring in discrete components of our end-to-end tests, as Gradle did not support running executing external processes and waiting for them to be ready. I wrote a small plugin to get around this problem.

Once these steps were done and we started writing our application, it was very productive and fast. The significant difference in the ability to test ( ~4-8 seconds vs. ~90 seconds in our Grails app ) meant that we were able to write out these services pretty quickly.

Overall, starting to write DropWizard apps with Groovy and Gradle is pretty simple and easy to learn.

Where does Grails fit in?

To reiterate, I’m a big fan of Grails. GORM is great for dealing with data and it is my preferred framework for the big complex parts.

There are things that DropWizard doesn’t do very well. Setting up security is not as straightforward as using Spring Security. There are a lot of extensions written by third parties, but the ecosystem is not as robust as Grails. In ways, Grails is much more powerful for all the other stuff around a site — DropWizard just does the content serving and REST service part better and more elegantly.

For us, there are two main weaknesses in Grails for our purpose — The UI Layer and serving content. We overcome weaknesses in UI tooling by using Yeoman and AngularJS. We make our web services more robust by using DropWizard.

What about Ratpack?

Fundamentally, I can also see an architecture where the Grails layer gets replaced by Ratpack. But DropWizard and Ratpack seem to serve slightly different goals. Where Ratpack fits as a fairly lean layer for dynamic content that is rendered server-side that is more general purpose, DropWizard finds it’s strength in very specific JSON / REST architecture. While you can probably do the same content serving layer in Ratpack, you do then lose service-friendly features like Metrics and Health Checks. 

Summary

In this post, I have outlined an architecture where we take full advantage of Grails as a CMS engine but rely on DropWizard as the means of serving and aggregating content.

There is a plugin written by Burt Beckwith that tries to integrate the entire dropwizard framework into Grails. However, to me, it feels like the wrong approach. Grails and Dropwizard are solving different problems and they both have different strengths. It feels better to keep them separated.

With DropWizard, you have a fast, easy to develop layer that is highly testable and deployment friendly. In Grails, you can do more complex things at the cost of a more cumbersome deployment and slightly more complexity. Both can co-exist in a given architecture and they complement each other rather well.

9 thoughts on “Choosing DropWizard to deliver content within your Grails projects

  1. Luke Daley (@ldaley)

    A couple of, hopefully, clarifying points about Ratpack.

    DropWizard will probably always be better at serving nothing but data feeds. It’s a specialist tool for that.

    I don’t think the statement “Ratpack fits as a fairly lean layer for dynamic content that is rendered server-side that is more general purpose” is quite right. Ratpack aims to be a composable toolkit for processing HTTP requests. At its core it just provides some basic abstractions and structure around async request processing. It could very easily, and naturally, layer more focussed abstractions for data feeds on top of what’s already there. And the point here is that this need not be done by the framework… because there isn’t much of a framework. The idea is that you can easily construct your own request processing framework on top. That’s the goal anyway.

    Reply
  2. dan roden

    Moving more into front end development activities I’ve been falling more and more in love with angular.js over the past couple of months and have been wondering about how to best to build the server side REST components that is fairly pain free. Coming from a Java/Groovy background with a strong affection for Grails, I have drawn towards Dropwizard and have been wondering whether it is feasible to extract the GORM component out of Grails and put this into a Dropwizard REST based implementation. This could, in my opinion combine the benefits of what Dropwizard offers with the strengths of GORM, especially now its abstracted out into an api and has many persistence options. I haven’t spent enough time thinking about this yet or seeing if anyone else is attempting this but I personally would love to have a angular.js + dropwizard + groovy + gorm combination.

    Reply
  3. groovybayo

    Tomas – thanks for the post. I am actually exploring this route in my recent project. Since dropwizard comes with its own embedded container and spins off via a fatjar, how where you able to combine it with grails so easily? Did you keep the grails and dropwizard applications completely separate? Also do you know how easy it is to host dropwizard apps with a cloud provider? I assume its a straight forward setup. Lastly, do you have a sample app(s) illustrating the described architecture on hub by chance? Or any showing using grails with dropwizard.

    Thanks in advance

    Reply
    1. Tomas Lin Post author

      On Cloud Foundry, Anynines or Appfog, you just spin it off with this command

      java $JAVA_OPTS -Ddw.http.port=$VCAP_APP_PORT -Ddw.http.adminPort=$VCAP_APP_PORT -jar myFatJarApp.jar server prod.yml

      I think it is similar in Heroku but the command is $PORT.

      There is a grails plugin for dropwizard, but the point for us was really to separate the two applications. We connect to dropwizard from the gsp pages served by grails and use javascript / ajax to access the application.

      No examples yet, but it is just integrating a REST service into Grails – of which there are a lot of examples and even the GORM-REST plugin

      Reply
  4. Ko-Chih Wu

    Can you elaborate on how your dropwizard apps accesses the DB? Do you go through the Grails app or access the DB directly?

    In our architecture, we have several REST apps and one heavy Grails app. Most of them write to MySQL directly, and it sucks that you can’t reuse GORM in REST apps, since they’re normal Jersey apps. We’re using Spring Data, which is convenient but still a lot of code duplication.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s