Reputation: 1
I'm creating a web application that contains two servers, php and node. PHP server will handle API calls and Node server will handle socket. And, both of these servers need to read and write to database -- MySQL. I wonder if it's a good idea to have these servers access database directly? Or, should I make a service in the middle and have these two servers talk to the service when they need to access to the database. Other suggestions are welcome.
Upvotes: 0
Views: 421
Reputation: 56
Generally, your second approach is the right way to go; if you are querying the same MySQL database for the same application stack, then create a service layer that handles this task. You can thus manage all of your connection details and modeling in one place. Ease of maintenance will improve your quality of life, and subsequent app developers praise your name.
This application design approach is recommended, even if future requirements have you connecting to any number of data sources. You want to maintain this sort of thing in one place, as much as possible. This does not mean, however, that your entire service layer should reside in one place. A composite service is pretty normal; various services can reside in different stack elements, and an api routing utility, like Umbrella (http://apiumbrella.io/) can handle all of the client service requests, so those requests can be location-agnostic. Given that, similar service tasks, like db transactions, should be handled in one place for the reasons already stated: make your life easier by not duplicating effort.
Upvotes: 1