AWS Certified Developer - Associate
Databases
DynamoDB TTL Demo
In this article, we demonstrate how to use Time to Live (TTL) in a DynamoDB table with a real-world scenario. Consider a table that stores users' login statuses. Typically, when a user logs into an application, a session is created to indicate whether the user is logged in or logged out. These sessions are maintained for a specific duration (minutes, hours, or days) as required by the application—for example, a session that expires four hours after login.
Table Creation and Session Data
Begin by creating a table named "login sessions." In this table, each time a user logs in, a new session entry is generated with a unique ID as the partition key. There is no need for a sort key in this context, and we will disable auto scaling by setting lower provisioned capacity units for a more controlled setup.
After creating the table, insert dummy data to simulate active sessions. Each session entry should include the following details:
- A unique session ID.
- A user attribute (email, username, or user ID—in our case, "[email protected]").
- Optional additional data such as the IP address or device type from which the user logged in.
- An expiration attribute that defines when the session should terminate.
For the TTL attribute, add a property (for example, "expires") of type Number. This attribute will hold an epoch timestamp indicating when the session expires.
Once capacity is configured, create a session entry with attributes such as:
- ID: 1
- User: [email protected]
- IP Address: [the user's IP address]
- Expires: [epoch timestamp representing the session expiration time]
Remember to provide the expiration time as an epoch timestamp rather than a human-readable date. For instance, to set a one-hour expiration, use a conversion tool like EpochConverter to generate the correct timestamp.
After converting the desired future time, copy the epoch timestamp and assign it as the value of the "expires" attribute in your session entry. This ensures that the TTL property is properly set for automatic deletion once the session has expired.
Enabling TTL on the DynamoDB Table
Next, enable TTL on your "login sessions" table by following these steps:
- Navigate to the "login sessions" table in the AWS DynamoDB console.
- Go to the "Additional settings" section.
- Select the "Time to Live" option.
- Enter the TTL attribute name (e.g., "expires" or "expiresAt") that the table will use to determine when an item should expire.
- Use the preview functionality to simulate expiration. For example, if you set the expiration to one hour in the future and refresh the current timestamp, no items should be eligible for deletion at that moment.
Simulation Tip
To observe TTL in action, simulate a future time (e.g., two hours ahead). DynamoDB will indicate that items with expired TTL values are ready for deletion. Once you are satisfied with the preview, activate TTL.
Conclusion
By following the steps outlined above, you have successfully configured TTL for your DynamoDB table. This feature automates session management by automatically deleting expired items, ensuring that your table remains efficient and performant. The key steps include:
- Defining an expiration attribute using an epoch timestamp.
- Enabling TTL in the DynamoDB console settings.
- Verifying the TTL behavior with the preview functionality.
This approach guarantees that session entries are removed after the designated expiration time, keeping your database streamlined and up-to-date.
Watch Video
Watch video content