Serverless using Java

My colleague were just discussing about the new architecture serverless most of the time and out of curiosity I fiddle around with this term and learned what this really is? Then I found out that it is just a way to execute your normal functions and from URL and you do not need  your server to be online all the time which results in super cost effective unlike normal server where you pay for every month/hour/second of its availability. The term looks super easy to understand. Execute my function foo() when https://mydomain.com/foo is called via HTTP protocol. The term serverless without any server is just for the developers but beneath this architecture there will still be a fully managed server who handles your request and warms up your code (jar file). So I came to think of demonstrating how to run your minimal functions as serverless . But before that lets learn some basics via image.

 

Serverless diagram

Java Serverless diagram

Long thing in short : If any POST request in /foo URI happen the Function handler will call to the appropriate function of a class which is predefined in serverless.yml. We developers do not have to worry about all those API endpoints handling because these are handled by serverless itself, this is the beauty of serverless. In an Amazon Web Service world serverless is handled from AWS Lambda service.

Now it is the time to catchup and write our very first and simple serverless project.

Requirements

 

Create a new serverless maven  project

 

Edit some codes inside Handler.java

You can directly override RequestHandler‘s handleRequest()method/function or you can create just simple method which is also supported for serverless function execution. The only difference is you have to use fully qualified Classname as well as the method name while mapping it to the URI in serverless.yml

 

Now there is one final piece of puzzle which is serverless.yml which will do the mappings of all functions to uri .

And it is just simple is that. You are ready for deploying your first serverless using simple Java class which will handle your HTTP request directly.  You can deploy your codes directly using your serverless command. But make sure you have proper AWS credentials and policy to deploy your code from commandline.

Once you deploy it the verbose log will look something like this.

Now it is time to do small testing from Postman .  As provided in the url : https://0manjxyoi0.execute-api.us-east-1.amazonaws.com/dev/foo [POST]

 

AWS Lambda Serverless Endpoing test

AWS Lambda Serverless Endpoing test

 

It is quiet so easy isn’t it ? Just code what you want and deploy it. But there are some limitation of the serverless. It is not suitable for all kind of application and it definitely does not supersede the normal server deployment because of its nature and limits.

1. Maximum execution duration per request

We just cannot use request for long task like more than 5 minutes. AWS has a hard limit of 5 minutes for every request execution.

 

2. Memory resources limit

There are some limits of memory to be used by every single function . It is like 3008M in aws lambda as of now (2018-09-11).

 

3. Coldstart

Your function will actually sleep after unused for some minutes. When you request after the sleep time it need to warm up the server again which takes some seconds and it might not be good for those who wants to get the request instantly (realtime). There are some way which will keep your server warm all time described How to keep desired amount of AWS Lambda function containers warm

 

4. Payload size limit

You might not want to send a large data from your http request because there is a limit for request payload as well . As per AWS there is 6MB limit for payload however you can use event driven aws lambda execution for avoid sending payload directly to lambda.

 

5. Ephemeral disk capacity

You just cannot use your disk space more than 512 mb which is /tmp directory of server while you do any data processing or any process which involves usage of hard disk space.

 

After talking about the limitations I would still prefer this serverless for some of my application like generating the pdf invoices for customer, doing scheduled tasks and others. In next blog post I would talk about how to run serverless locally and we can discuss about the overall complication and solutions.

Stay cool , be happy and code  😉

Thanks

Leave a Reply