Understanding DynamoDB’s Serverless Connection Model And It’s Benefits
How DynamoDB allows you to connect to your database in a serverless fashion.
When you begin using DynamoDB, one of the first things you will notice about the database is the way you connect to it.
One of DynamoDB’s key characteristics is its connection model.
Unlike most other databases, connecting to DynamoDB involves making a call to its API using HTTP requests.
Persistent Connections
With other databases systems, you connect to them by initializing a persistent TCP connection.
The disadvantages of persistent connections are the initialization time to create the connection and the fact that maintaining that connection over time requires additional resources from the database server.
To mitigate this issue, most database systems limit the number of open connections you can make it the database.
However, this can impact the scalability of these databases.
HTTP Connection
With DynamoDB, you don’t experience any of these limitations.
You can query your database instantly without creating a connection pool.
As well, you can have an unlimited number of concurrent requests with on-demand mode or by provisioning the reads and writes accordingly.
This is one reason why DynamoDB can scale so high and offer a high number of concurrent requests; provided that it is well designed.
To learn how to design your DynamoDB table for high scale, I recommend you read this article.
Let’s now take a look at how an HTTP connection works in DynamoDB.
Connection Implementation
To interact with DynamoDB you can use the AWS SDK in a variety of languages.
I’ll demonstrate this with Node JS.
const { DynamoDBClient, PutItemCommand } = require("@aws-sdk/client-dynamodb");
The DynamoDBClient is the main client class for DynamoDB. It provides low-level methods to interact with DynamoDB directly.
This client is necessary to establish a connection with DynamoDB and perform operations like GetItem, PutItem, UpdateItem, etc.
We can use the PutItemCommand to write items to DynamoDB.
We start by initializing the DynamoDBClient:
const dynamoClient = new DynamoDBClient({ region: 'us-east-1' });
We define an item to write to our database:
const params = {
TableName: 'users',
Item: {
userID: { S: "101" },
name: { S: "John Doe" },
email: { S: "john@gmail.com" }
}
};
Now we can easily write items with the PutItemCommand, and sending that command to DynamoDB:
const command = new PutItemCommand(params);
await dynamoClient.send(command);
Note that we do not initiate any connection beforehand. We only import the DynamoDB API, and simply call http method we want.
Conclusion
DynamoDB’s unique connection model sets it apart from traditional databases by using HTTP requests instead of persistent TCP connections. This eliminates the need for connection pooling and allows virtually unlimited concurrent requests.
This efficient model contributes to DynamoDB’s exceptional scalability and performance, allowing it to support many concurrent requests.
By leveraging the AWS SDK, developers can seamlessly interact with DynamoDB, executing operations without the overhead of maintaining persistent connections.
👋 My name is Uriel Bitton and I’m committed to helping you master Serverless, Cloud Computing and AWS.
🚀 If you want to learn how to build serverless, scalable, and resilient applications, you can subscribe to my blog:
https://medium.com/@atomicsdigital/subscribe
Thanks for reading and see you in the next one!