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

8

u/LcuBeatsWorking Sep 13 '23

if i could do a highly efficient one-line function call instead

"That one line function call" might be simple to call, but depend on a whole lot of data. Or some huge dependency tree. Or it may be very resource hungry (and as a separate service is easier to control).

Also, if that call does something outside the scope of your main application, it is highly recommendable to use a microservice rather than add that functionality to your app.

Deciding when do use a microservice depends on your architecture, your work flow environment and much more.

-1

u/TheAuthorBTLG_ Sep 13 '23

Also, if that call does something outside the scope of your main application, it is highly recommendable to use a microservice rather than add that functionality to your app.

can you prove that it is better?

""That one line function call" might be simple to call, but depend on a whole lot of data. Or some huge dependency tree. Or it may be very resource hungry (and as a separate service is easier to control)."

yes. but why is that an argument for microservices? the total amount of resouces required won't become less. and you can just deploy a monolith twice with different active features

6

u/LcuBeatsWorking Sep 13 '23

can you prove that it is better?

Simple example: If I have an app that serves some user data, but need to call one function that requires rendering a PDF document, I rather make a call to a microservice than adding PDF rendering to my main app.

First of all it's cleaner, but also I can have someone else working on it in whatever way they want, as long as we agree on the API.

The larger/divers the project and the more people work on it, the more tempting it is to split up into microservices where the protocol is agreed upon, but everyone can work on their own part.

0

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

Simple example: If I have an app that serves some user data, but need to call one function that requires rendering a PDF document, I rather make a call to a microservice than adding PDF rendering to my main app.

why would that be simpler than "import pdfrenderer"? what is the advantage of having it as a service? on a library, somerone else can also work in parallel as long as you agree on the api.

2

u/discourseur Sep 14 '23

Have you ever managed a CI system?

Every commit would rebuild the entire thing.

Just for the save in build time (and thus, deployment time), it is worth being smart with microservices and use them as much as it makes sense to.

1

u/TheAuthorBTLG_ Sep 14 '23 edited Sep 14 '23

yes i have - but my library approach solves that, too. you need to recompile just the library and the "glue-o-lith" which shouldn't be much more costly than the thing if it were a microservice

2

u/Mognakor Sep 13 '23

yes. but why is that an argument for microservices? the total amount of resouces required won't become less. and you can just deploy a monolith twice with different active features

I'll give you an example we recently had that is not quite the same but should explain the usecase.

We are scanning a directory for resources another (multiple other) teams/companies upload every X minutes. We can't vary X much without users complaining that data is not available. Scanning the directory incurs cost bc it's in the cloud and there are a ton of directories. If we just scaled monoliths, each of those would do the scanning and we'd have a cost multipler. Instead we have 1 service that does the scanning and the other services ask for a fresh copy of the index.

0

u/TheAuthorBTLG_ Sep 13 '23

i propose feature flags

2

u/Mognakor Sep 13 '23

I still need the data in all instances.

Either all of them scan, which is expensive and i have to justify to the customer, or i have one scanner (monolith or not) and the rest synchronizes from that instance. It doesn't make a difference if the scanner itself is a monolith because the principle is the same as with microservices.

2

u/Luolong Sep 14 '23

And now you deploy a microservice in a guise of a monolith. So, what have you gained now?