Quick revision
The short answer
We can add a global secondary index on email and phone, keep a local secondary index (which does not help here), write our own lookup items such as EMAIL#a@b.com -> userId in a transaction with the user item, or scan, which is never acceptable. A relational unique index is updated in the same transaction as the row, so it is immediately consistent and enforces uniqueness for free. In a partitioned store, a global index is eventually consistent and cannot enforce uniqueness, while transactional lookup items give both at the cost of extra writes and code.
What to remember
In this answer, we saw why a partitioned store can only look up items by partition key, and compared the ways to add lookups by email and phone. A relational unique index is maintained in the same transaction as the row and enforces uniqueness for free. A global secondary index is asynchronous, eventually consistent, costs extra write capacity and cannot enforce uniqueness; a local index only works inside one partition; a scan is never acceptable. Lookup items written with conditional puts in a transaction give uniqueness and strong reads, paid for with more writes and application code.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We can add a global secondary index on email and phone, keep a local secondary index (which does not help here), write our own lookup items such as EMAIL#a@b.com -> userId in a transaction with the user item, or scan, which is never acceptable. A relational unique index is updated in the same transaction as the row, so it is immediately consistent and enforces uniqueness for free. In a partitioned store, a global index is eventually consistent and cannot enforce uniqueness, while transactional lookup items give both at the cost of extra writes and code.
Why this question is asked
Almost every system that starts with a key-value store eventually needs a second way to find the same record. Login by email or phone is the classic case, and it is also a case where getting consistency wrong creates real bugs: two accounts with the same email, or a user who just signed up and cannot log in. Interviewers ask this to see whether we understand that partitioned stores only answer efficiently by the partition key, and what each workaround really costs. In this answer, we will recap why the partition key matters, compare the relational secondary index with the options in a partitioned store, look closely at the lookup item pattern, and finish with the follow-up about changing an email safely.
1A Quick Recap: Why Only the Partition Key Is Cheap
A hash-partitioned key-value store, such as DynamoDB or Cassandra, decides where each item lives by hashing its partition key. Our user table is keyed by user id, so the store can compute which partition holds user 42 and go straight there. That is why a lookup by user id takes a few milliseconds no matter how many users exist.
An email address gives the store nothing to hash, because items are not placed by email. To find the user with a given email, the store would have to ask every partition. That is a scan, and on a table of 50 million users it reads the whole table, costs real money in a pay-per-read store, and takes seconds or minutes. A login flow cannot do that on every request. So we need a second structure that is placed by email.
2The Relational Baseline
In PostgreSQL or MySQL, we would simply create a unique index on email. The important property is that the index is maintained inside the same transaction as the row. When the INSERT commits, the index entry commits with it; when it rolls back, the index entry disappears. Two concurrent sign-ups with the same email cannot both commit, because the second one hits the unique constraint.
So the relational answer gives us immediate consistency and uniqueness with one line of DDL. That is the yardstick against which the NoSQL options should be measured.
3The Options in a Partitioned Store
A global secondary index, such as a DynamoDB GSI, is a second copy of selected attributes, partitioned by a different key, here email. The store keeps it up to date for us, but asynchronously: the base write succeeds, and the index catches up a fraction of a second later. So reads from it are eventually consistent, and a user who signs up and immediately logs in may, rarely, not be found. It also cannot enforce uniqueness, because two users with the same email can both be written to the base table before the index sees either. Every write that changes the indexed attribute costs extra write capacity for the index, and if the index runs out of capacity and is throttled, the store slows down writes to the base table too.
A local secondary index keeps the same partition key as the table and only adds a different sort key. It is useful for queries inside one partition, such as one customer's orders sorted by date. Since our partition key is user id, a local index could only search inside a single user, so it cannot answer which user owns this email.
The third option is to write our own lookup items. Next to the user item, we write an item whose key is EMAIL#a@b.com and whose value is the user id, and another for PHONE#+15551234567. We write all of them in one transaction, with a condition that the lookup items do not already exist. Login reads the lookup item with a strongly consistent read, then reads the user by id.
| Option | Consistency | Uniqueness | Cost |
|---|---|---|---|
| Relational unique index | immediate, same transaction | enforced by the database | one index write per insert |
| Global secondary index | eventual, async | not enforced | extra write capacity; throttling backs up writes |
| Local secondary index | strong within a partition | within one partition only | useless for lookup across users |
| Lookup items in a transaction | strong reads possible | enforced by conditional put | 2 to 3x writes, more code |
| Scan with a filter | whatever the table gives | no | reads the whole table; never on a hot path |
4The Lookup Item Pattern in Code
Here is how sign-up looks with the DynamoDB Java SDK in a single table that holds both kinds of items. The transaction succeeds only if neither the email nor the phone is already claimed.
Notice what we pay for this. Every sign-up is three writes inside a transaction, and DynamoDB charges transactional writes at about twice the normal rate, so it is roughly six times the cost of a plain put. The application now owns the index logic: every change to email or phone must update the lookup items in the same transaction, and a bug there produces stale entries. In return, we get what the relational index gave us for free: uniqueness and a strongly consistent lookup.
5Which One to Choose
For a login flow, uniqueness is not optional, so the lookup item pattern is the usual choice in a pure key-value design. A GSI is fine for lookups where a short delay and duplicates are acceptable, such as finding users by referral code for a report. And if many such lookups keep appearing, it is worth asking whether user accounts belong in a relational database in the first place: 50 million users fit comfortably in PostgreSQL, and the constraints come free.
6Follow-up questions
How would you change a user's email without a window where two users own it?
We do it in one transaction with three operations: put the new lookup item EMAIL#new with the condition that it does not exist, delete the old lookup item EMAIL#old with the condition that it still points to this user, and update the user item with the condition that its email is still the old value. Either all three apply or none do, so at no moment do two users own an address, and a concurrent change by another request fails its condition instead of overwriting.
Can I read a DynamoDB GSI with strong consistency?
No. Global secondary indexes only support eventually consistent reads, which is exactly why they are a poor fit for a uniqueness check. Local secondary indexes do support strongly consistent reads, but they only work within one partition.
What about normalising the email?
We should lower-case and trim it, and decide a rule for plus addressing, before building the lookup key. Otherwise A@b.com and a@b.com become two different lookup items and uniqueness is lost in a way the database cannot see.
7Conclusion
In this answer, we saw why a partitioned store can only look up items by partition key, and compared the ways to add lookups by email and phone. A relational unique index is maintained in the same transaction as the row and enforces uniqueness for free. A global secondary index is asynchronous, eventually consistent, costs extra write capacity and cannot enforce uniqueness; a local index only works inside one partition; a scan is never acceptable. Lookup items written with conditional puts in a transaction give uniqueness and strong reads, paid for with more writes and application code.
Open with
A hash-partitioned store only finds items cheaply by the partition key, here user id, so lookup by email or phone needs a second structure. In a relational database that is a unique index, and it is updated in the same transaction as the row, so it is immediately consistent and guarantees no two users share an email.
Then, if they want more
In something like DynamoDB I have a few options. A global secondary index on email is maintained for me but asynchronously, so reads are eventually consistent, it cannot enforce uniqueness, every write costs extra capacity, and if the index is throttled it slows writes to the base table.
A local secondary index keeps the user id as partition key, so it cannot search across users at all. For login I would use lookup items: EMAIL#address and PHONE#number items pointing to the user id, written in one transaction with the user item and a condition that they do not exist.
That gives uniqueness and strongly consistent reads, at the cost of roughly three transactional writes per sign-up and code that must keep the lookups in sync. Changing an email is also one transaction: claim the new lookup item, delete the old one, and update the user, each with a condition. A scan with a filter is never acceptable on a login path.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What are the options for the extra lookups, and what consistency and cost does each...": https://scrivio-interview-library.netlify.app/architecture/storage-and-pipelines/e72b5e8c59/