The Hardest Part of Microservices: Calling Your Services
You’re probably saying, “Wait. You already told me the hardest part of microservices was my data. So what is the hardest part? That? Or calling your services?”
Join the DZone community and get the full member experience.Join For Free
you’re probably saying “wait. you already wrote a blog telling me the hardest part of microservices was my data . so what is the hardest part? that? or calling your services?”
there are lots of hard parts of microservices, actually. the blogosphere/conferencesphere/vendorspehere tends to romanticize microservices, but from the technology perspective, we’re building distributed systems. and distributed systems are hard.
i work very closely with red hat’s top strategic customers to help them successfully navigate these hard parts, implement services architectures to stay competitive, innovate, and succeed in generating business value . i also stay very close to how quickly technology is evolving in the open-source communities. you can follow me @christianposta as i often speak/write/discuss these topics. but my background is integration and asynchronous messaging, so the topic at hand is something near and dear to my heart: getting services to talk to each other using various conversations patterns .
as we continue to iterate on these concepts surrounding services architectures, we inevitably have to deal with a major source of these hard problems. i’m hoping that as you explore and begin implementing microservices, you’ve seen and digested the fallacies of distributed computing. but i also recommend jeff hodges’ “notes on distributed systems for young bloods.”
i feel like we’ve been chasing our tail a bit with past solutions to some of these problems. we’ve been trying to find the holy grail of “how do we just let developers write the business logic that will deliver value and try to abstract away distributed systems.” we’ve done things like making service calls look like local calls by abstracting away the network with local interfaces (mmmm… corba , dcom , ejbs , etc). but then we learned that’s a bad idea. then we switched to things like wsdl/soap/code generation to get away from the brittleness of those other protocols, but we continued the same practice (soap client code generation?). some folks actually did make these approaches work, and they’ve got many scars to show. let’s look at some of the problems encountered when simply calling your services:
- failures or latency.
- service discovery.
failures? or latency…
what happens when we send a message to a service? for the purposes of this discussion, that request gets broken down into smaller chunks and routed over a network.
we deal with the fallacies of distributed computing because of this "network." our applications communicate over asynchronous networks which means there is no single, unified understanding of time . services live in their own understanding of what “time means” and that could be (is) different from other services. more to the point, these asynchronous networks route packets based on availability of paths, congestion, failures in hardware, etc. there is no guarantee a message will get to its intended recipient in bounded time (note, this same phenomenon does not occur in “synchronous” networks which have a unified understanding of time).
so that sucks. now it becomes impossible to determine failure or just slowness. but if a customer makes a request to search for concerts on our ticket-selling website, we don’t want to wait until the universe melts away. we want to be responsive, and at some point just fail the request. so we add timeouts to our services. but, one does not simply add timeouts to their services.
when you’re making downstream requests, we can’t afford to be slow because our down-stream network interactions are slow. some timeouts to think about setting:
- how long it takes to establish a connection to a downstream service so we can send a request.
- whether or not we’re getting responses back to our requests.
quick side note: the huge advantage to building a system as a services architecture is speed which includes speed of making changes to the system. we value autonomy and frequent deployments of our system. but when we do this, we can quickly find ourselves in odd situations where timeouts don’t properly work well together.
consider our client application sets a timeout of 3s to get a response back from a recommendation engine. but the recommendation engine also consults a relevance engine. so it makes a call with timeouts set to 2s. this should be okay because our upstream service call will wait for at most 3s. but what if the relevance engine has to converse with a promotions service? what if those timeouts end up being set to 5s? our testing (unit, local, integration) of the relevance engine seems to pass all tests even under latent operations because our timeouts are set to 5s and the promotion service either doesn’t take that long, or the relevance engine properly ends the call after the timeout elapses.
what we end up with is a nasty, very difficult to debug situation when lots of calls are coming in (or not… this can happen at any time because our network is “asynchronous” remember?). timeouts are great, until they’re not.
since we really don’t have any bounded-time guarantees in distributed systems, we will need to timeout at some point when things are taking too long. now we end up in a situation of “what do we do after we timeout?” do we just throw a nasty http 5xx to our caller? do we take some advice about 3 easy things to make microservices resilient and promise theory and fallbacks? or do we retry?
if we retry, what if we’re making a call that changes data on the downstream service? check that 3-easy-things blog aforementioned, but i also cover some of that from the data/consistency perspective in my blog (and associated talks), the hardest part of microservices was my data .
but more interestingly, what if our downstream service starts failing and we end up retrying every request? what if we have 10s or 100s of instances of our recommendation engine calling our relevance engine, but our relevance engine is timing out? we end up with a variant of the thundering herd problem . this ends up ddos our services even as we try to remedy and slowly bring back the affected service.
we need to be careful with our retry strategy. exponential retry backoff can help, but may still experience the same issues.
because we deploy our services with resilience in mind, we ideally have multiple instances running in different fault-tolerance zones such that some zones can fail without taking down the availability of our services. but as things start failing, we need a way to route around these failures. but we may have other reasons for service traffic routing in/between fault-tolerance zones. maybe certain “zones” are implemented as geographic deployments of our services as backup; maybe it’d be too expensive from a latency perspective to have our traffic go to our backup instances during normal operation. or maybe it costs too much money to always route traffic that way. so we may want to shape traffic around these considerations.
maybe you want to route ingress client traffic like this, but what about inter-service communication? what about the routing for the same considerations between services that must converse with each other to satisfy client requests?
a variation of routing between fault tolerant zones could be routing and load balancing requests around outlier services or services that appear to periodically be slow? we’d like to adapt our routing to services that can keep up with our service calls. it makes very little sense to keep sending traffic to services that can’t seem to keep up.
we can discuss even more trickiness with routing when we talk about how to deploy new versions of our services. as mentioned earlier, we’d like to maintain some level of autonomy between services so we can quickly iterate and push out changes. we’d like to not break our dependent services, so what if we could route certain portions of traffic to our new versions and have this tied into our build and release strategies (ie, blue/green, a/b testing, canary releases ). this gets complicated quickly. how do we determine what traffic to route? maybe we’re doing testing in an ad-hoc “staging environment” that lives in production. maybe we’re just doing an unannounced/dark launch. maybe a/b tests. but what if we have some kind of state involved. we’d need to consider data schema evolution, multi-versioned database implementations, etc.
alongside some of the resilience considerations discussed earlier, in an environment where expect failures, how do we discover our collaborator services? how do we know where they are and how to talk to them? in traditional, static topologies, our applications would be configured with the urls/ips of the services with which we’d need to talk. we’d also build these dependent services to “never fail”. but inevitably they’d fail (or partial fail) in some unforeseen ways. but in an elastic environment, this kind of behavior from our collaborators should be expected. these downstream services could autoscale, reshape to different fault-tolerant zones, or simply be killed and restarted by some automation. clients of these services need to be able to discover them at run time and consume them regardless of what the topology looks like.
this is not a new problem to solve, but it gets difficult in elastic environments. things like dns just don’t work (unless you're lucky enough to run in kubernetes ) well.
trusting your services architecture
i’ve saved the most important consideration/problem for last. microservices is about making changes quickly to your system. if you cannot trust your system, you’re going to hesitate about making changes to it. you will slow down releases. if you deploy slower, this means you’ll have longer cycle times. which means you’ll probably try to deploy bigger changes. you’ll need more coordination between teams. you’ll develop a "this is how we’ve always done it" attitude until you’ve stifled your businesses it capacity. sound familiar?
you need to have trust in your systems architecture. with a monolith, you could at least trust if you’ve screwed something up it’s “somewhere in the monolith”. in a services architecture, all bets are off. how do you know what’s going on when things fail? you may have some complex combination of infrastructure (physical/virtual private/public/both .. containers?), with myriad deployments of middleware, services, frameworks, languages, and each service with its own release cadence. if a request is “slow” where do you even begin?
you need strong “observability” in your system. you need useful/effective logging, metrics, and tracing. if we’re quickly iterating and releasing services, we need to be data driven, understand the impact of our changes, rollback where it makes sense, or quickly release new versions to work around negative observations. all parts of our system need to be able to give reliable observability (logging/metrics/traces). the more we can trust this data, the more we can trust our services architecture and the more faith we’ll have for making changes.
to recap: we’re solving for…
so to recap… and to drill a bit deeper, when our services call other services, we need to solve for:
- service discovery.
- adaptive routing/client-side load balancing.
- automatic retries.
- timeout controls.
- back pressure.
- rate limiting.
- metrics/stats collection.
- a/b testing/traffic shaping/request shadowing.
- service refactoring/request shadowing.
- service deadline/timeout enforcement across service calls.
- security between services.
- edge gateway/router.
- surgical/fine/per-request routing.
- forced service isolation/outlier detection.
- fault injection (i.e., injecting delays? dropping ingress/egress packets?).
- internal releases/dark launches.
how do you propose doing this? what did other people do?
if you look at how the google/twitter/amazon/netflix folks solved this, you could say they brute forced a solution for this. they (simplistic explanation) basically said: “okay, we’re going to use java/c++/python and invest untold amounts of brute engineering force to build libraries to help our developers solve for this.” google created stubby. twitter created finagle , netflix created and open-sourced netflix oss , etc. others have done this as well, although maybe not at the level and amount of investment that some of the internet companies did. even we (fabric8 team) did this with the original fabric8.io 1.x (deprecated).
for anyone else, there is a single big problem with this approach:
you’re not netflix
i don’t mean this in a condescending way. i just mean it's not practical to invest substantial amounts of engineering/money to solve these problems the way they did. the type of companies we see interested in microservices are interested in doing so for speed to value and innovation; this is an area they don’t have expertise in.
so maybe you could just re-use their solutions?
that leads to another quite interesting problem:
you’ll end up with a very complicated, ad-hoc, partially implemented solution
think about it this way. a lot of customers i interact with are java shops. they think about solving this problem for java services. so naturally, they gravitate to netflix oss or spring cloud or something. that’s awesome! but what about their nodejs services? what about their python services? what about their legacy applications? what about, dare i say, their perl scripts?
each language has its own implementation of these problems. each is implemented to a different quality. it’s not as simple as grabbing open-source libraries and stuffing them in your application. you will have to test and validate each and every implementation. you are responsible for your services architecture, not these libraries. chances are high this proliferation of implementations/languages/versions will quickly become an insurmountable complexity.
another architecture smell i see:
we’re implementing lower-level networking functions at the application layer
so why do we complicate our applications with these things? we’ve been trying to solve for these problems in the applications by creating libraries (a library that does circuit breaking, one that does service discovery, one that does tracing, a different one that does stats collections, even more for doing complex routing, etc) and jamming those into our application space (as dependencies, transitive dependencies, library calls, etc). what happens if your service developer forgets to add a part of this implementation (tracing for example)? so the onus is on each developer to implement these properly, bring in the right libraries, compose them in their code, etc.
not to mention some of the frameworks that try to alleviate this with magic configurations of annotations in the java space. the goal of trust, observability, debuggability, etc, is missed with an approach like this.
as i’ve mentioned in my “microservices 2.0 blog,” i am partial to an implementation that’s able to do this with a more elegant approach.
so where does that leave us?
imho, this is where a service-proxy/sidecar pattern can help. if we can solve for these constraints:
- reduce any application-level awareness to trivial libraries if any.
- implement all of these features in a single place, not strewn about a dumping ground of dependencies.
- make observability a first-class design goal.
- make it transparent to our services, including legacy services.
- have very low overhead/resource implications.
- work for any/all languages/frameworks.
- push these considerations to lower levels of the stack (see above).
…then i’d think we’d be on to a more elegant solution to these problems. ask yourself:
do i spend the next few years implementing my services architecture using technology built for 5+ years ago?
some interesting technology that’s been percolating from some recent companies at the vanguard of microservices to implement the service-proxy sidecar pattern includes:
in the next blog post, we’ll look at each of these implementations in detail, and more specifically, how to use them on kubernetes.
i’ve run out of steam for this blog, and if you’ve followed me until this point, many thanks.
Published at DZone with permission of Christian Posta, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.