rspec/rules/S6243/java/rule.adoc

97 lines
3.9 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-06-01 01:19:13 +00:00
Resources that can be reused across multiple invocations of the Lambda function should be initialized at construction time. For example in the constructor of the class, or in field initializers. This way, when the same container is reused for multiple function invocations, the existing instance can be reused, along with all resources stored in its fields. It is a good practice to reuse SDK clients and database connections by initializing them at class construction time, to avoid recreating them on every lambda invocation. Failing to do so can lead to performance degradation, and when not closed properly, even out of memory errors.
2021-05-04 01:15:14 +00:00
2021-05-09 01:17:04 +00:00
This rule reports an issue when the SDK client or the database connection is initialized locally inside a Lambda function.
2021-05-04 01:15:14 +00:00
=== Noncompliant code example
2021-05-04 01:15:14 +00:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-05-04 01:15:14 +00:00
----
public class App implements RequestHandler<Object, Object> {
@Override
public Object handleRequest(final Object input, final Context context) {
S3Client s3Client = DependencyFactory.s3Client();
s3Client.listBuckets();
// ...
2021-05-04 01:15:14 +00:00
}
}
----
=== Compliant solution
2021-05-04 01:15:14 +00:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-05-04 01:15:14 +00:00
----
public class App implements RequestHandler<Object, Object> {
2021-05-04 01:15:14 +00:00
private final S3Client s3Client;
public App() {
s3Client = DependencyFactory.s3Client();
2021-05-04 01:15:14 +00:00
}
@Override
public Object handleRequest(final Object input, final Context context) {
s3Client.listBuckets();
// ...
2021-05-04 01:15:14 +00:00
}
}
----
== Resources
2021-05-04 01:15:14 +00:00
* https://aws.amazon.com/fr/blogs/developer/tuning-the-aws-java-sdk-2-x-to-reduce-startup-time/[Tuning the AWS Java SDK 2.x to reduce startup time]
* https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html[Best practices for working with AWS Lambda functions]
* https://aws.amazon.com/fr/blogs/compute/container-reuse-in-lambda/[Understanding Container Reuse in AWS Lambda]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Instantiate this [Client/Database] outside the Lambda function.
=== Highlighting
Client/Database connection creation
'''
== Comments And Links
(visible only on this page)
=== on 27 May 2021, 13:59:53 Janos Gyerik wrote:
In this sentence, I think the phrase "outside of the Lambda function" is not clear or precise enough. And I find the sentence a bit complicated:
____When initializing an SDK client or database connection outside of the Lambda function, you optimize your chances to benefit from context reuse, when the same container is reused for multiple function invocations.
____
Here's an attempt to make it more precise and clear:
____Resources that can be reused across multiple invocations of the Lambda function should be initialized at construction time. For example in the constructor of the class, or in field initializers. This way, when the same container is reused for multiple function invocations, the existing instance can be reused, along with all resources stored in its fields. It is a good practice to reuse SDK clients and database connections by initializing them at class construction time, to avoid recreating them on every lambda invocation. Failing to do so can lead to performance degradation, and when not closed properly, even out of memory errors.
____
(That last remark about performance degradation and out of memory errors come from real-life experience in SonarCloud ;))
=== on 27 May 2021, 14:09:12 Janos Gyerik wrote:
I think the title could be better. Instead of "AWS Clients and Database connections should be declared outside of the Lambda function.", how about: Reusable resources should be initialized at construction time of Lambda functions
That is, declaration would not be enough, it's the initialization that's the most important (declaration is implied), and "outside" is more specifically at construction time.
 
=== on 31 May 2021, 11:28:23 Quentin Jaquier wrote:
Sounds good to me, thanks for the suggestions [~janos.gyerik].
endif::env-github,rspecator-view[]