S
S
Sergey Ilichev2021-03-20 04:03:37
PHP
Sergey Ilichev, 2021-03-20 04:03:37

How to correctly transfer data between services?

Hello everyone!)

Share your thoughts and experiences on how and why (more convenient to maintain) to exchange data in microservices. Here the question is not about the method of transfer, I have queues here, but the question is what to transfer, a set of objects or an array of strings?

Suppose there is a service A, and it can store and process transactions (for example, payment for books). This service has a Transaction model and, accordingly, a table in the database. And there is another service B. In this service, only transactions from module A are displayed. That is, module B turns the curl to module A, which should give it a list of transactions.

The question is how exactly to give a list of transactions -

1) An array of strings (json)
2) An array of objects

Let's say we give an array of objects. What are the benefits?
- We got an array of objects, wrote it to the data provider and immediately render it to the grid.
- We can check the validity of the required data by the type of the interface of the objects.

Of the minuses -
- You need to carry incomprehensible objects.
- If something changes in the object on the side of service A, then the code in service B may break.

The second option, we give an array of strings.
We need to somehow transform this case into models.
We kind of look at what data comes in, create a class based on them with the necessary fields and validation methods, write everything there (we make the basic load, validate method), make a service that works with these models (entity) and pull it from our controller.

That sounds good. But more code and actions.

pros
- We create our own structure in service B, which describes what this service will work with (our own models for filling and validating the incoming data). This allows you to abstract a little from service A.

Cons
- A lot of code, since in fact we have a model describing the Transaction in service A and now we need to create the same model in service B so that service B understands what it generally works with (well and in general to shove the whole thing into a data provider with pagination, filters and everything else you need).

And what is the right way to do pagination in this case? In service B, through GET parameters, accept the page number and transfer the whole thing to service A, where to make the necessary selection and return to B? Or pull all the data from A and then shove them. pagination provider? It’s probably better to transfer data for pagination to service A and take from there only what you need right away.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vladimir Korotenko, 2021-03-20
@firedragon

Create a contract. This contract describes your objects here is an example

{
  "tranzactions": [
    {
      "name": "tr2177",
      "created": 6666666666
    },
    {
      "name": "tr2178",
      "created": 6666666667
    },
    {
      "name": "tr2179",
      "created": 6666666668
    },
    {
      "name": "tr2180",
      "created": 6666666669
    },
    {
      "name": "tr2181",
      "created": 6666666670
    }
    
  ],
  "total": "500",
  "pageSize": "5"
}

Service A and B only know about the contract. They don't care what's going on inside. The task of any service is to correctly create a model based on a contract and give it or process it.

A
Anton Shamanov, 2021-03-20
@SilenceOfWinter

some kind of nonsense, in any case, if the data structure changes, the adapter / handler will also have to be changed.
accessing your own site via curl is stupid and inefficient, it's easier to redirect requests to local services/controllers through routing.
it is easier and more convenient to pass objects, see the soap implementation in php, you can not invent anything and use an established business practice.
regarding services, see the strategy design pattern - it is not the data model that changes, but the way they are displayed.

E
Eugene, 2021-03-20
@Nc_Soft

You should have 1 service that works with transaction entities (receiving, displaying, etc.).
You are doing nonsense, you probably read a smart book or watched a report about Avito microservices on YouTube ..

G
Gena, 2021-03-20
@brary

maybe in the direction of gRPC & protocol buffers see https://youtu.be/6JF2U39J4RY?t=5728 there is an interesting report on this topic

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question