Skip to content
← All patterns

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

web-sessionsDynamoDB workbench
Ready to run
Explore an access patternSelect to load & run

Fetch the session behind a token, if it has not expired.

Request
Execute against the local Dynoxide engine
ReturnedFiltered outChanged
PK(pk)
createdAt
expiresAt
userAgent
userId
ipAddress
isActive
userEmail
SESSION#7f3a9c2e
1792886400N
1795478400N
Firefox/desktopS
u-adaS
—
—
—
SESSION#b81d4f6a
1793491200N
1796083200N
Safari/iOSS
u-rajS
—
—
—
SESSION#4e7b1c93
1794864960N
1797456960N
Edge/desktopS
u-samS
—
—
—
SESSION#2c5e8b17
1792108800N
1794700800N
Chrome/desktopS
u-meiS
—
—
—
SESSION#9a0f3d5b
1791849600N
1794441600N
Chrome/AndroidS
u-adaS
—
—
—
SESSION#sess_alice_laptop
1794871800N
1799369000N
NuboApp/1.0 macOSS
user_aliceS
192.168.1.10S
trueBOOL
alice.chen@example.comS
SESSION#sess_alice_phone
1794872520N
1800169000N
NuboApp/1.0 iOSS
user_aliceS
203.0.113.55S
trueBOOL
alice.chen@example.comS
SESSION#sess_bob_desktop
1794834600N
1799569000N
Mozilla/5.0 ChromeS
user_bobS
10.0.0.42S
trueBOOL
bob.patel@example.comS
SESSION#sess_carol_expiring
1718282600N
1718369000N
curl/8.4S
user_carolS
172.16.0.101S
falseBOOL
carol.garcia@example.comS
SESSION#sess_daniel_admin
1794870000N
1800769000N
NuboApp/1.0 macOSS
user_danielS
198.51.100.23S
trueBOOL
daniel.smith@example.comS
SESSION#sess_elena_already_expired
1728282600N
1728369000N
Mozilla/5.0 SafariS
user_elenaS
192.168.1.55S
falseBOOL
elena.kim@example.comS
SESSION#sess_frank_fresh
1794873480N
1799969000N
NuboApp/1.0 iOSS
user_frankS
10.0.0.88S
trueBOOL
frank.johnson@example.comS
SESSION#sess_grace_long_lived
1794619800N
1802369000N
NuboApp/1.0 macOSS
user_graceS
203.0.113.14S
trueBOOL
grace.ahmed@example.comS
Awaiting request
Your next query starts here.

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.