Con el ruido de las cosas al caer

Habito los espacios de la casa que llenaste de libros, discos, cuadros, imágenes, electrodomésticos y recorridos. Habito los espacios de la casa con mis pensamientos y mi presencia. No sólo estoy…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




API Design for Microservices

Api design plays great role in building applications. If you don’t design it well, you will have to face cons in future. As microservices architecture allows big application to divide into many small, autonomous, loosely coupled services. Micro service works as a pluggable component which we can modify anytime without affecting other services in microservice architecture. While working on microservices architecture, we write many services. If we don’t define it well, it creates lot of problems for other consumer services/api user. If you don’t design proper response, consumer services wont be knowing whats going on behind the service call. We always write at least 3 layer for any given api ex. Controller, service, dao. We should always try to get clear separation in terms of responsibility here.

There are considerable problems around this practice. Lets say you are writing one more api for customer so you would end up writing many similar endpoints using given practice. So at the end your application will have may endpoints. It would be difficult to maintains these. You would end up designing multiple endpoints for the same resource ex. employee, customer etc. which is not required at all. Api end points should not contains any verbs, actions. We should have only noun(resource with plural form) in endpoint. We don’t need to have action in endpoint, there are various https methods available(GET, POST, DELETE, PUT) which works for you here. Now if we follow this practice, we should be having endpoints like as follows:

2. Exception Handling : This is where we need to be very careful as it gives lot of information to consumer about your api when it is not working as expected. We should always try to come up with minimal required status codes, messages while working on writing new api. Lets assumer we are writing an api which process payment for given customer order

3. Controller : Controller is the place where you get request first, We should always have dto classes designed for controller. Never mix your domain classes(entity/document ) with controller as we always expect these two to behave in different manner. Let take an example

4. Service : Service is the place where your api business logic needs to be written.

6. Unit testing : Unit testing helps you in testing your code independently. if your code methods looks very complex or contains many lines of codes it is always difficult to test it. Try breaking into multiple small methods(discussed in service part). Unit testing help you a lot when you writing big modules with lots of configuration, and many conditions around it. It saves lot of time as you don’t need to redeploy your code every time when you make small code changes. You can just verify it through test case. It also helps you in duplicating application issues, you just need to get configuration around given issue in your test case, and you can easily fix it.

The API is an interface, which allows many developers to interact with the data. We should always focus on designing great api as it helps a lot in terms of using, and understanding it.

Add a comment

Related posts:

LANDBRIDGE

Landmass over which containers offloaded from ships at one or more ports at its either coast are transported by rail or truck to the ports at the other coast for onward journey on ships…

How I Overcame Paralyzing Fear of Public Speaking

A couple of years ago I was asked to present at a women’s business conference. I was terrified of public speaking and I stressed for several weeks leading up to the event. You see earlier that year I…

Choose The Best Monetization Method For Your iOS Application

As an indie developer, I’ve seen firsthand the evolution of app monetization strategies. Monetization is the process of generating revenue from your app, and it’s a critical aspect of mobile app…