What is API gateway really all about? Java Brains - Brain Bytes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
an api gateway is an important concept in microservice architecture it forms an entry point for external clients or really anything that's not a part of your core microservice system by watching this video you will learn all the basic concepts of api gateway that you should know [Music] so you're a back-end developer and you've built an application with microservices your awesome online shopping application now we have a handful of micro services that discover and talk to each other using service discovery uh you have a catalog service that provides the product catalog a shopping cart microservice a search microservice a user microservice everything is great you're super happy with what you have going on in the server your application api is all ready and now you need to hand it over to a front-end dev team to write the ui client for it in html and javascript so the front-end web developer comes to you and says okay man i need to build a catalog ui page is there an api for it sure here is the catalog microservice just call it it has rest apis you know okay thanks man and he goes off to build his ui catalog page sometime later he comes to you again okay man i got the catalog info that's all good but now i also need to show the currently logged in user at the top of the page i don't see that api where's that api at oh that's a different microservice really yeah you need to use this user microservice oh okay thanks man and he goes off to show the currently logged in user on his page then he comes back again and says hey man i also need to show the number of items in the card where's that api at oh that's a completely different microservice that's the shopping cart service are you kidding me man no okay thanks man so he carefully builds his ui page and by the end of the day he's called about 10 different micro services and said are you kidding me man about 20 times and then the page is finally done and you deployed to production you have a team outing to celebrate you come back to work to handle production issues and then you realize you should actually refactor your code the user microservice should actually be split ideally into a user profile microservice and a session microservice but hang on the ui guy yeah he's using it so you walk up to him and say hey listen that user microservice you're calling yeah you need to update the api why because i'm refactoring the code here are two new microservices that you need to call now are you kidding me man what the okay so if you stop and think about what you did wrong it basically boils down to a very simple concept in computer science you need it to build an abstraction layer there's no reason for the ui guy to know all the details of each and every micro service that you have in the backend imagine standing up a simple abstraction service that acts as a facade it does nothing on its own really except that it routes the requests and responses made by anyone outside your micro server system and this abstraction layer is all that they need to be worried about you give this information to the ux guy and you're done the subtraction layer has all the apis that you want to expose to the outside world and when the request comes in it knows which actual micro service to call in that sense this abstraction service acts as a traffic controller or a router it forms a single entry point for all your micro services if you do this you're not free to split your back into as many micro services as you like or refactor them as many times as you like and you have them communicate with each other internally however you like as long as you retain the contract of this facade service everyone accessing your apis from outside don't even need to know what you're doing within this facade layer ladies and gentlemen is what's called an api gateway it's as simple as that it's a gateway it sits at the very edge of your microservice architectural diagrams and as a result it's sometimes referred to as an edge micro service when people are referring to something as an edge micro service this is what they're referring to so given a micro server system what do you need to do to introduce an api gateway into the picture first you need to identify what your external api is going to be every micro service in your system has rest apis of their own but you obviously don't want all of them to be accessible to the outside world what are the apis that you are okay with other people calling what is that contract going to look like identify that first design and craft that api then you bring in this micro service that will act as a gateway you can either write your own or use one of the available technologies there's really nothing special to this you're basically creating a bunch of apis in accordance to this public api that you've decided to expose but all it does internally is call one of your existing microservices and pass along the response this technique is also called api composition right you're composing an api out of other existing apis okay so is that it for the api gateway well it can be but here's the thing since you now have the single point of entry that all your requests need to go through there are a bunch of other things that you can do to take advantage of it for example you can add some kind of a monitoring system that measures how many requests are coming in how long they're taking and all that stuff this is great for operations and support teams you can authenticate users here you can pass security tokens like jwt you can implement security measures and prevent stuff like handling denial of service attacks prevent access to certain users and ips and a whole lot more now if you end up needing to do all this other stuff then you probably want to avoid writing your own api gateway and look at one of the existing technologies there are a bunch of options to choose from one popular option is the open source api gateway implementation from the netflix microservice stack it's called zool you download and configure zul and you run it wherever you run your microservices and there's your api gateway there are other options including hosted implementations by nginx and aws no matter which one you choose the patterns are pretty much the same as what we discussed now the obvious next question what are the disadvantages of using the api gateway pattern and how do you remediate them first obvious disadvantage you've added a network hop here so things are going to be a little bit slow period there's really nothing you can do about it since the pattern kind of requires it second disadvantage let me ask you how many of these api gateways do you need do you just create one well one could be a problem you build microservices with fault tolerance and redundancy in mind so that even if some of these instances were to go down the system will still function what if your one api gateway goes down since it's a single entry point your whole system goes down as a result that's not good so yeah you can create multiple api gateways and split your incoming calls to them using stuff like load balancers and elastic ips the other problem with api gateway is that it can technically get a little too complicated so let's say you have a web client for your micro services and you have an ios client team and an android team they might need different apis in different gateway configurations in that case rather than over complicate the single api gateway you can create multiple types of api gateways one for each client type and you can have those clients call the right api gateway or configure load balancers that route requests to the right api gateway some people call this pattern the back end for front end you're creating separate back-end endpoints for the front end that's calling it so that's the api gateway in a nutshell to wrap up our story what you need to do is simple go to your front end developer and tell him to use your api gateway as the one end point that he needs to call listen to a couple of are you kidding me from him and then once he refactors all his code to call your gateway you are now free to do whatever you like in your microservice architecture as long as you retain this external api contract problem solved and thanks for watching [Music] you
Info
Channel: Java Brains
Views: 331,872
Rating: undefined out of 5
Keywords: java, java brains, tutorial, brains, koushik, kaushik, brainbytes, explained, programming, koushik kothagal, beginner, microservice, api gateway, microservice architecture, monolith, microservices, architecture, java tutorial, microservice tutorial, brain bytes
Id: 1vjOv_f9L8I
Channel Id: undefined
Length: 8min 55sec (535 seconds)
Published: Fri Aug 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.