pom.xml
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.256</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
</dependency>
Client Code
public static AmazonDynamoDB getDynamoDBClient() {
return AmazonDynamoDBClientBuilder.standard().withRegion("ap-southeast-1").build();
}
Now i am trying to execute a normal query having few records but it is taking long time to fetch the result.
For first time it is fetching the records in around 5-6 seconds on multiple requests in reduces by half. 2-3 seconds is still large time for fetching only few items.
Already checked the tuning of dynamo DB using different client configurations (connection timeout, request timeout, retry etc.) but not giving results as expected.
Also checked with SDK version 2 URLConnectionHTTPClient config but same results came there too.
One possible cause can be the credentials fetch time for dynamo DB client but not having any credentials caching reference in java. Can any one suggest possible configuration to improve this latency.
2
Answers
You are using a very old API and is not best practice anymore. To use best practice with Java, use the AWS SDK for Java v2.
The AWS SDK for Java 2.x is a major rewrite of the version 1.x code base. It’s built on top of Java 8+ and adds several frequently requested features. These include support for non-blocking I/O and the ability to plug in a different HTTP implementation at run time.
The POM for v2 is:
To retrieve records using the AWS SDK for Java v2, you have three choices.
I will show you all ways. All examples can be found in the Amazon Github repo.
Use the DynamoDbClient
Code is:
Enhanced Client
Code is:
PartiQL
Code is
All of these ways are recommend using over AWS SDK for Java V1. If you are not familiar with the V2 API – including creds and setting up your dev environment, see:
Developer guide – AWS SDK for Java 2.x
This is expected even with dynamo at production because first time you fetch the following sequence of operations happens:-
As you see there is caching in step 2, so the next time requests go, the partition id is fetched from the cache thus the latency decreases. (will share the source once I have it)
Also, please don’t use local dynamo for performance benchmarking, because it internally uses SQLite, i.e. SQL DB for storing.
refer:- https://www.dbi-services.com/blog/aws-dynamodb-local/#:~:text=Yes%2C%20this%20NoSQL%20database%20is,Local%20engine%2C%20embedded%20in%20Java.