Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pros and Cons of using API instead of direct DB Access

Tags:

rest

php

api

I found myself in several discussions throughout the week regarding a web application under development and whether it should leverage an API that is being created.

Here's the situation. I have a PHP MVC web application with a MySQL DB as well as several mobile apps all being developed in house. For the mobile apps we're building a rest api. The big question is why should my PHP web application now use that rest api? I've always expected the use of an API to be for third party systems that need to interface with my database or for systems built on a different technology. The web app is certainly not a third party system and the services are in PHP. If the API is on a different server than the web app then I guess it could be considered a third party system... which has not been decided yet.

To me, it just seems strange to leverage the API for the web app especially since the APIs services are going to be limited to about 50% of the functions available in the web app leaving me to build the other 50% that would be unique to the web app. I also foresee a performance hit to the web app stepping through the service layer rather than just accessing the DB directly. On the other side I see more maintenance having a code base for my web app hitting the DB and similar functions built into the api for mobile apps.

Has anyone found themselves in a similar situation and can provide some technical pros and cons to why I should just use the API or can point me to a solid case study?

like image 373
Runicode Avatar asked Apr 22 '16 02:04

Runicode


People also ask

What is the difference between an API and a database?

Application Programming Interface (API) The API is not the database or even the server, it is the code that governs the access point(s) for the server. An API is not a database. It is an access point to an app that can access a database.

Why would a company provide an API to their data as opposed to letting you use SQL to query their database?

Data is not bound to particular resources or methods, so APIs can handle many different types of calls and return different types of data. This allows you to build APIs that meet your company's and customers' needs, all likely using a variety of differing databases and resources.

Why do we use REST API why not perform SQL query directly on the central database what is preferred and why?

The main reason to prefer a RESTful API over direct SQL access is that it provides a static interface to a changing implementation - the API doesn't change if some of the data ends up being accessed through the file system or a messaging queue.

Does access need API?

Meta description: API access is required in order for two or more applications can work together. This process allows developers to save time while increasing the efficiency and productivity of their application.


1 Answers

Pros:

  • What if one day you decide to move the backend app to another machine? With an API, your app code won't need to change.
  • What if one day you grow, and need to scale to 10000 backend apps instead of 1? With an API, your app code won't need to change.
  • What if one day you decide to swap out MySQL for Mongo? With an API, your app code won't need to change.
  • ^ Enforced separation of concerns between data access layer (DB) and application

Cons:

  • More code up front when writing the app layer
  • More incremental work when you need to support a new app layer feature that your API doesn't support yet

To me, the pros clearly win.

like image 69
bcherny Avatar answered Sep 20 '22 14:09

bcherny