Making APIs faster using serverless
How does your application communicate with the backend? Probably, it will talk through an API. It will support reading and writing the data source of choice. REST API protocol seems to be quite a standard way of resolving it. It supports verbs like POST, GET, PUT, and some others. However, there are edge cases when your response grows into hundreds of kilobytes, and one will need to filter it. The lags will appear on the front-end side, which will lower the overall user experience on the website.
Let's consider a use case. Quite often, one needs to get the users' information. The entity will have a name, email, phone, and home address, even in a very simplistic form. This example is not considering connections to anything else. Imagine that you need to get just the emails and names for the next marketing campaign. There are two possible solutions: creating a filter on the front-end to get the required fields or making a new endpoint that will do the trick.
Additionally, one could use - GraphQL. It is an upgraded version of REST APIs where you either query or mutate (changing) data. It also has the schema, which becomes a contract for all the clients that request information. The front-end decided in one call in which shape the data would come. In the example of a marketing campaign, the client will ask for the name and email from the 'User' object. Developers will not need to write any additional filters or endpoints.
How could one host the GraphQL server for the application in a serverless way where it pays only for usage without managing infrastructure? There is an AWS AppSync for this job. It handles the provisioning, authentication, and connections to the correct endpoints. AWS Lambda function will be a compute service. Working with databases is also great because one could call a fast NoSQL DynamoDB database directly without writing a code for this request.
Recommended by LinkedIn
We are using it on the project currently. I am impressed by how fast one could set up custom authorization. We need to call a third-party cloud to get the authentication token. Also, it is cached with no setup for 5 minutes. During this period, the request goes directly to the business logic layer, where it will know the information from the authorizer function.
All in all, the cases of APIs could be different. If the API should be public for externals - not so many clients have used GraphQL yet. However, using AppSync for internal backends could significantly make the front-end faster and more straightforward for developers to operate.
Thanks for reading! Subscribe, share and give feedback - these three components motivate me to post more inspirational content.