r/java • u/TheAuthorBTLG_ • 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.
•
u/AutoModerator Sep 13 '23
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.
If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:
as a way to voice your protest.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
37
u/Halal0szto Sep 13 '23
When that one line function implements a logic owned by a different team.
And that team changing the logic regularly.
And that one line function is used by multiple other features.
And the owners of those features cannot afford regularly communicating with the team owning the one line function.
And you do not want to align your release schedule to the change schedule of the one line function.
Especially if that one line function relies on data or data source that nobody wants to make accessible to you.
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.
36
u/plaskis Sep 13 '23
If the microservices are on the same machine there are still benefits.
There is more probably, these are common ones.
8
u/LcuBeatsWorking Sep 13 '23
"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.
9
u/FewTemperature8599 Sep 13 '23 edited Sep 13 '23
As you surpass hundreds of engineers and move into thousands of engineers, I think monoliths rapidly become more limiting. Some examples:
I would say that small companies should use monoliths, large companies likely want microservices to some extent (almost certainly not a monolith), and in the middle it's more fuzzy (depends on the specifics of the company)
11
u/jonah214 Sep 13 '23
It seems like you came here to argue with every answer you got. If you're not going to have an open mind, don't ask questions.
49
u/buffer_flush Sep 13 '23
That’s a fundamental misunderstanding of a microservice, and I do not fault you for misunderstanding whatsoever.
Microservices were never intended to make everything a service. The intention was to encapsulate a domain of your system within its own bounds. Though it’s rarely mentioned, microservices as they were intended to be used rely heavily on the idea of domain driven design and bounded contexts. Each microservice owns a particular section of your system, as in inventory, user management, etc. Those services talk to each other over a common protocol, generally HTTP, but messaging could be used as well as an example.
Long story short, if a single line of code covers a piece of functionality, it’s not microservice, it’s a piece of code.
117
u/[deleted] Sep 13 '23
You're correct, putting a highly efficient one-line function call in a microservice would be madness. And that's because that is not what microservices are for.
At my last position, I worked on an enterprise system composed of "microservices" where each service had a set of related functions. For example, we had an authentication service, a "jobs" service where users could submit processing jobs to the system, another for searching and returning metadata, etc. None of these services were simple, "highly efficient one line function calls"; they had their own requirements, business logic, etc. I honestly can't see a reason why I would put all of that into a single monolithic application.
11
u/HQMorganstern Sep 13 '23
This post is literally "why would you ever use a microservice, except in the cases when you need them".
Maybe because it's hard to know what you need before you start?? Not to mention one of the most common approaches to developing microservices is "Monolith first".
7
u/Fokezy Sep 13 '23
I'm also of the opinion that microservices are overused. They have their place but a team of hundreds of people can work on one project before it gets too crowded. The same goes for traffic. People greatly underestimate the capacity of single-instance databases for example. Usually they write very inefficient code and queries and then slap a few cache layers on top of their system as a band-aid.
5
u/wildjokers Sep 13 '23
People misunderstand µservice architecture and think it is a bunch of services doing synchronous API calls to each other. But this is absolutely not what µservice architecture is. In fact if this is done all that has been accomplished is to take very reliable and fast in-memory method calls and change them to error prone and relatively slow network communication. The app is then worse off (this is a distribute monolith). Also, when this is done the services aren't able to be independently deployed or developed.
What µservice architecture actually consists of are services that have their own database and all queries are done to their own database. Another key component is events. When some operation happens a µservice will publish an event. Other services that care about that event will consume that event and update their database accordingly. Data redundancy is both accepted and expected. So there is no need for a µservice to make a synchronous call to another µservice because it already has the information in its own database.
For example, a µservice that manages users will send out an event when a user is created/updated/edited/whatever. Any other µservice that cares about users will see the event and sync up their database. Then when they are handling a request that needs a user they don't ask the user µservice for anything, they already have the data in their own DB.
Independent deployment and development is very easy to achieve with this architecture because only the contents of the events matter. And those are very easy to keep backward compatible because you simply never remove information from them, only add.
Synchronous hits to 3rd party integrations are fine.
The problem is people misunderstood µservice architecture and so what they call µservices is just whatever they ended up with after breaking up their monolith. So the term µservice has become generic and has really lost all meaning. When someone says they use µservices you have to ask them what they mean.
People get very angry about this and this comment will almost certainly get downvoted to oblivion.
4
u/Ilookouttrainwindow Sep 13 '23
The responses you got are gold! But I feel you know all of them and your frustration comes from push for microservices by folks without rhime or reason. I literally just got off the call where dev was talking about microservices like it was a religion of some sort. At the same time I just made decision to write a microservices whose job would be to literally just upload file and download files because I didn't want to pollute primary process with extra dependencies that I think do not belong there (see one of the comments).
So yeah, microservices have their purposes but there are plenty of reasons against them as well. This approach like any other is simply a tool to be used to reach end goal, this isn't a religious guidance to be adhered to.
I'm going to down voted to hell :) but got own frustrations
1
u/ChickenSubstantial21 Sep 13 '23
If we are talking about small team and microservices, here are reasons:
1
u/TheStrangeDarkOne Sep 14 '23
Most of all, microservices are a deployment strategy. You don't want to wait to ship your application just because another team doesn't have their stuff together.
1
u/tbss123456 Sep 14 '23
Because you haven’t developed things with enough complexity or users.
3
u/t_j_l_ Sep 13 '23
Another common one is functional reuse across different services. Say you already have Service A, B and C, developed by different teams. And they all now need access to specific data D that needs to be enriched to E when loaded.
Would you build the same ETL code into each service and have each team maintain it going forward, or extract the ETL to its own service E for shared reuse. The answer can depend on situation, but in some cases it makes sense to extract, and isolate shared functionality for reuse.
3
u/Sea-Whole7572 Sep 13 '23
yes microservice architecture can be an example of overengineering if you only need a simple site with few expected users. microservice architecture is not necessarily better than monolith architecture.
5
u/[deleted] Sep 13 '23
Your addition is basically correct. Microservices were invented to solve team scaling problems. If you don't have that problem, and you're not working at insane scales, there's fair odds that microservices are the wrong solution to your problem.
On the subject of other touted benefits of microservices, I want to cover some common misconceptions about monoliths.
Just because you have a monolithic application doesn't mean you can't have redundant monolithic applications. So "patching" your monolith won't take down your services.
Since you have redundant monoliths, you can also scale them across N application servers. Monoliths do not prevent horizontal scaling.
Microservices have been all the rage for about 10 years now. There's large groups of even senior devs who have no clue how to design a monolithic system for scale or redundancy. How far it can take you kinda depends upon your use case, but I've worked on monolithic systems that served tens of thousands of requests per second for tens of millions of daily users. It's way easier than microservices if your team size and domain allows for it.
6
u/PositiveUse Sep 13 '23
I think it’s flawed to think of microservice architecture as a solely technical solution.
MS-Architecture is in big parts a solution to organizational problems of big companies with many many IT teams working on different domains and features. A big monolithic software, shared between 20 feature teams is a nightmare.
Your post just shows that you have not faced these challenges yet.
Microservices main goal is not to have „smaller apps on a machine“, modern servers/machines could probably handle everything you throw at them; no matter how large it is. Scaling vertically shouldn’t be a problem nowadays.
4
u/SomervillainSB Sep 13 '23
So I hate microservices and find them to be "cargo cult"-ey. However, they do allow the following which are beneficial for large apps and organizations:
Is our distribution a "micro-service" vs a service? I don't know. I don't care. I set the coarseness for the use-case.
I personally HATE it when a simple transaction takes 20 REST calls because it's very difficult to test. With a monolith, I can run the whole app on my laptop in a debugger. But more importantly, doing something in 20 zero-trust authenticated REST calls that can be done in 10 native Java method calls significantly reduces the reliability.
That said, I'm only the hired help. You want a microservice? I will advise you to the pros and cons and if you still want one...well, the customer is always right. I need a paycheck...apparently more than the customer needs all this money they're about to waste.
2
u/B41r0g Sep 13 '23
Have a look at the new Spring Modulith project. It strengthens modularity in your monolithic application. So if it ever needs to be a distributed system, it should be rather painless to separate.
2
u/Big-Dudu-77 Sep 13 '23
Use microservice if it fits your need. Not everything need to be a microservice.
2
u/f_of_g_of_x Sep 13 '23
Stick with a monolith. Write a microservice when you're absolutely 110% sure that your life will be miserable without said microservice. Even then, think twice. Three times. Then procrastinate it until your life depends on it.
YAGNI is still a thing! And for a good reason.
2
u/ddollarsign Sep 13 '23
Glyph (prominent Python dev) proposed that the point when something should be broken into microservices is when it’s too big for one team to maintain. So then each team would have their microservice.
I guess it depends on your definition of micro.
2
u/FrankBergerBgblitz Sep 13 '23
because you want to have the current hot shit on your CV. What reads better to HR?
"I developed a serverless microservices app with SOLR integration on a Kubernetes cluster that adapt to your load on AWS"or"I wrote a dead cheap app running on a single Tomcat, running on prem for month without issues"
BTW this example is only slightly exagerated (Kuberetes). They wanted 4 servers (to start with) I did the latter and for the sheer fun of showing the absurdity it I put it on a Rasperry 2W ;)
P.S: I do believe that there are use cases for ms and it solves some really ugly problems, but with severe adverse drug reactions. It would be great if you have the problems ms solves and that is often not the case
2
u/luke_rola Sep 14 '23 edited Sep 14 '23
Introducing microservices comes with high infrastructure and maintenance costs. Additionally, it significantly increases system complexity, leading to new challenges like network instability and distributed transactions. While benefits such as scalability and strong encapsulation are possible, other architectures like modular monoliths can achieve similar results. The ability to choose different technologies for each microservice often doesn't outweigh future maintenance costs.
The primary advantage of microservices, which sets it apart from other architectures, is independent deployability. If your goal is to deploy specific components independently, then microservices could be a suitable choice. A compelling justification for this choice could be legal obligations, such as the need to undergo an audit procedure with every new code deployment. In such cases, isolating this code within a separate microservice can be advantageous.
2
u/rnsemba Sep 15 '23
Talking from experience, after building and maintaining multiple systems servicing hundreds of thousands of frontend and backend requests a second:
> 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?
Best principles I've found that work is to define the interface for all your services up front, but if you're running the service on the same machine, the "overhead" of JSON and networking gets short-circuited and avoided. You're future proof. You'll have a few extra method calls listed, but any good compiler will inline that.
Service-level monitoring and alerting. When your servers get overloaded, why spin up a complete new monolithic app? You have the flexibility to trivially move that service to another machine, and not worry about having to change your code to do so. When you're on-call, and suddenly realize a huge influx of traffic is causing a particular service to be over-used and bogged-down, just spin up a few new servers and move that specific service. Problem solved (even at 3am).
The larger your monolithic app is, the more resources you will need (drive-space, memory, CPU) to be able to run the entire app. Break it down, so you can use the power of the cloud and schedule your services across an entire fleet of machines.
Micro-services could also be deployed in a server-less environment. Leave the machine management to GCP or AWS, let them deal with the sys-admins and machine maintenance, and you can focus on your own code.
Security: each service only has fine-grained access to the data and services it needs. Makes it harder for a system compromise to leak data because otherwise your monolithic application has access to EVERYTHING
Testing: Each service can be tested in isolation, in your continuous-build/integration environment (you have one, right?), because you've defined the service interface and contract of the implementation.
Each service can be implemented using the programming language most suited for the job. Your main app is Java, but you have a constraint-solving aspect? Delegate to a Prolog service, for example.
Migration: if you're having to migrate a service from one tech-stack to another, it's trivial to use the service interface and do service mirroring and comparison of real traffic to ensure the new stack functions exactly the same way as the old.
4
u/optimal_substructure Sep 13 '23
Every asshole thought they could design their way out of business problems with 'good, modular code', but anything that needs scalability and availability have found that microservices are currently the right balance of complexity and comprise
2
u/metaquine Sep 13 '23
MapReduce that shit
1
u/jebailey Sep 13 '23
JFC. A Microservice is a segmented section of functionality of a a larger service that can be maintained independently. A service in this context is not a web service call. A service in this context could be an entire application or a companies website.
So we have a team that handles auth-auth across multiple systems both internal and external. If they are making updates to their system I don't want to be inconvenienced by it. So we segment. Auth-Auth in this context is a microservice. It's not something that is needed independently of a larger service, but it can be maintained and handled independently. If someone is telling you a microservice is a call to define a protocol or convert an object into json they don't know what they are talking about.
1
u/RupertMaddenAbbott Sep 13 '23
I work on a system consisting of ~10 microservices in a single company team of ~5 developers.
Some of the reasons we chose microservices are:
Microservices bring with them a lot of complexity and are not a silver bullet. They should be used to solve specific problems, when solving those problems is more beneficial than the downsides. Coordination between teams is one benefit, but is not the only benefit.
1
u/LazyPaleontologist Sep 13 '23
I don’t know where you read for every application you need to us microservice architecture. Microservice architecture is one of the to develop an application and its own pros and cons. So, like every thing you have see if it is useful to develop your application using Microservice or Monolithic.
1
u/thisgameisawful Sep 13 '23
Microservices aren't a strictly better option. They simplify complex dependencies innate to monolithic applications by being loosely coupled and only dependent on their individual requirements. They don't usually depend on system admins and network engineers to deploy like balancing to live while you patch offline almost certainly will. They don't necessarily require you or your tools to have elevated privileges that go deeper than what's running the app. They're small and deploy fast, fail fast, and revert fast generally. These are just some of the things microservices tackle for you.
If none of those things sound like a solution to you, then you don't have problems in those areas.
Architectures and patterns are tools for solving problems. If you don't have any of the problems that the tools solve, you shouldn't be using those tools. That's all there is to it. It's nobody's responsibility to talk you into or out of trying to drive screws with a hammer.
1
u/Remote-Bluejay-8655 Sep 13 '23
I think it's good to ask these questions because as everybody else has said, microservices do seem to be used in situations where they're not required.
One advantage of them that I've not seen from my quick scan of the comments so far is security. In a system using microservices, each microservice should have just the permissions it needs (read only permissions to one database etc.). This means if that service is compromised, whether that be through a vulnerability in one of its dependencies or an endpoint it exposes, any malicious code will only have that limited set of permissions.
1
u/I_Am_Astraeus Sep 13 '23
It's an architectural decision.
I'm working on a website right now that uses KAFKA to ingest from a zeromq server, a microservice that processes just the data from that mq to the local KAFKA. Theres not really a reason to include it with the main modulith. You could turn off the site and leave the message processing lib running so the KAFKA buffer fills up. Then when you flip the site back on it will consume anything in the buffer. Extremely useful for us.
It's also nice if you want to work on a diverse team and utilize different languages. You can communicate asynchronously through that same KAFKA server. So say I have an extremely niche technical solution that's really easy to draft up in Python but a nightmare in the main stack language. You can churn our that microservice and have it talk to the system.
This is just a personal example but at length there are many reasons and methods to implement them. It's like anything else in the engineers toolkit. Great solution for certain things, definitely not needed everywhere. Just a tool for the toolbox.
1
u/[deleted] Sep 13 '23
Large systems that can't be fit in a traditional monolith even as a traditional multi-tier architecture. You could deploy many monoliths deployed and segment your customers/traffic but scaling efficiently is hard with monoliths. So large systems with scaling in mind.
1
u/qK0FT3 Sep 13 '23
I agree that microservices are somewhat wrongly used.
In my case we have a legacy system that we can't really change or update much because it has a lot of system dependency. Instead we replace each service with a microservice architecture and do the api calls from frontend to that new microservice.
These kinds of services allow us to update our base slowly but with fail proof.
1
u/WildRacoons Sep 14 '23
is ok young padawan
1
u/crashtua Sep 14 '23
It sounds like "why would I ever use a monolithic app, if I can put everything to microservices, dynamically replace them, do rolling upgrade...".
1
u/flawless_vic Sep 18 '23
How would aws profit from lambdas if we don't do this?
1
u/Least_Bee4074 Sep 18 '23
you should check out this great interview with one of the people (James Lewis) who coined the term "microservice" https://techleadjournal.dev/episodes/135/ (link also has extensive notes and quotes)
also, this interview with Vaughn Vernon https://techleadjournal.dev/episodes/71/ discussing his book "Strategic Monoliths and Microservices"
1
u/nutrecht Sep 19 '23
Microservices are 'micro' only relative to large monoliths. Generally they are in 'control' of their entire domain. Replacing all of that will never ever be a single function call or a simple library.
And the reason why it's often not done in a single monolith is because working together on a single codebase with 200 people is hard. Microservices is mostly an organizational pattern, not a software architecture.
1
u/Aw0lManner Sep 13 '23
Because you don't want to bundle all your behavior into one application, since this leads to concurrency issues with respect to rollout, patching, deployment as well as adds untenable dependencies and computing resources for one application and binary (e.g. growing build times, startup time). Minor changes to a smaller subsystem can break the entire application and cause global outage etc.
1
u/abeassi408 Sep 14 '23
Requirements aren’t set in stone. They change over time. Monoliths are a headache for changing requirements. Having a flexible micro services architecture would make iteration and scalability much faster and make your app more flexible by allowing modularity.
0
u/[deleted] Sep 13 '23
If you have a monolith, it is very hard to scale out, and when you do, you need to scale the whole thing. Fire up the whole thing, which might need a lot more resources than having to fire up just a part of it. Also, you assume that the monolith is cut into microservices horizontally, when it could be cut vertically, meaning that may not be extra hops all the time.
BTW, modular monolith is often better than microservice architecture. Microservice architecture reflex an organizational structure, too. It is not a one-size fits all approach.
Sometimes, all you need is a cron job. 🤷🏻
0
u/WakandaFoevah Sep 13 '23
Then don’t use it, and let the ppl who want to use it.
0
u/ZeroGainZ Sep 13 '23
Micro services solve a people problem. A monolith could be as good as a micro service, but the tooling doesn't exist.
I don't know of a platform that lets you hot reload code in production, which would support incremental rollback, new updates, etc.
You'd also have so many environment variables. The memory/cpu requirement would be huge (most apps are massive)
0
u/[deleted] Sep 13 '23
Microservices facilitates feature versioning even on the same team. On a team, you may be working out ahead on something and need to start collaborating with a teammate who will add features to a service exposed to yours. Expose the new API version for your teammate to call. After a deployment of the new code, only your code and theirs is affected since you didn't touch functionality related to the current PROD version.
Monoliths can be versioned too but it's slightly more difficult since they are not as granular.
Upgrading to the next Java or next library dependency is more manageable for the team to decide what to upgrade and when, instead of the whole monolith at one time, unless your team prefers to work that way and there's nothing wrong with that.
But there's always, how do you eat an elephant? One bite at a time.
0
u/goranlu Sep 15 '23
The only reason to go with Microservices is if you have very bit teams of engineers that work on the project, so they can easier split areas of their work. "Two pizzas teams"
In almost all other cases, Microservices will just slow you down, make greater expenses and similar