Should I use AWS Cognito username or sub uid for storing in database?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Use Cognito sub as the canonical external identity key in your database. Keep username only as a mutable attribute for display, search, or login workflows.
This is the safer choice because sub is the stable unique identifier Cognito assigns to the user, while username is a login-facing attribute whose semantics can vary with pool configuration, aliases, and user-facing rename expectations. Database keys should prefer stability over readability.
Why sub Is the Better Database Identifier
The most important property of an identity key is immutability. Foreign keys, audit logs, and user-owned records should not depend on values that might change or be reinterpreted later.
Cognito tokens include a sub claim specifically for stable user identification. AWS documentation also describes sub as the best way to identify a user, while warning that username is not always the safest long-term identifier.
That leads to a simple rule:
- use
subfor referential identity - use
usernamefor user-facing display or lookup
A Practical Data Model
Store the external Cognito identity separately from your internal primary key.
Then the rest of your database can reference app_user.id, while authenticated requests map JWT claims to the correct user row through cognito_sub.
This gives you two levels of stability:
- your internal numeric or UUID key for database joins
- Cognito
subfor external identity mapping
Lookup by JWT Claims
At request time, validate the token and map the user by sub.
If the username changes later, this lookup still works because sub does not.
Why username Should Not Be the Key
username feels attractive because it is readable, but readability is the wrong property for a long-lived identity key.
Problems with using username as a stored foreign-key-style identifier include:
- rename or alias changes break assumptions
- case-sensitivity behavior can vary
- federated identity scenarios complicate uniqueness expectations
- login UX choices are not the same thing as canonical identity
Even if the username never changes in your current configuration, treating it as a display/login attribute keeps your schema more future-proof.
Migration Strategy If You Already Used Username
If your existing database already stores username as the main Cognito identifier, migrate in stages:
- add a
cognito_subcolumn - backfill it from validated token claims or Cognito exports
- enforce uniqueness on
cognito_sub - switch application lookups to
sub - gradually remove username-based references from downstream tables
This avoids a risky one-step identity cutover.
Common Pitfalls
A common mistake is storing only username and assuming it is permanently stable because the current UI does not expose a rename feature. Identity design should survive future product changes, not only today’s interface.
Another issue is validating a token and then still looking up the user by username instead of the token’s stable identity claim. That adds unnecessary fragility to every request.
Developers also sometimes skip an internal user key entirely and spread Cognito identifiers through many relational tables. Keeping one internal user ID remains the better database design.
Finally, do not confuse a friendly sign-in attribute with a durable identity key. Those are different concerns.
Summary
- Store Cognito
subas the canonical external identity key. - Treat
usernameas mutable login or display data, not referential identity. - Map validated JWT claims to your user row by
sub. - Keep a separate internal user primary key for database joins.
- If you already keyed on username, migrate gradually rather than flipping identity logic in one step.
Related reading
- Should I use AWS Elastic Beanstalk or the Amazon EC2 Container Service ECS to scale Docker containers?
- Should I use AWS Elastic Beanstalk or the Amazon EC2 Container Service ECS to scale Docker containers?
- sidecar vs init container in kubernetes
- Signing ElasticSearch AWS calls
- Should I use the datetime or timestamp data type in MySQL?
- should mongodb nodes in replicaset need to be time synchronized?
- Simple DynamoDB request failing with ResourceNotFoundException
- SNS to Lambda vs SNS to SQS to Lambda

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.