auth
Web Sessions
Who owns this session?
Server-side sessions keyed by token, expired by the database and listed per user.
A hash-only table where a session is read by its token. The interesting part is not the read, it is the deletion: a numeric expiresAt attribute hands cleanup to DynamoDB's TTL instead of a sweep job, so the table stays bounded without anyone writing a cron. An index on userId lists every session a user has open.
The model
Session
A signed-in session, valid until its expiry.
- pk
- SESSION#<token>
Attributes: userId (S), expiresAt (N), createdAt (N), userAgent (S), userEmail (S), ipAddress (S), isActive (BOOL)
Access patterns
- GetItemResolve a session token
Fetch the session behind a token, if it has not expired.
- Query · GSI1List sessions for user
Return every session bound to a user (e.g., for a device-management UI).
- DeleteItemRevoke session
Delete a session by id, invalidating the token immediately.
Design notes
TTL is a design here, not a live demonstrationtrade-off
The in-browser engine runs no background expiry, so every seeded session stays in the table and you can read the expired ones. Real DynamoDB deletes them too, but on its own schedule: AWS documents typically within a few days of expiresAt passing, with no guarantee attached. It is background work, not a trigger.
Presence is not validitywhy
Four of the hand-written sessions have passed their expiresAt and are still sitting in the table, because TTL deletion lags. So an application compares expiresAt to the clock rather than treating a row's existence as proof it is still good - a signed-out-by-expiry user whose row has not been swept yet must not be let back in.
Why not delete on sign-outtrade-off
Explicit deletion still makes sense for sign-out, since that is a deliberate act. TTL covers the far commoner case of a session nobody ever closes, which is otherwise a growing table and a write-heavy sweep to keep it in check.
TTL expiry is eventualwhy
DynamoDB deletes an expired item in the background, usually within a few days of its expiry time, and until then a read still returns it. So check expiresAt > now() when signing someone in: the attribute decides whether a session is valid, and TTL only tidies up.
A GSI on userIdwhy
Without the GSI you'd need to scan the whole table to enumerate a user's sessions. The GSI makes List sessions for user an O(k) query where k is the session count for that user.
Four sessions start out expiredgotcha
sess_carol_expiring, sess_elena_already_expired, 2c5e8b17 and 9a0f3d5b have expiresAt times in the past, so they are due for deletion as soon as the table is seeded. Until TTL removes them, they are the case the expiresAt > now() check is there for: still in the table, but expired.
Taught in the course
- The key is the address - GetItem needs the whole primary key: partition and sort.
- A different key for a different question - A secondary index re-keys your items so you can Query them on a new axis.
Fetch the session behind a token, if it has not expired.
Choose an access pattern above, or build your own request. See what comes back and what it costs.
Write transactions, streams, tags and TTL are among the operations this browser build leaves out. dynoxide's native build has them.