Search code examples
phpdatabaselaravelapilatency

multiple database connection VS. curl request in Laravel, which is faster?


we have a project with multiple subdomains and each subdomain is working seperately on different aspect of the business. for example customers.domain.com would be a completely seperate laravel project with its api where users(customers) can come and inetract or connect through api connection and it only saves data from their interactions and orders. then there is suppliers.domain.com where people who offer the products come and interact with their information and add rates and inventory for their products and see their orders. and then there is a central.domain.com which would make communication between these 2 subdomains possible and also admin can add products(suppliers can select from these products and sell them) and see orders. if admin wanted they could add a service cost to the final price that comes through supplier and then show it to the customer.

at the moment all of these sections are in the same server but in the future they might be moved to differnt servers, therefore we developed them as 3 diffferent laravel projects and the communciation between them is through API XML or JSON.

now my question is, would it be better to make multiple database connections in the laravel projects and in each project be able to directly fetch what is needed from database OR the current workflow would be better?

and by better I mean faster, because as the matter of workload, the code has already been written and is working well as api connection, so there is no extra thing needed to be done But if the multiple connection turn out to be better and faster we can change all the codes to that.

I would really appreciate your help with this matter.

PS. customers and suppliers are all from differnt part of the world, if that matters for the decision.

Thank you very much in advance.


Solution

  • Sharing databases or sharing data via APIs is a trade-off.

    If you share data via APIs you:

    • Have decreased performance because data is retrieved via http
    • Only the application providing the API is responsible for the validity of the data in it

    If you share databases you:

    • Have better performance because data can be pulled straight from the database
    • Multiple applications are responsible for making sure the data is valid

    What I mean with validity is the business rules regarding the data. For example 'A customer has a name, e-mail address and shipping address', which makes sense from a shopping cart perspective. But if you were to introduce a newsletter, a customer will probably not have any shipping data. It's best to take care of these inconsistencies in one spot, instead of several spots. That's why I'd choose data validity over performance in this case.