Database – Why do people do REST API’s instead of DBAL’s

databaserestweb-apiweb-applications

At the past two companies, I've been at, REST API's exist for querying data via webapp – i.e. instead of having the webapp do SQL directly it calls a REST API and that does the SQL and returns the result.

My question is – why is this done?

If it was going to be exposed to third-parties I could understand. Better to expose a limited REST API than the full DB. But in both of these companies that's not the the case.

It's been suggested to me that these REST API's make it easier to switch between DBMS. But isn't that the point of a database abstraction layer (DBAL)? Maybe you use an ORM as your DBAL or maybe you could just write raw SQL and have your DBAL translate the DB specific stuff if appropriate (e.g. translate LIMIT for MySQL to TOP for MSSQL).

Either way it seems unnecessary to me. And I think it makes diagnosing issues more difficult as well. If a report on the webapp is giving the wrong numbers you can't just dump out the SQL query – you have to dump the REST URL and then go into the project that's serving as the REST API and pull out the SQL from that. So it's an extra layer of indirection that slows down the diagnostic process.

Best Answer

If you allow a client to access the database directly - which they would do, even with a database abstraction layer, then:

  • You get a coupling between their code and yours - particularly, there is a very strong coupling between your database structure and their code;
  • Your client may do some pretty undesirable stuff on your database - whether it be updating data that they should not, writing a query that takes too much time, deadlocking something because they do not acquire locks cleanly...
  • If you have made some less than optimal choice in your database structure, then moving out of that choice may be very hard, especially if you do not have a good way to make your clients migrate over to new structures.

That is, I am not touching at all on the REST part - isolating your database behind an API is simply a more sensible choice if the team that maintains the database and the teams that use it are not in sync, as it allows these parts to evolve at their own pace.

Related Topic