Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

soufyaneyassin's avatar

microservices with laravel

hello everyone, i would like to discuss this particular situation when you want to use microservices architecture, imagine for example you have 3 laravel projects (each one as a service with its own database), how will you manage relationships across these databases (from one service to another) ? for example table users in one service related to another table in a different service.

0 likes
4 replies
LaryAI's avatar
Level 58

One way to manage relationships across multiple databases in a microservices architecture is to use a message broker. A message broker is a software application that acts as an intermediary between two or more applications that need to communicate with each other.

For example, if you have two services, Service A and Service B, and you need to establish a relationship between them, you can use a message broker to send messages between them. Service A can send a message to Service B with the data it needs, and Service B can respond with the data it needs. This way, the two services can communicate without having to directly access each other's databases.

Another way to manage relationships across multiple databases is to use an API gateway. An API gateway is a single point of entry for multiple services. It acts as a proxy between the client and the services, and it can be used to manage requests and responses between the services.

For example, if Service A needs to access data from Service B, it can send a request to the API gateway, which will then forward the request to Service B. Service B can then respond with the data it needs, and the API gateway can forward the response back to Service A. This way, the two services can communicate without having to directly access each other's databases.

// Example code for using a message broker

// Service A

// Send a message to Service B
messageBroker.sendMessage('ServiceB', {
    data: 'data from Service A'
});

// Service B

// Listen for messages from Service A
messageBroker.onMessage('ServiceA', (message) => {
    // Do something with the data from Service A
});
jlrdw's avatar

I don't consider the way you are using to be microservices.

I look at it like a doctor's office can view lab results from an outside lab they are set up with thus viewing of the results would be a microservice.

I admit I don't fully understand what you are trying to do, if the same users are in all the databases, my suggestion would be just use one database for the application.

soufyaneyassin's avatar

@jlrdw The application is not just a backend; it also includes a frontend built with Vue, and each functionality is separated into a service as a Laravel project using APIs and JWT for authentication. If this isn't considered microservices, then what would it be classified as? I didn't provide many details in the post, but let's consider that we have a microservices architecture that uses multiple databases (the reason why we have multiple databases, is to improve performance and security...).

jlrdw's avatar

@soufyaneyassin In a microservice each service should handle it's own data. In my opinion if you have to worry about related data between two of them, you defeated the purpose.

In my earlier example, the labs database is completely independent of any doctors office. There are lab results for John Doe.

The doctor logs into the lab (API) and can view or print out the results for John Doe.

The doctor's database is totally separate from the labs database.

Each service is it's own entity, not in any way dependent on another database.

how will you manage relationships across these databases

That is not applicable, as there is no relation. You get (retrieve) needed data from a service.

See also https://laracasts.com/discuss/channels/guides/how-to-setup-microservices-in-laravel

Please or to participate in this conversation.