Quick revision
The short answer
The usual answer is a new query table, orders_by_product_day with partition key (product_id, day), filled from existing data by a backfill job and kept current by writing to both tables from the application or through change data capture, with a plan for keeping the two consistent. Cassandra's materialized views would maintain it automatically but are experimental with known consistency problems, so they should not back critical paths; check your version. If the request is really analytical, the data belongs in a warehouse or search index, and either way the request goes into the documented list of access patterns.
What to remember
In this answer, we saw that a Cassandra table partitioned by customer cannot serve a query by product without a full scan, so the standard answer is a new table, orders_by_product_day with partition key (product_id, day). We start dual writes from the application or through events or change data capture, backfill history with a throttled, resumable job, plan for consistency and reconciliation between the tables, avoid experimental materialized views for critical paths, send analytical questions to a warehouse or search index instead, and record the new pattern in the documented access-pattern list.
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.
The usual answer is a new query table, orders_by_product_day with partition key (product_id, day), filled from existing data by a backfill job and kept current by writing to both tables from the application or through change data capture, with a plan for keeping the two consistent. Cassandra's materialized views would maintain it automatically but are experimental with known consistency problems, so they should not back critical paths; check your version. If the request is really analytical, the data belongs in a warehouse or search index, and either way the request goes into the documented list of access patterns.
Why this question is asked
Cassandra tables are designed around the queries known at launch, so the first new query after launch is a rite of passage. With only orders_by_customer, finding all orders for a product across customers would mean scanning every partition in the cluster, which Cassandra refuses without ALLOW FILTERING and which would be unacceptable on a production table anyway. Interviewers ask this to see whether we know the options, what each costs, and how to add a table to a live system safely. In this answer, we will recap why the existing table cannot answer, design the new table, backfill it, keep it current, look at materialized views and the analytics alternative, and close with the access-pattern list.
1Why the Existing Table Cannot Answer
In Cassandra the partition key decides which nodes hold a row, and an efficient query must name it. orders_by_customer is partitioned by customer_id, so it can find one customer's orders quickly. A query by product_id names no customer, so the coordinator would have to ask every node to read every partition and filter for the product, a full scan whose cost grows with the whole table. Secondary indexes do not fix this for a frequent query, because they still have to consult many nodes. So we need data organised by product, which in Cassandra means another table.
2Option 1: A New Table for the New Query
We design the new table from the query: orders for one product within the last week. Partitioning by product alone would put every order of a popular product, forever, in one partition, so we add the day to the partition key to keep partitions bounded. The query for the last week then reads seven partitions, which the application can do in parallel.
Notice that one order with three products becomes three rows here, one per product, because the table is keyed by product. If a few best sellers have tens of thousands of orders a day, we can bucket further by hour or add a small shard number to the partition key, so no partition grows too large.
3Backfilling Six Months of History
The new table starts empty, so a batch job copies existing orders into it. The job reads orders_by_customer, which is a full table read, and that is acceptable here because it runs once, in the background, at a controlled rate. The Spark Cassandra connector reads token ranges in parallel and writes the transformed rows; DSBulk can unload and load data for simpler cases. We throttle the job so it does not hurt production latency, and make it resumable by tracking which token ranges are done.
The order of steps matters. We start writing new orders to the new table first, and only then run the backfill. That way no order placed during the backfill is missed. Rows written by both paths do not conflict, because a Cassandra write is an upsert by primary key: writing the same order twice produces the same row.
- Create orders_by_product_daypartition (product_id, day)
- Start dual writes for new ordersapplication or CDC
- Backfill history from orders_by_customerSpark connector, throttled, resumable
- Do sampled counts match?YesRelease the new queryNoFind gaps, rerun those ranges
- Add the pattern to the documented listwith owner and table
5 steps in order. Play them, or step through them one at a time.
4Keeping Both Tables Current
From now on every order must reach both tables. The simplest way is in the application: the order service writes to both. Cassandra has no transactions across tables, so we must decide what happens if one write succeeds and the other fails. A logged batch containing both inserts guarantees that, once the batch is accepted, all its statements will eventually be applied, though not in isolation and at extra cost. Alternatively we write the order once and publish an event, for example through an outbox and Kafka, and a consumer writes the product table, which makes the product table eventually consistent, typically a few seconds behind.
Change data capture is the other route: Cassandra can write commit log segments for CDC-enabled tables, and connectors such as the Debezium Cassandra connector turn them into events. It avoids changing every writer but is operationally heavier. Whichever we choose, a periodic reconciliation job compares samples of the two tables and repairs differences, because some divergence will happen over time.
5 messages in order. Play them, or step through them one at a time.
The product table lags by seconds but every order eventually arrives.
5Why Not a Materialized View?
Cassandra has materialized views, which automatically maintain a second table keyed differently from a base table. They look like the perfect answer, but they have known consistency issues: under failures the view can diverge from the base table, and there is no complete built-in way to detect and repair that. Because of this they were marked experimental, and recent versions disable them by default; check your version's documentation and settings before considering them. For a path that matters to the business, the explicit table we control is safer. Views also cannot easily express our day bucketing, since their partition key must come from the base table's columns.
The other question is whether this query belongs in Cassandra at all. 'All orders by product in the last week' sounds like a report for the product team. If it is used in dashboards or ad-hoc analysis, the better path is to stream orders to a warehouse or a search index, where any grouping and filtering is possible without designing a table per question. We keep Cassandra for the operational, high-volume, low-latency queries the application itself makes.
6Updating the Access-Pattern List
Every Cassandra system should have a documented list of access patterns: each query, its parameters, its expected volume, the table that serves it and the team that owns it. This request is a good moment to add 'orders by product per day' to it, together with the decision we made and why. The next time someone asks for a new query, the list shows what exists, whether an existing table can serve it, and what a new table costs in storage and write amplification, since every query table multiplies writes.
7Follow-up questions
How would you decide between the new table and the warehouse?
By who asks and how. If the application needs the answer in a request path, with low latency and high volume, for example a seller page showing recent orders of a product, a Cassandra table fits. If people ask it in dashboards and change the question often, a warehouse or search index is cheaper and more flexible.
Why not just add a secondary index on product_id?
A secondary index in Cassandra is stored locally on each node, so a query by product has to ask many nodes and gather results, which does not scale for frequent queries or popular products. Storage-attached indexes in newer versions improve this for some use cases, but a dedicated query table remains the predictable choice for a core access pattern.
What extra cost does the new table add?
Storage for another copy of the order data, about one row per product per order, write amplification since each order now causes more writes, compaction and repair work for another table, and the code or pipeline that keeps it current. These are usually acceptable, but they should be stated in the decision.
8Conclusion
In this answer, we saw that a Cassandra table partitioned by customer cannot serve a query by product without a full scan, so the standard answer is a new table, orders_by_product_day with partition key (product_id, day). We start dual writes from the application or through events or change data capture, backfill history with a throttled, resumable job, plan for consistency and reconciliation between the tables, avoid experimental materialized views for critical paths, send analytical questions to a warehouse or search index instead, and record the new pattern in the documented access-pattern list.
Open with
The existing table is partitioned by customer, so a query by product would scan every partition, which Cassandra rejects without ALLOW FILTERING and which we should not run in production anyway.
Then, if they want more
My first option is a new query table, orders_by_product_day, with partition key product_id and day so partitions stay bounded, clustered by order time; a last-week query reads seven partitions in parallel.
I would start dual writes for new orders first, either from the application with a logged batch or through an outbox event and a consumer, or with change data capture, and then backfill six months of history with a throttled, resumable Spark Cassandra connector job reading orders_by_customer. Writes are upserts, so overlap is harmless.
Since there are no cross-table transactions, I would plan for the tables to diverge occasionally and run a reconciliation job. I would avoid materialized views for this, because they have known consistency problems, are marked experimental and are disabled by default in recent versions; I would check the version we run.
If the query is really for reports or dashboards, I would rather stream orders into the warehouse or a search index. And I would add this pattern to the documented list of access patterns with its table and owner.
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
- Redis documentation, MongoDB manual
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 "Six months after launch, the product team asks for a new query on a Cassandra-based...": https://scrivio-interview-library.netlify.app/databases/nosql-fundamentals/0889a98ff5/