Sessions and rate limits (DynamoDB) database schema
Sign-in sessions and per-key request counters, each removed by DynamoDB's TTL when it lapses.
2 tables, 0 references
Opens as a new diagram you can change. Free.
Tables
sessions
One item per session, found by its token.
- tokenS not null
- userIdS not null
- userAgentS
- createdAtS not null
- expiresAtN not null
rate_limits
A counter per caller and window, bumped with an atomic ADD.
- keyS not null
- windowN not null
- countN not null
- expiresAtN not null
As AWS CLI script
The diagram also exports CloudFormation template, AWS CDK (TypeScript), Terraform, Sample items (BatchWriteItem, one request per line).
#!/bin/sh
# Sessions and rate limits (DynamoDB): DynamoDB tables. Needs the AWS CLI signed in to the target account and region.
set -e
# sessions
# Other attributes, not declared to DynamoDB: userId (S), userAgent (S, optional), createdAt (S), expiresAt (N)
aws dynamodb create-table --cli-input-json '{"TableName":"sessions","BillingMode":"PAY_PER_REQUEST","AttributeDefinitions":[{"AttributeName":"token","AttributeType":"S"},{"AttributeName":"userId","AttributeType":"S"},{"AttributeName":"createdAt","AttributeType":"S"}],"KeySchema":[{"AttributeName":"token","KeyType":"HASH"}],"GlobalSecondaryIndexes":[{"IndexName":"byUser","KeySchema":[{"AttributeName":"userId","KeyType":"HASH"},{"AttributeName":"createdAt","KeyType":"RANGE"}],"Projection":{"ProjectionType":"KEYS_ONLY"}}]}'
aws dynamodb wait table-exists --table-name 'sessions'
aws dynamodb update-time-to-live --table-name 'sessions' --time-to-live-specification '{"Enabled":true,"AttributeName":"expiresAt"}'
# rate_limits
# Other attributes, not declared to DynamoDB: count (N), expiresAt (N)
aws dynamodb create-table --cli-input-json '{"TableName":"rate_limits","BillingMode":"PAY_PER_REQUEST","AttributeDefinitions":[{"AttributeName":"key","AttributeType":"S"},{"AttributeName":"window","AttributeType":"N"}],"KeySchema":[{"AttributeName":"key","KeyType":"HASH"},{"AttributeName":"window","KeyType":"RANGE"}]}'
aws dynamodb wait table-exists --table-name 'rate_limits'
aws dynamodb update-time-to-live --table-name 'rate_limits' --time-to-live-specification '{"Enabled":true,"AttributeName":"expiresAt"}'
More SaaS & accounts templates
- SaaS starterOrganisations, users and memberships for a multi-tenant app.
- Authentication & sessionsUsers with linked identities, sessions, password resets and MFA factors.
- Roles & permissionsRole-based access control: roles, permissions and who holds them.
- Subscription billingCustomers, plans and prices, subscriptions, invoices and payments.
- Usage-based billingMeters, usage events and rate cards that roll up into invoices.
- Feature flagsFlags per environment, targeting rules and per-user overrides.
- Audit logWho did what to which record, with before and after values.
- NotificationsNotification types, per-user preferences, an inbox and delivery attempts.
- API platformDeveloper apps with API keys, webhooks and usage records.
- Product analyticsUsers, sessions and tracked events with properties, plus saved funnels.
Start with one diagram, free
One diagram with two branches, no card needed. Upgrade when your team needs more.