Carving the Java EE Monolith Into Microservices
This article looks at how to break up enterprise Java applications into microservices and how to separate concerns and build smaller usable modules.
Join the DZone community and get the full member experience.Join For Free
following my introduction blog about why microservices should be event-driven , i’d like to take another few steps and blog about it while i prep for a bunch of talks i’ve got coming up (see you at jbcnconf and red hat summit in san francisco ). follow me on twitter @christianposta for updates on this project. in this article, we discuss the first parts of carving up a monolith.
the monolith i’m exploring in depth for these articles will be from the ticket monster tutorial which for a long time has been the canonical example of how to build an awesome application with java ee and red hat technologies . we are using ticket monster because it’s a well-written app that straddles the “non-trivial” and “too-complex for an example” line pretty well. it is perfect for illustrative purposes and we can point to it concretely and discuss the pros and cons of certain approaches with true example code. please take a closer look at the domain and current architecture in light of the further discussions .
looking at the current architecture above we can see things are nicely broken out already. we have the ui components, the business services, and the long-term persistence storage nicely separated and decoupled from each other yet packaged as a single deployable (a war file in this case). if we examine the source code, we see the code has a similar structure . if we were to deploy this, any changes to any of the components would dictate a build, test, and release of the entire deployable. one of the prerequisites to doing microservices is autonomy of components so they can be developed, tested, deployed in isolation without disrupting the rest of the system. so what if we just carve out the different layers here and deploy those independently? then we can achieve some of that autonomy?
we’ve spent a lot of time in the past arguing for this type of architecture and it seems to make sense. we want to be able to scale out the individual components based on their needs. scale out the web tier if we need to handle more web requests. scale out the business services tier if those services start to become the bottleneck. deal with and manage the database and the data-access layers independently from the rest of the application/services. “decoupling” the ui logic from the middle tiers and the data access is a good guiding principle, but don’t confuse it with requiring layers.
what really happens in practice is that all of these “layered” architectural components, for all of its separation of concerns etc., succumbs quite easily to the whims of the data and the database. we can add all the cpus we want, all the middle tiers and ui tiers we want, but no matter how fast our networks, compute, memory, etc. have become, the bottleneck for this type of system typically is the competing domain models and ultimately the database. there’s a stress here on “domain models” — the internet companies practicing microservices may not have complex, ambiguous, and contradicting domain models like an fsi or insurance or retailer may have. for example, twitter has a simple domain: publish and display tweets. but that becomes complicated at such large scale — enterprises are starting to have both problems at the same time — the domain model and its complexity is just as important as how to scale it (and often impedes the effort to scale). so now you just think “we’ll just use a nosql database like mongodb so we can scale out our backends” — now you’ve got even more problems.
what about our teams? another part of architecting a system like this is so we can have specialist teams independently work on these layers at different speeds, different locations, different tools, etc. they just need to share an interface between each other and they can work autonomously. this plays to conway's law a little bit:
organizations which design systems … are constrained to produce designs which are copies of the communication structures of these organizations
unfortunately, i feel like it’s the reverse that’s true. it’s not that by doing this architecture we’re creating this opportunity for this specialization in teams and efficiency. it’s because of our organizational structure that forces us down this system architecture. just like we have separate database teams, ui teams, security, operations, qa, build, and release, etc., etc. this is how our organizations have been organized for decades. however, if you look at the success of the companies practicing microservices, there’s something quite a bit different about their organizational structure .
let’s take a look at what happens. taking the ticket monster app as an example, the business asks that we alter the way the administration of the website is handled. they ask us to add some extra fields related to tracking how often concerts are added to and removed from the website because they want to add some predictive analysis about what may or may not be good events to add in the future based on time, location, weather, etc. this may involve the ui team if the business wants to display this predictive analysis to the administration users. it will certainly involve changing the business services layer of the app, and it will certainly impact changes to the database. we want to add some functionality to our app that forces ripple effects across all the layers and even more importantly, across all of the teams involved. now we have to have project managers coordinating and tracking meetings with all of the teams involved. we need to create tickets to get the ui and db teams to do anything not to mention qa, security, operations, etc to all be on board. all of this creates complex synchronization points between all of our teams and now we have to coordinate all of the changes, builds, and releases of our layers (and deploy everything together!). this is not the type of autonomy we desired. we cannot make changes independently of each other and, in fact, we’ve become quite brittle.
for our ticket monster app, let’s prefer to split the functionality into cohesive “verticals” not by technological or organizational
. each vertical will have its own “ui” (or ui component), “business services” and “database” that are specific to the function of administering the site. (for the first steps, however, we’ll leave the ui as a monolith and break down the pieces behind it. we’ll come back to breaking apart the ui, though that has its own challenges). ticket monster also allows users to review and book orders for concerts. let’s split that into its own vertical. it may also have loyalty, recommendations, search, advertisements, personalization, etc. we’ll split those into their own verticals each owning their own database, ui, and integration points (rest services, backends, etc). if we need to make changes to the loyalty functionality of the website, i don’t need to go and re-deploy the entire monolithic business-services tier or anything related to search for example. i can deploy the part of loyalty from the ui to the db that i need without forcing impacting changes to other services. ideally, a single team would own and operate each service as well.
this gives us better cohesion within the code as well as more autonomy between the services. once you start wrapping your head around what it means to split along business function verticals, we can explore for each vertical what its bounded context looks like; or whether or not it makes sense to apply cqrs within a bounded context. or what type of database it should use based on its read/write patterns (document? relational? graph) and whether you favor consistency or can tolerate a loss of data/inconsistent data. or what transactions, compensations, apologies, etc. might look like. and on and on... we can now make these decisions in terms of what's best for our individual services not the lowest common denominator for a layer or a monolith. that’s what we’ll continue to explore in the next post! stay tuned!
Published at DZone with permission of Christian Posta, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.