AWS Cognito
database storage
user identification
username vs UID
cloud computing

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.

Practice system design

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 sub for referential identity
  • use username for user-facing display or lookup

A Practical Data Model

Store the external Cognito identity separately from your internal primary key.

sql
1create table app_user (
2  id bigserial primary key,
3  cognito_sub text not null unique,
4  username text,
5  email text,
6  created_at timestamptz not null default now()
7);

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 sub for external identity mapping

Lookup by JWT Claims

At request time, validate the token and map the user by sub.

javascript
1function getUserKeyFromClaims(claims) {
2  const sub = claims.sub;
3  if (!sub) {
4    throw new Error("missing sub claim");
5  }
6  return sub;
7}
8
9async function findUserByTokenClaims(db, claims) {
10  const cognitoSub = getUserKeyFromClaims(claims);
11  return db.query(
12    "select id, cognito_sub, username from app_user where cognito_sub = $1",
13    [cognitoSub]
14  );
15}

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:

  1. add a cognito_sub column
  2. backfill it from validated token claims or Cognito exports
  3. enforce uniqueness on cognito_sub
  4. switch application lookups to sub
  5. 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 sub as the canonical external identity key.
  • Treat username as 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.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design