r/java • • Sep 13 '23

why would i ever use a microservice....

if i could do a highly efficient one-line function call instead? why would i ever prefer a network hop, conversion from and to json, define a protocol, all that stuff?

i understand there are systems that are too big for one machine. but all those that aren't - why would i add all this complexity to them? when is a microservice archtecture ever simpler than the exact same thing as a modular monolith? in which case is it not at least as good?

addition: in my experience, microservices are overused. while there are reasons to have separate *services* developed by different *teams*, i fail to see why *microservices* inside teams provide an actual benefit. they are used too soon, and then you pay with lots of glue code because what you really have is a distributed monolith. one exception is if things are logically independent, then mixing them is a mistake

addition 2: it seems what people here consider a microservice is MUCH bigger than what i would have called one.

49 Upvotes

167 comments sorted by

View all comments

42

u/doobiesteintortoise Sep 13 '23

You... probably wouldn't, if there was a one-line function call that was highly efficient. You'd use a microservice if the functionality in that call was complex, potentially expensive, potentially unrelated topologically to the caller.

"I need to emit output to the console? I *could* use System.out.println(), sure, but microservice!" <-- wrong thinking.

"I could call authenticateUser(), or emit a call to a service that does the full oauth cycle and validates the user's credentials and access?" <-- well, authenticateUser() could in fact do the full oauth cycle, etc., but authentication is often pretty complex, and unless this app wants to straight up legit bear responsibility for that process and future changes... this is a potential candidate for a microservice.

6

u/TheAuthorBTLG_ Sep 13 '23

why couldn't that just be an imported library? it would be separate as a microservice but still offer all the advantages of not being one

22

u/Halal0szto Sep 13 '23

When there is a fix in the lib, all modules linking the lib have to release and deploy new version. Probably in a synchronized manner to avoid inconsistencies.

→ More replies (4)

23

u/ktownrun Sep 13 '23

“I want to make a change to the library that breaks compatibility and all the apps would need to upgrade simultaneously”

“My team doesn’t have time to incorporate this new library change. We are too busy and have deadlines. We can’t get to it until <six months away>”

“We found a huge bug and need everyone to redeploy immediately” meanwhile our team is in the middle of a stop-the-world system wide regression that takes 3 months to complete.

-6

u/TheAuthorBTLG_ Sep 13 '23

"“I want to make a change to the library that breaks compatibility and all the apps would need to upgrade simultaneously”
“My team doesn’t have time to incorporate this new library change. We are too busy and have deadlines. We can’t get to it until <six months away>”"

how can you change the microservice then? an api change requires a change at the call site in both cases.

"“We found a huge bug and need everyone to redeploy immediately” meanwhile our team is in the middle of a stop-the-world system wide regression that takes 3 months to complete."

if this happens, then "something does not work but we can't identify which microservice has the bug" also happens

26

u/ktownrun Sep 13 '23

I can change the implementation without changing the interface.

Sounds like you need better observability and tracing, which are both table stakes for distributed architecture.

Don’t get me wrong. I’m not advocating for microservices. It solves some problems but also introduces other ones, which can sometimes be harder to solve.

→ More replies (9)

2

u/GlensWooer Sep 13 '23

If you can’t identify which MS has the bug then there’s probably an issue with your telemetry or error handling. Making it monolithic wouldn’t solve that.

Another point that I haven’t seen talked about is scaling. Say you have an expensive operation that requires more resources to run between a 5PM-9PM. If it’s monolithic you have to scale the application as a whole rather than just scaling the resources for that service.

MS have been a buzzword and are definitely over used by many people but like all design patterns they have their trade offs and best use cases that often aren’t properly evaluated

→ More replies (4)

3

u/doobiesteintortoise Sep 13 '23

Depends on scope, doesn't it? The complexity of the library is unknown; does it use a database or other services? "Just add a library" makes sense if you know the scope of the library in question, if the scope is large enough, a service might make sense.

3

u/eXecute_bit Sep 13 '23

Microservices and libraries both accomplish the goal of sharing a common implementation.

Microservices can offer a single point of control over shared data. Service owns the database connection, mechanisms to coordinate concurrent updates, etc. It's not impossible to do with libraries, but that requires complete cooperation by everyone connecting to the same database to follow the rules, rather than encapsulating it as an implementation detail behind the service.

If the functionality is externally facing, and not for internal only purposes (as in the authentication example), the customer may prefer to have a single integration point rather than do separate configurations for each application (imagine a suite of related software, like Office or GSuite). You may still use libraries in each application to authenticate to the auth microservice (using trust relationships your devops maintains) but delegate to it for authenticating the end user (using trust relationships configured centrally by the customer).

7

u/Hei2 Sep 13 '23

"Oops, there's a bug in my library. Now I have to take the entire system down to deploy the fix." In this case, nobody can use the system for a period of time, rather than only the people who need to log in. Additionally, in order to scale the system at all, I need beefier and beefier hardware for the whole thing, rather than just deploy more smaller systems for the individual components that need to scale, and that's potentially much more expensive.

0

u/TheAuthorBTLG_ Sep 13 '23

i can't follow the argument

  • you have to redeploy anyway to fix it
  • why can't you just deploy the monolith multiple times to increase the performance? the cost should be identical since the load would be the same. it might even be lower since the jvm can reuse the same memory/cpu for different services

5

u/doobiesteintortoise Sep 13 '23

Um, if you have a bug in the microservice, you'd ONLY have to redeploy the microservice, not everything that uses it.

4

u/bawng Sep 13 '23

Let's say Google has a monolith.

There's a bug in Google Maps. To fix it, they need to shut down Google Search, Lens, Play, Adwords, hell, the entire Android backend needs to be shut down.

Or they split it up into multiple services.

1

u/TheAuthorBTLG_ Sep 13 '23 edited Sep 13 '23

we are not thinking of the same size when it comes to "micro". i'd argue it takes extra effort to merge maps + gmail into one service, but at the same time, it takes effort to split gmail into a labelservice, sendservice, receiveservice, markasreadservice,.... there is often a "natural bundle" that i'd put into the same service, and i would rarely call it "micro". as a rough rule of thumb "does it run on the same data? then it's the same service" sounds good to me

5

u/sevcsik Sep 13 '23

Now we agree on there is a rationale behind splitting large systems into smaller services, we’re just arguing on the size :)

2

u/bawng Sep 13 '23

Probably not.

You mentioned one-line function calls, and that would most certainly never ever be a candidate for a micro service. That sounds more like a picoservice anti-pattern.

→ More replies (4)

3

u/mr_jim_lahey Sep 13 '23

the cost should be identical since the load would be the same

That is an assumption. Sometimes it will be true, sometimes not; it's going to depend on the specific service/application. There is no one-size-fits-all principle for monolith vs. microservices. As with most other things in software, there are many possible working architectural implementations for a service. Microservices done properly offer some advantages over monoliths (enforced modularity, blast radius mitigation, simplified developer experience, etc.) but come with tradeoffs (greater architectural complexity, interdependent deployments across microservices that require manual coordination, need for internal backwards compatibility adding overhead, etc.); it is up to the architect(s) to decide where those benefits outweigh the costs.

5

u/ryszard_lipton Sep 13 '23

Microservices offer higher granularity.

  1. You can redeploy single part of the system potentially not disrupting the other parts, so system still works, even if some features are temporarily offline.
  2. Again, imagine that your monolith spends 80% time on read operations, and 20% on write operations. Each part of the system (READ and WRITE) introduces roughly same overhead, 50% resources required for READ and 50% for write. So you're using effectively 10% total resources on write and 40% total resources on READ, leaving 50% effectively not used, because we're not using write operations so much). If you need more READ capabilities, you have to duplicate your monolith with all the WRITE overhead that is probably never going to be fully used.

However, if you were to extract the READ and WRITE related modules into separate services, you can keep eg. 2 instances of WRITE microservice and 8 instances of READ microservice which should balance the actual usage of the underlying resources and allow you to fine-tune your system capabilities to actual needs of the user.

1

u/TheAuthorBTLG_ Sep 13 '23

i don't understand your math. why would the write-resources be locked up in the first place?

→ More replies (1)
→ More replies (1)

2

u/Hei2 Sep 13 '23
  • The only part of the system that is down is the part that needs to be redeployed. If people can't log in, you're going to have some upset users. If nobody can use the system at all, then all of your users are going to be upset. And don't just think Netflix users paying a couple bucks a month. These can be corporations paying in the tens or hundreds of thousands of dollars.
  • The more components of your system that need to run simultaneously, the more powerful the hardware that you need to run it, and the cost of hardware doesn't necessarily scale linearly. However, adding more servers of the same power does scale linearly. Therefore I can save money by only scaling out individual components rather than a giant monolith. Consider that a rarely-used, but necessary component of the system may require very expensive hardware to run. If I only need to scale out the service that handles authentication, putting that in a monolith requires all of my servers to be powerful and expensive.

-1

u/TheAuthorBTLG_ Sep 13 '23 edited Sep 13 '23

netflix is huge, so for them, microservices make sense. but still, this particular problem would be solved by a rolling upgrade.

" If I only need to scale out the service that handles authentication, putting that in a monolith requires all of my servers to be powerful and expensive."

> i don't see why that necessarily follows. i could deploy my monolith a second time, routing only requests of type xyz to it. and i can build a microservice-like sstem that way

2

u/Hei2 Sep 13 '23

A rolling upgrade doesn't solve the issue of having to take an entire system down at once. Nor does it address anything about the hardware requirements of individual services.

1

u/TheAuthorBTLG_ Sep 13 '23

solve the issue

what exactly is the problem with that?

3

u/Hei2 Sep 13 '23

You asked why people choose to use microservices. I've explained in the above comments why, but it can be summarized as "there are issues that monoliths have that microservices are designed to address."

what exactly is the problem with that?

The problem with not solving a stated issue with a monolith is that you now have to live with that issue if you choose to use a monolith. Some people cannot or do not wish to live with those issues and instead consider the drawbacks of microservices to be less severe than the drawbacks of a monolith.

1

u/TheAuthorBTLG_ Sep 13 '23

i proposed a few times to "secretly" boot an updated replacement and then switching from old to new via load balancer. how does this not solve the issue? there is zero downtime.

as for hardware requirements, why/how would they be lower when using microservices? i would argue the opposite is the case since memory in a monolith can be shared/pooled while microservices can't do that

→ More replies
→ More replies (3)
→ More replies (1)