3

i created an AWS lamnda 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 data base.

It's a good idea use Spring Boot Function against another language or framework ?

For example use phyton, nodejs or another solution.

  • It's not clear what do you mean by 'slow'. If you are simply referring to cold start vs warm start its a whole separate issue and has nothing to do with languages or Spring. However Spring does provide you with the way of improving it significantly via function bean registration - https://cloud.spring.io/spring-cloud-static/spring-cloud-function/3.0.6.RELEASE/reference/html/spring-cloud-function.html#_functional_bean_definitions. Also I am not sure what do you mean by "It's a good idea use Spring Boot Function against another language or framework ?" – Oleg Zhurakousky Apr 28 '20 at 02:59
  • Sorry for not being clear, well when I say it is "slow" I mean the response time. Thanks for the link, I'm going to change the way I define beans and see what happens with the response time. – Martin Delgado Apr 28 '20 at 15:07

2 Answers2

7

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.

Matthias Steinbauer
  • 1,786
  • 11
  • 24
1

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.

Vinay
  • 72
  • 3
  • Please don't confuse users by mixing personal opinions with facts especially if you're not ready to back up your opinions with any kind of data. – Oleg Zhurakousky Apr 28 '20 at 03:08
  • 4
    Sorry @OlegZhurakousky, I did not just express my personal opinion but also gave reference to AWS documentation on the same. Well, I agree that some additional data must be provided to really show the difference. Will try to do so a I get some time today. – Vinay Apr 28 '20 at 08:49
  • I mean I think maybe Spring Cloud is heavier in execution just to save an object, I have no data to compare Spring Cloud with other frameworks and say what is better. – Martin Delgado Apr 28 '20 at 15:15