{{announcement.body}}
{{announcement.title}}

Async Rest Client to DynamoDB Using Micronaut, Maven, and GraalVM

DZone 's Guide to

Async Rest Client to DynamoDB Using Micronaut, Maven, and GraalVM

See how to implement a simple REST DynamoDB client and build a native image with GraalVM and comparison in resource usage between clients.

· Java Zone ·
Free Resource

Overview

It will be a simple how-to article where I will be showing how to implement simple Rest DynamoDB client using Micronaut Framework and Maven, build a native image with GraalVM and simple comparison in resource usage between clients on Spring Boot and on Micronaut with GraalVM.

For those who are not familiar with Micronaut - it is a framework for building microservices and serverless applications. One of the key differences between Spring Boot and Micronaut is that Micronaut doesn’t use reflection to do IoC, so application startup time and memory consumption are not bound to the size of project codebase.

So our task is to handle HTTP requests for retrieving or storing some Event(id: string, body: string). Events will be stored in DynamoDB.

It might be easier to simply look at the code on Github and follow it there.

Maven

Let’s start with Maven runtime dependencies for Micronaut and DynamoDB SDK. As Micronaut doesn’t use reflection/annotation processing during startup but does it during build — we need to add annotation processors to maven-compiler-plugin. 

XML


DynamoDB

Configuration

A simple config where we set up connection to DynamoDB. For test purpose we need to specify dynamoEndpoint. In case of real application we need to specify region instead of endpoint. 

Java


Async DynamoDB Service 

Service for saving/retrieving event to/from DynamoDB. All async requests to aws are wrapped into RxJava constructions for easy handling of futures.

Java


Controller

Here we gonna expose our REST Api with GET method for retrieving event from DynamoDB and POST for storing event. 

Java


Integration Test

Maven dependencies 

For running integration test with DynamoDB we need DynamoDBLocal dependency, which is not really the DynamoDB, but SQLite with implemented DynamoDB interfaces on top of it. 

XML

DynamoDB server 

Now we need to start DynamoDB before tests, we can do it with the Jupiter Extension. 

Java


Running test

Now we can create an integration test and check if our REST Api methods do what we think. 

Java


Native Image

Using GraalVM we can build ahead-of-time compiled native image which is very useful for small applications. Native image includes an application classes, classes from its dependencies, classes from JDK. It does not run on the JVM. So in the end you will get standalone executable image which you can run without any JVM.

Because image already compiled, linked and partly initialized it will start faster, and you will get lower memory footprint. But keep in mind that there is a price for that - absence of JIT compiler, much simpler GC(SerialGC), platform dependent, hard to use frameworks which heavily relies on reflection(Spring Framework).

You can build image in several ways:

Building Image With Docker Multistage 

One disadvantage is that you need to know application’s classpath or download all dependencies in folder and point to it during building image.

Dockerfile


Building image With Maven

A bit harder than with a docker. You need to install GrallVM JDK, install native-image tool, set GraalVM as JDK for your project. After that you can add a plugin to maven and plugin will do the job. 

XML


When native image is ready we can build Docker image with it.

Dockerfile


Simple Comparision Between Dockerized Native Image and Dockerized Spring Boot App

As an example I’m gonna take spring boot application from this post which is basically doing the same stuff but with a help of Spring. 

Image Sizes 

First lets look at image sizes by running docker images 

Plain Text


Obviously, docker with native image uses less space, cause the native image removed everything that won’t be in use, including JVM.

App startup 

I run each image and just look at logs to get info when application completed startup. Micronaut-Native-Image started in 54 ms. Pretty impressive :) 

Plain Text


Spring Boot application took much longer to start:

Plain Text


Memory Footprint

To print Memory and CPU consumption run docker stats. But as I don’t do any requests to images - CPU numbers are irrelevant. 

Plain Text


Hurray, you made till the end!

Happy coding :)

Topics:
aws ,docker ,dynamodb ,graalvm ,howto ,java ,micronaut ,microservice ,tutorial

Published at DZone with permission of Yegor Bondarenko . See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}