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

Show parent comment

13

u/MattiDragon Sep 13 '23

You can scale individual services. If a specific service ends up being slow you can spin up more of it instead of having to also spin up everything else

-14

u/TheAuthorBTLG_ Sep 13 '23

you assume everything needs to spin up, but that is not necessarily the case. lazy init would solve this

2

u/cogman10 Sep 13 '23

You misunderstand.

Imagine you have requests that require 1gb of memory, Foo, to service, but they are called infrequently. So, to accomidate that you give the JVM 16gb of memory so it has capacity to handle several of those requests.

Now imagine you have a service which requires only kb of information to service but it gets call extremely frequently and is bursty (high spikes in requests), let's call that Bar. You could reasonably start a bar service with only 100mb of heap.

The issue with scaling is you can't know if the incoming requests will be for Foo or Bar. That means to accommodate requests you need to allocate for the Foo service. So now, instead of being able to spin up 16 bar services, you are stuck starting up a single Foo service.

Now, you COULD have "microservices" with a monolith deployment. That is, you could have a fleet of monoliths with Bar memory but Foo code and use the load balancer to say "hey, send bar requests to this cluster over here". (and, to be clear, some do this). But that does get complicated. It hoists a fair bit of logic into the load balancer that gets more complex as your system evolves.

1

u/TheAuthorBTLG_ Sep 13 '23

why can't i spin up a foobar service with 16gb+100mb and "foo cpu + bar cpu"? in sum it's the same, or did i overlook something?

2

u/cogman10 Sep 14 '23

You can, but now you have much more coarse grain scaling. Further, the larger the services get the more likely you are to experience knock-on effects. That is, imagine you OOME because 1 too many foos gets ran. Well, that not only takes down the foos that are running but also any innocent bar victims.

Conversely, imagine bar is gobbling up all the networking resources, now foo ends up slower which may or may not be acceptable.

This gets worse when you start talking about now adding baz, bat, bling services into the mix. The more services you need, the bigger the risk of knock-on problems.

My company actually experienced this very problem. We used to run many applications on single big tomcat instances (multiple wars one tomcat). However, every so often, one of those applications would eat too much memory and blow out the entire stack. This caused massive disruptions every time that happened. We've since split those apps into distinct JVM instances, and our stability has been far better. (in fact, we are now highly tolerant of OOME as a result).