r/programming • • Jan 30 '23

When Do I Need Microservices?

https://levelup.gitconnected.com/when-do-i-need-microservices-d76fac58ac7c
600 Upvotes

253 comments sorted by

View all comments

Show parent comments

30

u/TooMuchTaurine Jan 30 '23 edited Jan 30 '23

If you have an automated deployment system and practice CD for a small to medium sized team (1 to ~20) you don't need microservices, you can easily deploy a single app 50 times a day.

And you'll be in a much better place to refactor and make larger sweeping changes when you inevitably get stuff wrong.

4

u/cat_in_the_wall Jan 31 '23

This works great if your app can be updated without downtime. Not every flavor of application has that luxury, especially ones that are extremely schema sensitive. I am not saying microservices are the answer, but "monolith" isn't a panacea either.

-1

u/TooMuchTaurine Jan 31 '23

Haven't run across an app scenario I can't easily deploy continuously so far..

In terms of schema, there are plenty of ways to deal with rolling updates to change schemas with no downtime.

4

u/cat_in_the_wall Jan 31 '23

"I haven't experienced this problem so it doesn't exist". I am happy that your relationship with databases has been so smooth. Not everybody has that.

24

u/WallyMetropolis Jan 30 '23

You don't ever need any particular architecture or methodology. There isn't one right way. And every approach has trade offs.

69

u/[deleted] Jan 30 '23 edited Jan 30 '23

With micro services, you put a network connection between modules that could have lived in the same process. That's a rather big con.

If your situation isn't such that you get pros, you don't have a trade off, you just have a situation that's worse than it could have been.

21

u/slobcat1337 Jan 30 '23

This is a really good point that people don’t often mention.

2

u/BinaryRockStar Jan 30 '23

I think about this a lot and wonder whether there is some sort of framework or library that would allow you to write modules and have your application use them, then in the future transparently pull that module out into a microservice with only a configuration change on the caller's end. In Java at least this would require separate API/implementation libraries (like Log4J2's log4j-api and log4j-core).

From memory this is how DCOM in Windows worked, the caller had no idea whether the object it had instantiated was in-process, out-of-process or on another machine across the network.

This would allow a "strangler"/incremental approach to turning a monolith into microservices so I would be surprised if something like it doesn't exist, although I haven't looked hard.

9

u/[deleted] Jan 31 '23

I imagine you could do this with any message passing / actor architecture. Definitely with Erlang/Elixir.

5

u/throwaway1847384728 Jan 31 '23

What advantage do you imagine such a library having over interface inheritance? For instance, an Authenticator interface with two implementations: LocalAuthenticator and RemoteAuthenticator.

If you are properly using dependency injection, it should simply be a matter of calling the RemoteAuthenticator constructor in main() instead of LocalAuthenticator.

2

u/BinaryRockStar Jan 31 '23

That's kind of precisely what I mean except being done at a higher level by classloading rather than changing code, as mentioned by the way Log4J2 does things.

For a concrete example there would be a Java library called my-authenticator-api providing interface Authenticator. The application uses a factory to receive an implementation of Authenticator, it doesn't know or care what the concrete type is.

There would be two libraries my-authenticator-local and my-authenticator-remote that both implement Authenticator interface so which one is called is determined by which library is loaded on the classpath. This way no code change is needed and the exact same application can make local or remote calls based on what is on the classpath.

The thing I'm missing is that now you have to maintain two Authenticator libraries- sure they would be 99% the same code just with a microservice wrapper in the "remote" one, but scale that up to 100+ microservices and it's immediately a maintenance risk. And as far as management is concerned it's wasted time as you're coding up two solutions for future flexibility that may not be required.

What I'm after is a framework that would make this seamless and automatic. It may require a Java compiler plugin or a Maven plugin or similar as I doubt it could be implemented as a library.

1

u/throwaway1847384728 Jan 31 '23

In C++ I would define a public interface authenticator.h, defining the Authenticator interface. Then, I would define authenticator_factory.h with two methods: CreateLocalAuthenticator() and CreateRemoteAuthenticator(). Both would return the Authenticator interface.

Then in the private authenticator_factory.cc file, I would define three classes: LocalAuthImpl, RemoteAuthImpl, and SharedAuthImpl. SharedAuthImpl would be a member variable if the former two with sets of common logic. C++ also allows freestanding functions for miscellaneous pieces of logic.

Finally, in main(), I would add an if/else branch that calls either CreateLocalAuthenticator() or CreateRemoteAuthenticator() depending on some config value. Then I pipe the returned Authenticator interface throughout the code.

This way, I can switch from remote and local with just a change in my config. The only boilerplate code is the if/else branch in main, all of the rest of the system only sees the interface. And free functions and the SharedAuthImpl in my .cc allows me to shared important between the two.

I think this is why Java has a stereotype for leading to unclean code. I can think of ways to implement the above in Java, just not anywhere near as clean. Take this with a huge grain of salt though because I’m obviously not a Java programmer.

→ More replies (1)

1

u/ricecake Jan 31 '23

I think what they're getting at is that you wouldn't have to write the different interfaces, nor actually implement the differences between remote and local authenticators, to use your example.

An abstracted version of how databases work. Your code doesn't change when you move the database from being on the local server to being a remote cluster, it just sees a changing connection configuration.

2

u/sheatrevor Jan 31 '23

JBoss Modules or OSGI. Take a look at Apache Karaf.

1

u/BinaryRockStar Jan 31 '23

Thanks, I'll look in that direction!

2

u/bladub Jan 31 '23

Isn't this reinventing Java rmi? It has some annoying downsides, because there are physical differences in how remote and local connections behave, so in the end you usually have to know anyway if it is remote or not (or have the overhead and failure options anyways).

At least those are part of the reasons why other approaches won out.

1

u/BinaryRockStar Jan 31 '23

Isn't this reinventing Java rmi

It may well be, thanks for the pointer

1

u/ricecake Jan 31 '23

I bet you could do it with some sort of "local RPC" setup, as oxymoronic as that sounds.

I think my biggest concern would be that it would be really easy to write something that was sane when the call was local, that would be best done differently if it was remote.

1

u/[deleted] Jan 31 '23

Maybe what we need is a common object-request broker architecture :)

1

u/BinaryRockStar Jan 31 '23

You might just be on to something :)

1

u/9034725985 Jan 31 '23

With micro services, you put a network connection between modules that could have lived in the same process. That's a rather big con.

Worse, you could end up serving your microservice as a nuget package that other teams consume which means the other team is now forced to use dotnet.

1

u/maspe1 Feb 01 '23

Perhaps this is just my niche in robotics, but this isn't true if you're using DDS. You can have multiple processes automatically use shared memory if they are on the same machine or UDP if they are on separate machines, without code changes.

3

u/bighi Jan 30 '23

Yes. But what they mean is that for small teams not only it's not needed, but it's not even something you should consider.

2

u/confusedpublic Jan 31 '23

Doesn’t have to be one extreme or the other.. actual normal sized services are a good pattern too.

2

u/TooMuchTaurine Jan 31 '23

100% agree with this. Also some things just make sense as services as they have natural and well known boundaries. A good example of this is an authentication service, or perhaps a document scanner.

But trying to do microservices in your business domain can go wrong very quickly and turn into a distributed monolith, which is very hard to refactor as you inevitably learn more about your business domain.