Should instances of a horizontally scaled microser

2020-06-23 08:46发布

问题:

Given a microservice that owns a relational database and needs to scale horizontally, I see two approaches to provisioning of the database server:

  • provide each instance of the service with it's own DB server instance with a coupled process lifecycle

OR

  • have the instances connect to a shared (by identical instances of the same service) independent db server or cluster

With an event driven architecture and the former approach, each instance of the microservice would need to process each event and take the appropriate action to mutate its own isolated state. This seems inefficient.

Taking the latter approach, only one instance has to process the event to achieve the same effect but as a mutation of the shared state. One must ensure each event is processed by only one instance of the given microservice (is this trivial?) to avoid conflict.

Is there consensus on preferred approach here? What lessons has your experience taught you on this?

回答1:

I would go with the first approach, a service local DB. Each instance has its own DB instance. This enables to change the persistence layer between versions of the service. Changing the ER model otherwise would lead to conflicts. You would also be able to change to a NoSQL solution with this approach easily.

With the event driven design, I can recommend this book: Designing Event Driven Systems

As I see it, a service receives an request that leads to an Event. This Event is consumed by the other instances of the service, therefore the request doesn't need to be processed again, but the result has to be copied to the instances state.