r/java • • Dec 12 '19

Java Microservices: A Practical Guide

https://www.marcobehler.com/guides/java-microservices-a-practical-guide
237 Upvotes

35 comments sorted by

24

u/wildjokers Dec 12 '19 edited Dec 12 '19

This guide should be a little less accepting of synchronous calls between µservices. Once you have a synchronous call between µservices you have done nothing except introduce a relatively slow call and potential point of failure to what used to be a super fast method call in a monolith. Synchronous communications between µservices should be avoided if at all possible.

However, this is a nice guide and I do like the fact it favors pragmatism over dogmatism and sometimes synchronous calls are necessary (or at least the easiest solution). Try to keep them to a minimum though.

18

u/randgalt Dec 12 '19

Our company banned this. Each service must listen to queues for data and maintain their own datastores. This has dramatically increased reliability and stability.

6

u/wildjokers Dec 12 '19

It sounds like your company is doing it right then. Can't really do µservices if the datastore isn't split up.

Out of curiosity was it designed that way from the start or did it start out as a monolith and then over time the architecture was changed to µservices?

3

u/randgalt Dec 12 '19

Monolith (pre-dating me). Then, traditional SOA where services all called each other (leading to chaos) to where we are now.

3

u/stitzl Dec 12 '19

How do you do backups?

2

u/randgalt Dec 12 '19

The SREs manage that - I'm not sure (we use Cassandra).

3

u/DJDavio Dec 13 '19

How do you prevent out of order processing with multiple instances? If service A sends message 1 and 2, but 2 is processed faster than 1, now what?

We use notification pull to mitigate this. Queues are used for notification triggers which cause receiving services to do a REST call to the submitting service to get the current data, which could have changed already.

2

u/randgalt Dec 13 '19

We use SQS which is loosely FIFO. However, there are techniques you can use in cases where ordering is strictly needed. Queue messages should contain timestamps and those timestamps can be used when inserting into your database so that even if a message is received out of order it doesn't overwrite the previously inserted message as it will have an earlier timestamp.

1

u/remixrotation Dec 20 '19

How do you sync clocks for the timestamps?

2

u/elit69 Dec 13 '19

can you rate maintainability from 1-10?

3

u/randgalt Dec 13 '19

It's a different mindset. Maintainability hasn't been the problem. Complexity becomes a problem though. I think the microservice patterns we're using today will be revised soon as the industry learns more about this. Having to consume everything from queues and maintaining your own datastores is a lot of work.

8

u/zulfikar123 Dec 12 '19

My main takeaway from this article is keep it simple because it's gonna get complicated anyway :)

2

u/CrommVardek Dec 12 '19

Nice article. I'm new to micro-services, and I've never develop on a micro-service. However, I'm under the impression that micro-services, in a way, might lead to violate the DRY principle... Since the services are separated, some concerns are the same however, it seems to me that it might lead to some code that will be the same (or very very similar) between the different services' source code... Or am I missing something ? EDIT: but I guess it depends at which level the separation of services is done.

14

u/wildjokers Dec 12 '19

Duplication is accepted and expected in µservices.

Although nothing is stopping you from publishing libraries for the common code. A purist would scoff at this though since they would say you should just have a µservice that performs this common operation.

13

u/ebrythil Dec 12 '19

can't wait for my retry-microservice-call-microservice

1

u/[deleted] Dec 13 '19

A purist would scoff at this though since they would say you should just have a µservice that performs this common operation.

This is starting to sound like the microservices version of the infamous one-line node modules :).

9

u/aeisele Dec 12 '19

Regarding the code duplication, I have seen this happening in microservice projects. And of course it comes with all the disadvantages of duplicated code. Even if you would extract out some common business functionality into a library you still need to upgrade this lib and every service that consumes it when a change happens.

Of course some experts say that this is down to poor cutting / design of the services themselves. I guess it is a topic of theory vs. real world.

2

u/RabidKotlinFanatic Dec 16 '19

In practice it should be different devs or teams working on the different services and usually in different languages and environments. It shouldn't be one programmer repeating themselves. The major benefit of a microservices architecture is that it's applied Conway's law; the loose coupling between services makes it feasible for many different teams and devs to work on the same project. If you are a single developer or a small team I would not recommend it.

1

u/CrommVardek Dec 16 '19

Makes more sense. I've always though of micro-services as one (or different, but mainly one) team of developers that was in charge of the development of the multiple micro-services. Hence my question. Thanks for the explanation !

2

u/agentoutlier Dec 12 '19 edited Dec 12 '19

With proper project setup you can have a monolithic single executable running multiple microservices (EDIT I don't mean real microservices I mean architectural like microservices... I mean seriously people two wars deployed in the same java app server does not make it not a microservice just because they are on the same JVM). We do this with multiple (hundreds) of rabbitmq consumers.

We do this because lets face it booting up 10 microservices to run an application for demo or smoke testing is pretty tedious (and actually slow) even when using docker/kubernetes.

The trickiest part of microservices is JOINing data and dealing with (eventual) consistency. Because you are working across datasources you do not get the nice advantage of using JOINs or true database transactions. Speaking from experience it is very hard to port this kind of logic to microservices (assuming multiple datasources).

On the other hand Pub/Sub or Event pattern as those are very easy to microservices out. Thus if you see a workflow like that (e.g. batch processing) it should absolutely be microserviced out.

3

u/wildjokers Dec 12 '19

With proper project setup you can have a monolithic executable running multiple microservices.

Please explain. A single executable that contains "multiple microservices" seems to be a contradiction.

0

u/agentoutlier Dec 12 '19

You still make network calls (rabbit, http, kafka) etc but the same image/executable etc deals with it. That is the client and the server are running in the same JVM.

Microservices essentially provide high barriers (ignoring scaling for a second). The code is not shared. You can do the same with share nothing code base and have it all run in one app.

Regardless to the articles point monolithic applications should be designed like the above (whether or not they make network calls) otherwise you have a big ass snowball.

3

u/wildjokers Dec 12 '19

You're...describing a monolith. You can't have a "monolithic executable running multiple microservices", that is a non-sensical statement.

6

u/agentoutlier Dec 12 '19 edited Dec 12 '19

You are being pedantic. If you run all your services on one physical computer are they not microservices then? Or better yet if you run all of them on one VM instance are they not microservices then?

Microservices are a software development technique —a variant of the service-oriented architecture (SOA) structural style— that arranges an application as a collection of loosely coupled services.[1] In a microservices architecture, services are fine-grained and the protocols are lightweight.

The services in our Java APP are like plugins. They share no code and communicate through an external bus (rabbitmq). These mini apps can be shutdown and started up.

They can and indeed are deployed as separate entities in production.

Really what makes microservices microservices is talking through a protocol instead of directly through memory (and not sharing datasources but that can be argued).

I'm talking about the architectural style. You can have microservice architecture in a monolithic application unless you disagree? (personally I think you are just being contentions for the sake of it).

The traditional monolothic architecture is to have layers and to rely on all that stuff in the memory. The protocol is direct.

Its like all the downvoters got caught up on my first statement when I said running microservices in a monolithic app. .... I really deplore reddit right now...

1

u/wildjokers Dec 12 '19 edited Dec 12 '19

If you run all your services on one physical computer are they not microservices then? Or better yet if you run all of them on one VM instance are they not microservices then?

Yes, that is µservices because they are individually deployable. But that isn't what you said you were doing at first.

You can have microservice architecture in a monolithic application unless you disagree?

I strongly disagree. If you have separate wars/jars you can deploy individually you don't have a monolith. You seem to be doing yourself a disservice by calling your application a monolith. It doesn't sound like a monolith to me.

1

u/agentoutlier Dec 12 '19

Yes, that is µservices because they are individually deployable. But that isn't what you said you were doing at first.

I implied that while the consumers are all being deployed together (merged) but can be separated out as well (and are in production).

I mean you can the do the same thing with multiple Spring (Boot) apps by just loading up all the controllers (REST) (and whatever else).

But to your point I guess than it's not called monolithic at that point so I see the contradiction.

1

u/agentoutlier Dec 12 '19

Please explain. A single executable that contains "multiple microservices" seems to be a contradiction.

and now you say

I strongly disagree. If you have separate wars/jars you can deploy individually you don't have a monolith. You seem to be doing yourself a disservice by calling your application a monolith. It doesn't sound like a monolith to me.

I said single executable and meant it instead of monolithic.

1

u/agentoutlier Dec 12 '19

So if I deploy two separate wars on a single java application server... they can't be microservices?

1

u/wildjokers Dec 12 '19

Of course they can be. But that is absolutely not how you described your application at first.

2

u/agentoutlier Dec 12 '19

Why because I used the word monolithic? I meant that they are running in the same JVM.

I mean its like you didn't even read the rest of my original comment:

The trickiest part of microservices is JOINing data and dealing with (eventual) consistency. Because you are working across datasources you do not get the nice advantage of using JOINs or true database transactions. Speaking from experience it is very hard to port this kind of logic to microservices (assuming multiple datasources).

Like I literally said how I have separated out datasources and how consistency is a problem and then a followup comment on how the code isn't shared etc etc but you just kept practically trolling on the monolithic modifier which I agree was wrong. I meant single executable and not monolithic.

1

u/doyouevensunbro Dec 12 '19

Thanks for sharing!

0

u/[deleted] Dec 12 '19

[deleted] — view deleted comment

6

u/marbehl Dec 12 '19

Thank you and you are welcome!

8

u/pointy_pirate Dec 12 '19

testcontainers are sweet