3 Easy Things to Do to Make Your Microservices More Resilient
For every problem they solve, microservices introduces a new one. The main issue is resiliency, particularly when coordinating across so many services. Let's take a look at 3 simple tips to help your systems scale safely.
Join the DZone community and get the full member experience.Join For Free
one of the advantages of building distributed systems as microservices is the ability of the system as a whole to withstand faults and unexpected failures of components, networks, compute resources, etc. these systems are resilient even in the face of faults. the idea behind this resiliency seems simple: if our monolith fails, everything for which it’s responsible fails along with it; so let’s break things into smaller chunks so we can withstand individual pieces of our app failing without affecting the entire system. this sounds great in theory, but does simply breaking things into smaller chunks give us this?
one way to test this is to use what netflix calls “chaos monkey” or similar “chaos” strategies: introduce perturbations in the system on purpose to prove the resilience (or fragility) or your system. systems show their true colors during failures, not happy-path processing, so purposefully introducing failures is a great way to find out what you’ve really built. actually, what you really want to do is build antifragile systems, but that’s a topic for a different post :)
breaking things into smaller pieces can give you some of these qualities of resilient systems, but not without some forethought. for example, the smaller we break our systems the more we may need to “orchestrate” them or rely on “downstream” services for data, functionality, or both. if these downstream services go down, what does our service do? the more we have these types of dependencies the more interconnected our services are at runtime. if a service on which we depend makes a change to its contract, payload, or event structure, how does that affect our service? are we forced to change when our collaborators do? this sounds quite brittle if we do. will the service provider run multiple versions of their service for us? a lot of times we rely (or imply) on once-and-only invocation of a service for a given event. or at least once-and-only-once processing. what happens if an upstream service experiences network latencies and retries? we may end up with multiple invocations. what do we do?
these are all the details of microservices that get ignored at in all the hand-wavy hype at conferences and such, but they’re real situations. i’m going to make a concerted effort to cover these “microservices implementation details” as i build out a non-trivial example of “microservices” with step-by-step examples (i'll blog and announce them on twitter as i write them @christianposta). saying we’re “doing microservices” doesn’t make these distributed-problems go away. so let’s look at three fairly well-understood patterns that you should probably always practice when building your microservices to add resiliency.
promises and fallbacks
promise theory, first introduced by mark burgess to describe how it systems interact with each other, shows us that our systems may or may not be as well-behaved as we’d like. a service provider publishes its “intent” to “do something” and it may or may not do that “something”. in many ways it’s how we as humans interact with each other as well. the more we look at microservices as independent, autonomous “agents” in a complex system, the more we have to respect this autonomy by understanding these systems are voluntarily intending to provide some service and at times will be unable.
so what happens when things don’t go as planned? let’s look at a non-computer example for a second. say for a moment that i’m a consultant and i provide a service to my customers. maybe i’m an architect that helps you build microservices architectures and i’ve promised to deliver an on-site architecture workshop. this is me volunteering to provide this service to you. what if my flight to your company site is canceled (e.g. i was trying to fly through o’hare :) )? do i just call you up and say “sorry, cannot deliver the workshop, my flight was canceled”? i suppose i could. then next time you ask me to deliver a workshop you may second-guess things. but maybe i say “sorry, my flight was canceled, maybe i can find another flight?” or “maybe i can deliver it remotely”, or “can we reschedule?” i’ve voluntarily promised to deliver a workshop on microservices so it’s incumbent on me to do what i can to fulfill that service.
it’s important to think this way when providing a service in a microservice architecture. what happens when collaborator services are unavailable? what fallback procedures are available to me? a lot of times this fallback may be dictated by the business. maybe you returned a canned response. maybe you call a different service as a backup. maybe you do a simplified calculation yourself. either way, in the face of some unexpected fault, you should think through what alternatives there are to help fulfill (or partially fulfill) the service promise.
from our soa days we’ve been ingrained to think of service contracts as something the service provider publishes. in the above discussion about promises, it would be the “intent” of the provider. however, from the above, we also see that the provider may also run into situations where it cannot fulfill its promise and maybe it returns something else. how should the consumers react?
the service provider provides a contract of some form (ie documents or schemas that describe the payload of the request and expected responses) and the consumers conform to these documents and implement their internal data models in terms of what the provider has decreed. then consumers would unmarshal and maybe even validate the contents of the payload during these service interactions. now if the provider ends up changing the contract (ie adding new fields) the unmarshalling and validation of these data payloads may break. that’s not good because we value our service autonomy. we should be able to make changes to a service without forcing ripple effect of changes on other services.
a solution to this is based on the principle to be “conservative in what we send to a service and liberal in what we accept.” basically, we do “just enough” validation of the response and pull out just the data we need instead of trying to do full data validation. this means our unmarshalling logic should be smart enough to work around the parts of the data model/response that it doesn’t know (or care) about. moreover, if we can capture the parts of the response that consumers really care about, we can begin to return this in a feedback loop to the service providers to help them understand what’s actually being used across the service consumers and when they make changes what changes may be breaking changes. ian robinson from thoughtworks covers this well in consumer driven contracts: a service evolution pattern .
what happens when things go wrong? or when services fail? a service may go down in the middle of a transaction. a misbehaving service may be inadvertently pounding our service with requests. a consuming service may experience latency in the network (expect this in cloud deployments!) and may have timed-out and retried. a system that expects to receive once-and-only-once delivery of a message is brittle by definition. if you build your services to be able to deal with these kinds of “unexpected” behavior they will be far more resilient. we need idempotent services .
one example is to not exchange messages between systems as “deltas”. these are not idempotent messages; if you receive a message multiple times that says “increment x by 20” , you will probably end up with an inconsistent value. maybe prefer “current-value” type messages where if you receive them multiple times, they don’t add to any inconsistencies in the data.
another option is to employ infrastructure that can filter out duplicates. for example, in a failover scenario, apache activemq can filter out duplicates when a producer sends a message to the broker and then ends up failing over to a different broker for some reason; the broker index can track and identify duplicates and discard them.
yet another option is to track unique identifiers in the messages you receive in your services and reject those that have been processed successfully. storing this information in an lru cache helps you quickly diagnose whether you’ve seen a message and either return a canned response, the original response, or ignore it. apache camel makes it really easy to build services that use this idempotent-consumer pattern.
however you implement these patterns doesn’t really matter, but we need systems that can deal with failures gracefully. all of these are tried and true patterns. none of these ideas are new, but i don’t see them implemented that often. imho they should always be implemented. following these three suggestions will help you build resilient services, although they’re not the only things. other things to consider include isolation, bulkhead patterns, load-balancing, service discovery, apologies, eventual consistency, et.al. to help with resiliency. if one of the advantages of microservices is the resiliency aspect, we should design our microservices architecture with these concepts at the forefront.
Published at DZone with permission of Christian Posta, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.