AWS Lambda with Java : Starting the journey

 The majority of courses out there cater to a teaching Lambda (or Serverless in general)  in combination with some scripting language like python or javascript based. Although this has its own advantages,  Java developers find little solace seeing their favourite language being left out. This series is an attempt to understand the AWS lambda model in combination with java and the amazon sdk for java. I personally enjoy the java lambda combination


A love-hate relationship


Before divulging into the details let us have a look at what are the advantages and disadvantages of using java for Lambda functions. After all we are going against opinion, so better be prepared with the facts.


Fact 1:


JVM based applications have higher start up times which has an impact on event based firing of lambda functions. This has a major impact on cold start time of lambda events. There are a couple of ways around it like provisioned concurrency with its own downsides with cost or reducing what the code does at start up or using frameworks like Micronaut (if at all required) which helps the start up time.


Fact 2:


Java is verbose in nature, compared to other languages out there. Accept it. But what is lost in verbosity, is gained in having a solid structure of application. The scripting languages are great in having “glue” scripts within your application combining different moving parts, but as part of stand alone full fledged applications, java is a good choice.


Fact 3:


When performance is crucial, I.e a higher throughput is required then Java is the clear winner. Compared to any interpreted language java has a higher throughput. Large scalable applications are where java makes an impact.


Fact 4:


There is tonnes of lines of Java code and developers out there. Many refactoring and modernising of applications are happening in which there is scope for reusing components and logic. No point in reinventing the wheel in such cases.


Fact 5:


Cold start times are higher when memory allocated is lower. As memory increases the cpu allocated also increases. This is essential for reducing start up times. This does have a negative impact on the cost of lambda functions. The memory required for java is typically higher (some estimates around 1700 MB) for smooth operating of lambda



Although from above scenarios Java is not a winner, it is not a looser either. And interestingly for all the java hate out there, Amazon itself distributes its own java version and also uses java in their services extensively. 


Lambda Lifecyle:


Each Lambda invocation has a pre defined steps to setup the runtime environment and acts as a mini VM. Lets have a look at that




Any event or request that is supposed to fire up the lambda method, when occurs invokes the lambda service, which in turn creates a linux based VM (mini).

The VM initiates a language runtime model (JVM in our case). Our java code needs a app server to run. This is the Amazon Java server which is initiated by the JVM and it acts like any traditional app server (tomcat). This serves invokes our handler method, which is the custom code.


This is the behind the scenes of firing up a java based lambda function.


Some Rules for Java Based Lambda functions


  1. Lambda method (handler method) should be public. Could be both static or instance in nature
  2. Class containing handler method cannot be abstract, must have default or no-args constructor.
  3. Constructor helps in caching data between lambda cals
  4. AWS provides RequestHandlerInterface out of the box to override some methods and take care of boiler plate code but it is not mandatory to use it in the implementation.
  5. The Context object can be passed as a parameter to the handler method. This contains information about current lambda invocation.


In the next part we will have hands on part by creating a java application


Comments

Post a Comment

Popular posts from this blog

AWS Lambda, Kinesis and Java : The Producer
 (Part two of a three part blog.)

AWS Lambda, Kinesis and Java : Streams (Part one of a three part blog.)