Should Front-end make calls to Database directly or through a micro-service?

Viewed 1189

I have a frontend written in JSP and the database in DyanmoDB. Write now I have two options to create an application.

1.Either to make a call directly from JSP to DynamoDB.

2.Call a MicroService from JSP and then call the DB from that service.

I have to fetch data from the DynamoDB, not write data into it.

Which approach would be better and recommended for this and why?

3 Answers

The answer depends on your application, but in general, you should prefer to have some service layer between the front-end (the JSP pages) and the database. This allows for the database implementation details (such as what database is used and the actual queries used to retrieve and modify data) to change without changing the JSP pages.

The user interface (your JSP pages) should be concerned with displaying information and should be insulated from the details of the database. For example, in the ideal situation, if you decide to change the database from DynamoDB to say MongoDB, the JSP pages should not change. Instead, the portion of the service layer that directly interacts with the database should change, but the portion of the service layer that interfaces with the JSP pages should remain the same.

There is one caveat to this approach: You should not introduce indirection for the sake of it. The overall goal of software is to solve a problem as effectively as possible. If this is a small application or a prototype, then introducing a service layer may be more than is needed. On the other hand, if this is an enterprise-level application, introducing a service layer may save you and your company a great deal of time in the future and make for a much more extensible architecture. The goal should be to solve the problem at hand as effectively as possible, not apply a pattern or indirection because that's what everyone does.

It's the responsibility of the model to fetch/update the database. To be mvc compliant, you should go through your jsp servlet to update the database and invoke the right microservice. Cheers, Hichem

Introducing a service layer is required when you plan reuse the functionality between several enterprise-level applications. Otherwise you can integrate the services directly in your application as a class library and refactor it in the future if need.

Related