Real-Time Middleware System – Direct DB Access vs REST API

apidatabaseormrestweb-applications

I'm developing a complex environment which involves a web application, a mobile app and a real time middleware system which accesses the DB for on-site tag readings. Currently, the web application exposes some services over a REST API, which are accessed by the mobile app to let it perform its business work (in this case, time delay is not critical). However, for my middleware system, I access the DB directly (through a ORM), in order to provide faster responses.

Am I doing it right or should I take advantage from the REST API to access the information to be processed in the middleware system too? Actually, this system, the web application and the DB belong to the same physical server.

Any recommendation about that will be welcomed!

Best Answer

From personal experience I suspect you are approaching it in a sensible way - there would be a performance hit by using the RESTful API for 'internal' access.

However, what you might consider is building an internal linking library that takes internal requests to access the DB and convert it into a form that is essentially what the RESTful API uses internally once it has accepted/authenticated and translated the request messages.

This is a model that splits the RESTful API into two layers - the message transport and control layer facing the Internet or network, and the internal message handlers. By having a 'local' linking library that bypasses the top layer but then goes through a common layer of handlers, you keep the advantages of one code base, one monitoring and maintenance/audit system etc.