Quick revision
The short answer
A Scan with a filter would read and pay for all 2 billion items on every request, so it is far too slow and expensive for a user-facing API. Instead we add a GSI with email as its partition key, plan capacity for the online backfill, wait until the index is ACTIVE, and enforce email uniqueness with a separate item keyed by email written in a transaction. Reads move to the index behind a feature flag, with shadow comparisons before we switch fully.
What to remember
In this answer, we saw that a Scan with a filter reads and bills every item, which at 2 billion items costs hundreds of millions of read units per lookup. We replaced it with a GSI keyed by normalised email with a narrow projection, planned capacity for the online backfill and waited for ACTIVE, used a transactional email item to enforce uniqueness and provide strongly consistent lookups, and rolled the reads out behind a feature flag with shadow comparisons.
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.
A Scan with a filter would read and pay for all 2 billion items on every request, so it is far too slow and expensive for a user-facing API. Instead we add a GSI with email as its partition key, plan capacity for the online backfill, wait until the index is ACTIVE, and enforce email uniqueness with a separate item keyed by email written in a transaction. Reads move to the index behind a feature flag, with shadow comparisons before we switch fully.
Why this question is asked
Adding a new access pattern to a large DynamoDB table is a real operational event, not a code change. The table here holds 2 billion items, so even small inefficiencies are multiplied into hours of work and large bills. Interviewers use this scenario to see whether we know why Scan is a trap, how index backfills behave, and how we protect users while the change rolls out. In this answer, we will measure what the Scan would cost, design the GSI, plan the backfill, deal with uniqueness and consistency, and roll the reads out safely.
1Why a Scan With a Filter Does Not Work
A Scan reads the table from the first partition to the last. A filter expression only removes items from the response after they have been read, so the read cost is the same as reading everything. Each Scan call also returns at most 1 MB, so the API would need to make thousands of sequential calls to finish.
Let us put numbers on it. If the average item is 1 KB, the table holds about 2 TB. An eventually consistent read costs half a read unit per 4 KB, so one full Scan costs about 2 TB / 4 KB / 2 = 250 million read units. Even with parallel segments, one lookup would take minutes and cost real money, and a few concurrent logins would consume the table's read capacity and throttle everything else. So a Scan is acceptable for an occasional offline job, never for a request a user waits on.
2Designing the Email Index
The right tool is a global secondary index whose partition key is email. A Query on it with email = :e reads one small item collection, typically one item, in a few milliseconds. Because GSIs can be created on an existing table, we do not need to migrate the base data.
Notice the projection. The API only needs the user id and status from the lookup, so we project those two instead of ALL. With 2 billion items, every attribute we copy is multiplied into storage and write cost. We also normalise email before storing it, for example lower case and trimmed, because the index matches the exact string.
3Planning the Backfill
When the index is created, DynamoDB backfills it online: it reads every existing item and writes an index entry for each item that has an email attribute. The table stays fully available, but the backfill consumes write capacity on the index and takes time, often hours for a table this size.
On an on-demand table DynamoDB scales the index as it backfills, and we mainly budget for 2 billion index writes. On a provisioned table, the index's write capacity limits the backfill speed: at 20,000 write units per second, 2 billion small items take about 2,000,000,000 / 20,000 = 100,000 seconds, a little over a day. If the index's capacity is too low, it also throttles normal writes to the base table, so we raise it for the backfill, start during a quiet period, and watch the OnlineIndexPercentageProgress and throttling metrics. Only when IndexStatus is ACTIVE can the index be queried.
- Normalise emails in writeslower case, trimmed
- Create email-indexINCLUDE
userId, status - Backfill runs onlinewatch progress and throttling
- Index ACTIVE?
IndexStatusYesEnable shadow readsNoKeep waiting - Compare shadow resultsflag still off for users
- Mismatch rate near zero?metricsYesRamp flag 1% -> 100%NoInvestigate and fix data
- Remove old code path
7 steps in order. Play them, or step through them one at a time.
4Uniqueness and Consistency
There are two traps that a GSI does not solve. First, a GSI key is not unique: if two users registered with the same email, the index happily holds both. Second, GSI reads are eventually consistent, so a lookup immediately after registration might miss the new user for a short time.
The standard pattern solves both at once. We write a separate item whose primary key is the email, in the same table or a small table, and write it in a transaction with the user item, with a condition that it does not exist yet.
Because the email item has a real primary key, a GetItem on it can be strongly consistent, so the login path can use it directly and avoid the GSI's lag. Existing users need these items too, which means a one-off job that writes them and reports any duplicates already in the data, so the business can decide how to merge them.
5Switching Reads Safely
We roll out the new lookup behind a feature flag. With the flag off for users, the service performs the new index lookup in the background for a sample of requests and compares the result with the old source of the answer, logging any mismatch. When mismatches are explained and close to zero, we ramp the flag from 1 percent to 100 percent while watching latency, errors and consumed capacity on the index. The old path stays available until the new one has run cleanly for a while, so rollback is one flag change.
6Follow-up questions
Why not use a parallel Scan to make it faster?
Parallel Scan divides the table into segments and reads them concurrently. That shortens the time but not the cost: it still reads all 2 billion items, and it consumes capacity even faster, so it throttles other traffic sooner. It is a tool for exports, not lookups.
What if some items have no email attribute?
They simply do not appear in the index. A GSI is sparse: only items that have the index key attributes get an entry. That keeps the index smaller, and it is often used on purpose.
Could we export the table to S3 and look up emails there?
For analytics yes, through export to S3 and a query engine, but not for a user-facing request. The latency is seconds or more and the data is a snapshot.
7Conclusion
In this answer, we saw that a Scan with a filter reads and bills every item, which at 2 billion items costs hundreds of millions of read units per lookup. We replaced it with a GSI keyed by normalised email with a narrow projection, planned capacity for the online backfill and waited for ACTIVE, used a transactional email item to enforce uniqueness and provide strongly consistent lookups, and rolled the reads out behind a feature flag with shadow comparisons.
Open with
I would not accept the Scan. A Scan reads every item and the filter only trims the response, so with 2 billion items it would read roughly 2 terabytes and hundreds of millions of read units per lookup, take minutes, and throttle the rest of the table.
Then, if they want more
Instead I would add a global secondary index with the normalised email as partition key, projecting just the attributes the API needs, like userId and status.
DynamoDB builds it online and backfills existing items, but that consumes write capacity on the index and can take many hours at this size, so I would plan for it: on-demand, or raised provisioned capacity on the index, start at a quiet time, and watch backfill progress and throttling until the index status is ACTIVE. Two caveats matter.
A GSI is eventually consistent and does not enforce uniqueness, so I would write a separate item keyed by EMAIL#address in the same transaction as the user, with attribute_not_exists, and backfill those items for existing users, reporting any duplicates. That item also gives a strongly consistent lookup for login.
Then I would roll out the read path behind a feature flag, run shadow lookups and compare results first, and ramp gradually while watching latency and capacity, keeping the old path for rollback.
About this answer
- Last updated
- 12 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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Cassandra documentation, Amazon DynamoDB developer guide
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 "A DynamoDB table backing a user-facing API needs a new query by email, which is not a...": https://scrivio-interview-library.netlify.app/databases/cassandra-and-dynamodb/1a4369a4e7/