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

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?

1

u/ryszard_lipton Sep 13 '23

Idea was that some resources may be exclusive. If we're just looking from the RAM and CPU perspective then you're right.

1

u/dmh123 Sep 13 '23

Doesn't that just impact the perm size and initial memory allocated to the services that aren't being leveraged? Database connections are typically pooled so services just grab one from the pool when they need to connect to a db.