accesspatterns.dev
← All models

auth

Web Sessions

Server-side sessions keyed by token, expired by the database.

A hash-only table where every read is a token lookup. 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.

The model

Session

A signed-in session, valid until its expiry.

pk
SESSION#<token>

Attributes: userId (S), expiresAt (N), createdAt (N), userAgent (S)

Access patterns

  • GetItemResolve a session token

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

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

Two of the five seeded 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.

Taught in the course

web-sessions

These rows are real and already here. Running an operation against them downloads a DynamoDB-compatible engine, about 850 KB, and executes it in this tab.

Try an example

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

PK(pk)
createdAt
expiresAt
userAgent
userId
SESSION#7f3a9c2e
1788220800N
1788307200N
Firefox/desktopS
u-adaS
SESSION#b81d4f6a
1788825600N
1788912000N
Safari/iOSS
u-rajS
SESSION#4e7b1c93
1790208000N
1790294400N
Edge/desktopS
u-samS
SESSION#2c5e8b17
1787443200N
1787529600N
Chrome/desktopS
u-meiS
SESSION#9a0f3d5b
1787184000N
1787270400N
Chrome/AndroidS
u-adaS

Run an operation to see the raw engine response.

table web-sessionskeys PKitems 5

Transactions, streams, tags and TTL are among the operations the in-browser engine doesn't implement yet.