Should multithreading be used in microservices?
Asked Answered
L

4

6

Should parallel programming be used in the development of microservices in case the microservices are scalable and, for instance, deployed as ECS on AWS?

If yes, what are the benefits of consuming more resources by one instance vs the same resources by N instances?

How does parallel programming match https://12factor.net/

P.S. to be more specific - should I conceptually use parallel streams rather than simple streams?

Loram answered 21/5, 2019 at 18:44 Comment(3)
This is a very vague question, but it makes more sense to think of tasks, as services are usually already multi-threaded using thread pools to handle multiple requests simultaneously. So the better question is, should a specific task use multiple threads? That depends on the task.Weekday
Two separate JVMs usually consume more resources that one JVM which has thread pool with 2 threads to process some tasks because there are JVM internal classes and some objects that could be shared among threads (like caches, database connection pools, etc)Kiangsi
All of that very much depends on context. Workload, "hardware specs", what not. should I conceptually use parallel streams rather than simple streams? ... err: measure. Dont make assumption. Make experiments. Locally, and in your target environment. A parallel stream comes with a lot of overhead. AFAIK, you shouldnt go parallel unless you talk way more than 1K of elements in that stream. Otherwise the overhead costs more than you can possibly gain.Caesarean
K
5

Basically the link that you provided also provides answer to your question already

This does not exclude individual processes from handling their own internal multiplexing, via threads inside the runtime VM, or the async/evented model found in tools such as EventMachine, Twisted, or Node.js. But an individual VM can only grow so large (vertical scale), so the application must also be able to span multiple processes running on multiple physical machines.

https://12factor.net/concurrency

Kiangsi answered 21/5, 2019 at 19:16 Comment(0)
P
4

Sure, imagine a microservice that needs to execute multiple independent calls to a dB or to other microservice and aggregate the results. As the calls are independent, they can be executed in parallel so that the total time is at most the time it takes to execute the slowest call.

Plankton answered 21/5, 2019 at 19:34 Comment(0)
J
0

Parallel streams must be used when the tasks at hand are mutually exclusive and can be done in parallel. However, parallel programming comes with an overhead of using a little more resources. So depending on the tasks at hand, you need to make a decision with trade-offs, which would be the best for you.

Jehias answered 15/4, 2022 at 6:38 Comment(0)
L
0

This comes a few years late but the answer is yes and it is actually used more often than it meets the eye. Microservices in the Java world are mainly written in frameworks like Spring and there the framework itself relies on multithreading. What may confuse you is that Microservices should be stateless to be scalable but that does not stop us from using threads. For instance, sending requests to another Microservice is generally always done on the other thread( not necessary for each request). Reactive programming is an example that has been commonly used in microservices.

Lefty answered 3/8, 2023 at 19:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.