Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It's a good idea use aws lambda with java and Spring Cloud Functions against other language?

I created an AWS Lambda with Spring Cloud Function, but I think that this approach is slow when execute the lambda.

My lambda is very simple it only save a object into a database.

Is it a good idea use Spring Boot Function against another language or framework?

For example use python, nodejs or another solution.

like image 709
Martin Delgado Avatar asked Apr 09 '26 11:04

Martin Delgado


2 Answers

This is a question which is very difficult to answer in the short.

Cold Starts

The downsides of Spring Cloud Functions and the Spring framework are, that its inherent mechanism of dependency injection can quickly take quite some time during startup time. There are certain popular parts of the Spring framework which just naturally take a bit of time to launch: database connections, messaging on a MQ etc.

This together with the fact that a JVM typically takes a bit more time to start compared to more lightweight environments, one would say Java is not the best option if you need or want to avoid cold starts of a few seconds. In my experience the cold starts usually hit you the hardest when you start getting invested in Lambda, because then you typically have small non-continuous workloads there such that your functions get evicted from memory on a regular basis, resulting in a situation where your users will also experience cold starts on a regular basis.

In my experience I would recommend to use a more lightweight runtime environment for functions that directly interface with uses. Meaning that basically all your code that reacts to HTTP calls should be in a technology like JavaScript/TypeScript/NodsJS or let's say Python.

So in the case you describe above, most definitely yes. Try to use some other technology but Java for that.

My lambda is very simple it only save a object into a data base.

I do have numerous functions with similar functionality in my codebase, these will usually cold-start in sub seconds time and will take below than 100ms in warmed up state.

Architecture

In general one can say once serverless applications grow frameworks like Spring Cloud Functions are still a very important tool in ones tool-belt. Typically once you got your feet wet in serverless and Lambda you will figure out that it is really easy and useful to use managed services on top of AWS: databases, message queues, e-mail, S3 object storage, etc.

It then becomes very obvious that an application is able to be event driven and actually raises a lot of other events but those triggered directly by the user through HTTP calls. For instance in one of my applications users can do the following thing:

  • Upload files and trigger a virus scan on these
  • Push a message into a message queue and generate a PDF report

For the above mentioned uses cases I already had code in Java. More specifically in Spring. So it totally makes sense to just reuse this code in a Spring Cloud Function but to decouple these longer running tasks from the users HTTP requests. They can run asynchronously in the background and suddenly the cold start scenario is not that important anymore.

A report that itself takes 15 seconds to generate, for the individual user is really not show-stopper, if from time to time it takes 18 seconds.

like image 156
Matthias Steinbauer Avatar answered Apr 11 '26 04:04

Matthias Steinbauer


Please refer to best practices documents by Amazon. https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html

Minimize the complexity of your dependencies. Prefer simpler frameworks that load quickly on execution context startup. For example, prefer simpler Java dependency injection (IoC) frameworks like Dagger or Guice, over more complex ones like Spring Framework.

Although Spring solves a lot of developer headache, it brings a lot of baggage with it and eats up Lambda execution time.

like image 45
Vinay Avatar answered Apr 11 '26 03:04

Vinay