Skip to content

Key-value database with automatic time-to-live for user session management

0

I'm maintaining a legacy web application which uses a homegrown solution to maintain HTTP sessions.

Session data is currently stored in a relational database table, composed of the following fields:

  • a session ID (which is the primary key),
  • a user ID,
  • a last access time (which is regularly updated, as it is used to implement session timeout).

The actions my application performs (with respect to session management) are:

  • creating a new session (inserting a record into the session management table),
  • checking if a session is there (looking up a record by its session ID), while refreshing its last access time (updating the field with the same name),
  • destroying a session (deleting a record).

Additionally, there is a background job to delete timed out sessions.

I would like to replace the use of this relational database table with something else, possibly a key-value database with automatic TTL.

Is there a simple AWS service which could suit my needs?

asked a month ago96 views

3 Answers
1
Accepted Answer

Would suggest Amazon DynamoDB, which natively supports automatic Time-to-Live (TTL) expiration of items. You can store session data as key-value pairs and let DynamoDB automatically delete expired sessions without needing background jobs.

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html

EXPERT

answered a month ago

EXPERT

reviewed a month ago

1

Hello.

As stated in the AI-generated response from the AWS re:Post Agent, DynamoDB is recommended as the primary choice.

I would recommend ElastiCache Serverless Valkey as the second choice.
For legacy applications where integrating the AWS SDK can be challenging, ElastiCache Valkey is a recommended choice, as it allows for connectivity via standard Redis clients.
https://aws.amazon.com/jp/blogs/database/get-started-with-amazon-elasticache-for-valkey/

EXPERT

answered a month ago

EXPERT

reviewed a month ago

0

Yes, Amazon DynamoDB would be an excellent fit for your session management needs. It's a fully managed key-value and document database that natively supports Time to Live (TTL) functionality, which can automatically handle session expiration without requiring a background job.

For your use case, you would structure your DynamoDB table with the session ID as the primary key. You can store the user ID and other session data as attributes. The key feature for you is DynamoDB's TTL capability, which allows you to specify a timestamp attribute that DynamoDB monitors. When items reach their expiration time, DynamoDB automatically deletes them in the background—eliminating your need for a custom cleanup job.

To implement session timeout based on last access time, you would update the TTL attribute each time a session is accessed. For example, if you want a 30-minute timeout, you'd set the TTL attribute to the current time plus 1800 seconds (expressed as a Unix epoch timestamp). Each time the user accesses their session, you'd update this timestamp to extend the expiration window.

Enabling TTL on a DynamoDB table is straightforward—you simply specify which attribute name DynamoDB should use to determine expiration. The attribute name is case-sensitive and must match what you use in your read and write operations. Once enabled, DynamoDB handles the deletion of expired items automatically, typically within 48 hours of expiration, though the exact timing can vary.

DynamoDB's performance characteristics make it well-suited for session management: it provides single-digit millisecond response times, scales automatically to handle your workload, and TTL deletions don't consume write capacity or impact your application's performance. This makes it a simple, cost-effective replacement for your current relational database approach.
Sources
Enable time to live (TTL) in DynamoDB - Amazon DynamoDB

answered a month ago

EXPERT

reviewed a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.