Top 4 Ways to Make Your Microservices Not Actually Microservices
Tags: microservices
Published: 2020-02-04 17:07:49

<p>Microservices have gained such a level of popularity these days that they're often touted as "the way" to build software. That's all well and good, except there are a lot of microservice anti-patterns flying around.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The vast majority of microservice systems I've seen aren't really <a href="https://www.scalyr.com/blog/microservices-logging-best-practices-2/">microservices </a>at all. They're separately deployable artifacts, sure. But they're built in a way that,&nbsp;for developers, causes more pain than solves problems.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So let's dive a little more into this. First, we'll talk about what a microservice was intended to be. Then we'll get into some of these anti-patterns.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":2273,"align":"center"} -->
<div class="wp-block-image">
<figure class="aligncenter"><img class="aligncenter  wp-image-1934" src="https://library.scalyr.com/2018/11/13003217/Disguise_face_in_Scalyr_colors.png" alt="" width="322" height="322" /></figure>
</div>
<!-- /wp:image -->

<!-- wp:more -->
<!--more-->
<!-- /wp:more -->

<!-- wp:heading -->
<h2>What Does "Microservice" Mean?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>As with many terms in the software industry, we have conflated "microservice" with plenty of things. For most people, it usually seems to mean "a deployment artifact." However, it's more than that. Members of ThoughtWorks developed the term in 2014. They were combating against the abuse of service-oriented architecture while still driving towards good software&nbsp;system design. As <a href="https://www.martinfowler.com/microservices/" target="_blank" rel="noopener noreferrer">described here</a>, microservices:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Are autonomous, relying minimally on other services through lightweight communication mechanisms.</li><li>Center around a business capability, in a similar vein to <a href="https://martinfowler.com/bliki/BoundedContext.html" target="_blank" rel="noopener noreferrer">bounded contexts</a>.</li><li>Are heavily reliant on automated infrastructure to get into production.</li><li>Require monitoring and good tooling to stitch&nbsp;together insights into what's happening&nbsp;across services.</li><li>Are designed for failure. A dependency going down should not bring down the entire service.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>If we break these traits, then what we call microservices are not actually microservices.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>How to Turn Your Microservice Into an Anti-Pattern</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Let's explore some common ways that teams take this original idea of microservices and twist it. I'll share some of the top microservice anti-patterns that I have seen in the wild.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>1. Ensure Your Services Are Around Technical Concerns</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Centering your&nbsp;services around technical concerns is a surprisingly common microservice anti-pattern. I believe this mostly comes from combining two principles incorrectly: the <a href="https://en.wikipedia.org/wiki/Single_responsibility_principle" target="_blank" rel="noopener noreferrer">single responsibility principle</a> and <a href="https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html" target="_blank" rel="noopener noreferrer">clean architecture</a>. Clean architecture says to use ports and adapters to isolate your business logic&nbsp;from things that need to use your business logic.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><img class=" wp-image-2811 alignright" src="https://library.scalyr.com/2018/10/01053246/Pull_quote%E2%80%94start_with_a_well-modularized_monolith_before_splitting_it_into_separate_services.png" alt="" width="243" height="192">This is all well and good, but it can easily mutate into something called layering, which is not all well and good. Layering was a key factor in the failure of <a href="https://www.scalyr.com/blog/microservices-vs-soa-whats-the-difference/">service-oriented architecture</a>. When you view everything in layers, your single responsibility sense kicks in and you say, "Well, I need to separate&nbsp;my persistence logic from my business rules logic."</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So when someone says "microservices," you decide you'll follow what you have and make all these layers into different, separately deployed services. They may end up looking like this: security service, data services, orchestration service, business service. This goes directly against the idea that microservices center around a&nbsp;<strong>business capability</strong>. Healthy microservices would look more like this: shipping service, inventory service, ordering service.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It's also common to slice out cross-cutting concerns, like auditing or email notifications, into a separate service. This will have the same pain points as layered services.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>When you follow this anti-pattern, you ensure that your system will be:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Very slow due to the latency in network calls across services.</li><li>Susceptible to network failure since if one layer fails to get its data across the network, all downstream layers fail.</li><li>Very hard for developers to add new features as they struggle to follow the code through all the layers.</li></ul>
<!-- /wp:list -->

<!-- wp:image {"id":2281,"align":"right"} -->
<div class="wp-block-image"><figure class="alignright"><img src="https://staging-design.scalyr.com/wp-content/uploads/2018/11/Pull_quote—start_with_a_well-modularized_monolith_before_splitting_it_into_separate_services.png" alt="" class="wp-image-2281"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>You can avoid this through diligent design and understanding your customer's needs. You need subject matter experts to help guide you through the different aspects of your business. Then, you can separate them out. It's often helpful to start with a well-modularized monolith before splitting it into separate services.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>2. Ensure Low Autonomy</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Let's say you've successfully avoided the first anti-pattern: your services have pristine names like inventory, purchasing, product, and customer support. However, you realize that your inventory service needs to talk to the product service in order to get some details on where in the warehouse a product belongs. And then you need to connect to the customer support service so that you can figure out which products customers really want so you can stock them appropriately.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Of course, whenever you run a request, you have to ping each individual service for information. Before you know it, you have a tangle of services all trying to talk to each other to do their jobs.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It can be even worse when two services try to talk back and forth like this:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":10411,"align":"center"} -->
<div class="wp-block-image">
<figure class="aligncenter"><img class="wp-image-10411" src="https://staging-design.scalyr.com/wp-content/uploads/2018/11/Service-Chatter-1.png" alt="" /><img class="aligncenter size-full wp-image-2812" src="https://library.scalyr.com/2018/10/01053308/Service-Chatter-1-300x285.png" alt="" width="300" height="285" /></figure>
</div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>If your services follow this anti-pattern, they're no longer microservices. That's because <a href="https://www.scalyr.com/blog/microservices-vs-soa-whats-the-difference">microservices</a> need to be (relatively) autonomous. They should not heavily depend on other services to do their work. By following this anti-pattern, you ensure that:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Development is painful since you need to go back and forth to add or change functionality if within the same team.</li><li>Each service team incurs a coordination cost to try to get both teams to align on when to make changes.</li><li>Failure of one affects the others.</li><li>Developers will get confused about who owns what data.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>You can avoid this by doing a couple things. First, ensure you that you diligently figure out the boundaries such that the correct service owns the correct data. In the diagrammed case, inventory should own stock and storage locations but should not care about price or invoice numbers. Perhaps inventory may need to know when a customer places a new order, so it can fulfill the order. Or purchasing might need to know when an item is not in stock. In these cases, you can use <a href="https://docs.microsoft.com/en-us/azure/architecture/guide/architecture-styles/event-driven" target="_blank" rel="noopener noreferrer">event-driven architecture</a> to ensure that when something happens to one service, the other services can follow up with their own actions.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>3. Have a Tightly-Coupled Orchestration Service</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In large organizations, it's popular to slice development teams horizontally, similar to layering. They then try to bring it all back together with an orchestration service. Orchestration acts as a bandage over their self-inflicted wounds.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>An orchestration service is not a microservice. Instead, it's a vicious anti-pattern that is hard to combat. These services don't revolve around a business capability. They're also not autonomous. These types of services are not designed for failure. You cannot easily deploy them independently, as they are often useless until their underlying services are deployed.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":2286,"align":"right"} -->
<div class="wp-block-image"><img class=" wp-image-1937 alignleft" src="https://library.scalyr.com/2018/11/13003219/Pull-quote%E2%80%94If-you-dont-design-for-failure-then-your-service-is-not-a-microservice.png" alt="" width="282" height="180" /></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The pain of orchestration services is similar to the above microservice anti-patterns. They:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Slow everything down by adding network latency.</li><li>Cannot respond faster than the slowest underlying service.</li><li>Slow development across all services, as teams have to coordinate with orchestration to get their features out.</li><li>Will often be blamed for any problems the underlying services have, and will not be equipped to understand these failures.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>If you find yourself in this position, there are some things you can do. You can build your orchestrator to be as schema-agnostic as possible. Try not to transform anyone's data unless absolutely necessary. If possible, push the composition of these services up to the customer-facing application.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>4. Ensure That Your Service Fails When Another&nbsp;Service Fails</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>You may have avoided the pitfalls above and have only a few dependencies on other services when you make requests. But what happens when those underlying services fail? Teams may have low coupling to others, but if those dependencies fail, your service could grind to a halt. This is especially true of orchestration services. Taking our purchasing/inventory example, what happens if someone tries to place an order with purchasing, but inventory says the item is out of stock? Or even worse, what if inventory is not responding? Do you give up?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you don't design for failure, then your service is not a microservice. If you don't design for failure, you might:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Get blamed by your consumers for failures that you do not own.</li><li>Lose customer trust with a higher-than-expected fail rate.</li><li>Spend an inordinate amount of time spent on production support.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>You can avoid this through various strategies. As stated above, trying to decouple the services through events works well. You can also add retries around your service calls, avoiding intermittent failures like network timeouts or dropped packets. If you're sending commands, you can add queuing and offline processing. Then you can put the command "on pause" and try again when things are working. If you're reading data, you can show an "empty value" on failure. You can also have a fallback cache of values to use if the service is currently non-responsive.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>In Conclusion: Learn More</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Microservices can be useful for handling large systems. However, you can see how easy it is to mess up and not have microservices at all, even though you think you do.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Follow these anti-patterns and you're guaranteed to have a hard time. Be careful and diligent when separating your services. Learn as much as you can about your business processes and follow those seams when you design systems. Understand what microservices are meant to do and design accordingly, and you'll save yourself lots of pain in the long run.</p>