Search code examples
mysqlmicroservices

Migrating to Microservices - mysql


The company I'm with is looking into migrating to a microservice/kubernetes architecture but hitting a couple hurdles in the research, so I thought I'd pitch them here.

With a typical monolithic database, a lot of queries join from other sources/tables, which would typically be considered a different microservices responsibility (eg. Customers -> Orders).

The question I have right now is, with microservices, what is considered the best practice?

I've found a few suggested approaches:

1) Join in code. Essentially running multiple queries. eg: const user = UserService.getUserById(1); const order = OrderService.getForUser(user);

I like this approach and can clearly see where data is from, and what is required etc. However this raises the issue of extra latency. Where as before I could JOIN this query and only have a single trip to a database. I now have 2 (or more depending on required data) requests to the db.

2) Join in external service. Building out a seperate service that handles queries. - This feels a lot like having an internal api that every service is dependant on and seems like a bad practice as a single point of failure. Maybe i'm seeing this wrong.

3) Join in mysql. Have all data in the same database and query as before. This has the benefit of single trip to mysql, however breaks the microservice approach and removes the ability to have database/schema per service.


I personally feel like option 1 is the most versatile option, but I'm curious about the added latency of multiple round trips, and how you guys have dealt with that. Or perhaps there is another solution I'm not seeing.


Solution

  • I would't worry too much about the latency, as all the calls would be asynchronous.

    Microservices are all about the options, selective: scalability, robustness/antifragility, deployment, etc. You cannot make the whole system robust but you can make some of it (the important bits).

    I would focus on modelling of domain models/boundary contexts, try to get Single Responsibility Principle right, that would hopefully help you to avoid functionality replication, Death Star dependencies.

    Very basic µService Architecture

    Reading:

    Building Microservices

    Domain-Driven Design: Tackling Complexity in the Heart of Software