Sunday, March 24, 2019

Refactoring a complex Java EE Monolithic App to Microservices

Hi there! In this blog post, I have addressed some of the major questions that will arise when transforming a Java EE monolithic application to a microservice architecture from my own experience.

I have been working at the final phase of a Java EE monolithic telecommunication application. According to the client requirements, after the final phase, we should break down the functionalities of the monolith application which is packaged on a single WAR archive to multiple micro services. ( multiple JARs and WARs ). Initially, I was assigned for a research task on microservices to identify the best practices, technologies and methods for breaking down the monolith application.

Below are the identified steps from the research in order to migrate the monolith application to microservices.

1. Follow Domain driven design architecture when creating micro-services or decomposing the existing monolith application. (Sub domains, boundary contexts, context maps)
2. Repackage the application
  • Repackage the app into multiple individual WAR files and apply container-per-service pattern and deploy each WAR file separately in to Wildfly or Docker container.
  • Build, deploy, and manage independently: After the WAR files are split, you can manage each WAR file independently through an automated DevOps pipeline.

3. Refactoring the application code.

Existing REST or JMS services ; You might have services that are already compatible with a microservices architecture, or that can be made compatible. Start by untangling each REST or simple JMS service from the rest of the WAR file, and then deploy each service as its own WAR file. At this level, duplication of supporting JAR files is fine—this is still mostly a question of packaging.

Existing EJB services : If you have services, they were probably built by following a functional approach, such as the Service Façade pattern. In this case, you can usually refactor function-based services design into an asset-based services design. If the functions in the Service Façade were written as create, retrieve, update, and delete operations on a single object, the mapping to a RESTful interface is simple: reimplement the EJB session bean interface or JAX-WS interface as a JAX-RS interface.

Internal Structure of a microservice
Calls to external services should be separate from the domain logic. Below figure shows a simplified version of a microservice architecture. This architecture separates the domain logic from any code that interacts with or understands external services. The architecture is similar to the Ports and Adapters (Hexagonal) architecture

Resources expose JAX-RS resources to external clients. This layer handles basic validation of requests and then passes the information into the domain logic layer.

Domain logic, as a general concept, takes many forms. In Boundary-Entity-Control patterns, domain logic represents the entity itself, with parameter validation, state change logic, and so on.

Repositories are optional, but can provide a useful abstraction between the core application domain logic and the data store when present. This configuration allows the backing data store to be changed or replaced without extensive changes to the domain logic.

Service connectors are similar to the Repository abstraction, encapsulating communications with other services. This layer functions as either a façade or an “anti-corruption” layer to protect domain logic from changes to external resource APIs, or to convert between API wire formats and internal domain model constructs.

http://alistair.cockburn.us/Hexagonal+architecture
http://martinfowler.com/articles/microservice-testing/#anatomy-modul


4. Refactoring the data.
  • Isolated islands of data
Begin by looking at the database tables that your code uses. If the tables are either independent of all other tables or come in a small, isolated island of a few tables that are joined by relationships, you can split the tables from the rest of your data design. Then you can consider the right option for your service. For instance, do you stay in SQL, but consider moving from a heavyweight enterprise database such as Oracle to a smaller, self-contained database like MySQL? Or do you consider a NoSQL database to replace your SQL database?

The answer to that question depends on the kinds of queries you run on your data. If most of the queries are simple queries on "primary" keys, a key-value database or a document database might serve you well. Alternatively, if you have complex joins that vary widely—for example, your queries are unpredictable—staying with SQL might be your best option.

  • Batch data updates 
If you have only a few relationships and you decide to move your data into a NoSQL database anyway, consider a batch update into your existing database. When you consider the relationships between tables, the relationships often don't take a time factor into consideration. They might not need to be always up to date—a data dump/load approach that runs every few hours might be fine for many cases.

  • Table denormalization
If you have more than a few relationships to other tables, you might be able to refactor, or in database administrator (DBA) terms, "denormalize," your tables. Even the discussion of this idea might alarm a DBA. However, your team as a whole should think about why data was normalized to begin with. Often, the reason for highly normalized schemas was to reduce duplication, which was to save space, because disk space was expensive. However, that's not true anymore. Instead, query time is now the factor you want to optimize. Denormalization is a straightforward way to achieve that.

https://www.ibm.com/cloud/garage/architectures/microservices/refactor-to-microservices


5.  Inter-service-communication - Use Saga Pattern (Event driven architecture)

Use Saga Pattern to maintain data consistency / usability across services if each service has its own database. 

This pattern has the following benefits
  • It enables an application to maintain data consistency across multiple services without using distributed transactions .
This solution has the following drawbacks:
  • The programming model is more complex. For example, a developer must design compensating transactions that explicitly undo changes made earlier in a saga. 
There are also the following issues to address:
  • In order to be reliable, a service must atomically update its database and publish an event. It cannot use the traditional mechanism of a distributed transaction that spans the database and the message broker. Instead, it must use one of the patterns listed below.

Microservices maturity model




References


https://microservices.io/patterns/microservice-chassis.html
https://www.nginx.com/blog/refactoring-a-monolith-into-microservices/
https://stackoverflow.com/questions/30286443/microservices-how-to-store-source-code-of-many-microservices
https://microservices.io/patterns/observability/health-check-api.html
http://projects.spring.io/spring-cloud/
https://microservices.io/patterns/observability/distributed-tracing.html
https://martinfowler.com/bliki/CircuitBreaker.html
https://dzone.com/refcardz/getting-started-with-microservices?chapter=5
http://callistaenterprise.se/blogg/teknik/2015/03/25/an-operations-model-for-microservices/
http://callistaenterprise.se/blogg/teknik/2017/05/12/building-microservices-part-6-configuration-server/
http://callistaenterprise.se/blogg/teknik/2017/09/13/building-microservices-part-8-logging-with-ELK/
http://callistaenterprise.se/blogg/teknik/2015/04/10/building-microservices-with-spring-cloud-and-netflix-oss-part-1/
https://www.infoq.com/articles/microservices-aggregates-events-cqrs-part-1-richardson