Quick revision
The short answer
We need three tables, one per query: orders_by_id keyed by order_id with the full order; orders_by_customer with PRIMARY KEY ((customer_id), created_at, order_id) clustered newest first and paged with the driver's paging state; and orders_by_warehouse_status with PRIMARY KEY ((warehouse_id, day, status), created_at, order_id). Every order event writes all affected tables, either in a logged batch for atomicity across tables (not isolation) or through an event consumer that updates each view idempotently, and a status change deletes the row from the old status partition and inserts it into the new one.
What to remember
In this answer, we turned three queries into three tables: orders_by_id by order_id, orders_by_customer clustered by created_at descending with order_id as a tie-breaker and paged with the driver's paging state, and orders_by_warehouse_status partitioned by warehouse, day and status. Each event writes all affected tables, with a status change moving the row between status partitions, either through a logged batch that gives atomicity but not isolation, or through idempotent event consumers. We avoided secondary indexes and materialized views because of their operational caveats.
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 need three tables, one per query: orders_by_id keyed by order_id with the full order; orders_by_customer with PRIMARY KEY ((customer_id), created_at, order_id) clustered newest first and paged with the driver's paging state; and orders_by_warehouse_status with PRIMARY KEY ((warehouse_id, day, status), created_at, order_id). Every order event writes all affected tables, either in a logged batch for atomicity across tables (not isolation) or through an event consumer that updates each view idempotently, and a status change deletes the row from the old status partition and inserts it into the new one.
Why this question is asked
An order service is the classic exercise in Cassandra modeling, because it has one entity and several very different ways people want to see it: customers browse their history, support looks up a single order, and warehouse staff work through today's orders by status. Interviewers ask for the CREATE TABLE statements because getting the keys exactly right, including the tie-breaker column and the bucket, is where understanding shows. In this answer, we will design each table from its query, explain the paging and the status move, describe the write path and its consistency, and see why secondary indexes and materialized views are not the answer here.
1Starting From the Three Queries
Query-first design means turning each query into a key. Get order by id has a single equality filter, order_id, so that is the partition key. List a customer's orders newest first with paging has an equality filter on customer_id and a sort by creation time, so customer_id is the partition key and created_at a descending clustering column. List orders by status for one warehouse per day has three equality filters, warehouse, day and status, so all three form the partition key, and we sort within it by time.
One detail applies to both sorted tables: two orders can be created in the same millisecond, and in Cassandra two rows with the same primary key overwrite each other. So we add order_id as the last clustering column, a tie-breaker that makes each row unique.
2The Three CREATE TABLE Statements
Here are the tables, each holding the columns its screen needs.
orders_by_customer holds a summary for the history list; tapping an order loads the full details from orders_by_id. The warehouse table includes the day in its partition key, which bounds each partition to one warehouse's orders in one status on one day, a few thousand rows rather than years of history. It sorts oldest first because warehouse staff work through the queue in arrival order.
3Paging Through a Customer's Orders
A customer may have hundreds of orders, and the app shows 20 at a time. The DataStax Java driver pages results natively: we set the page size, and after reading a page the driver gives us a paging state, an opaque token marking where it stopped. We send it to the client, which passes it back to fetch the next page.
Because rows are already sorted by created_at descending, each page is a contiguous slice of one partition, so paging costs the same on page 1 and page 30. The paging state should be treated as opaque; if we expose it to clients, we encode it, and the driver offers a safer PagingState wrapper for that purpose in recent versions, so check yours. An alternative is to page by the last seen key, WHERE customer_id = ? AND (created_at, order_id) < (?, ?), which is easy to put in a URL.
4The Write Path, Including Status Changes
Creating an order inserts one row into each table. Changing its status is more interesting: status is part of the warehouse table's partition key, and a key cannot be updated. The row must move from the PENDING partition to the PICKED partition, which means a delete plus an insert, while the other two tables just update the status column.
6 messages in order. Play them, or step through them one at a time.
If the coordinator dies mid-way, the batch log replays the missing statements.
A logged batch first writes the statements to a batch log on other nodes, so if the coordinator fails after applying some of them, the rest are replayed. That gives atomicity across tables: eventually all four apply. It does not give isolation: a reader can briefly see the order in neither or both status partitions. Notice also that the update needs the old status, the day and the created_at, which we get from orders_by_id before writing.
The other option is event-driven. The service writes orders_by_id as the source of truth and publishes OrderStatusChanged to Kafka, and a consumer updates the two views idempotently, keyed by order id and version so replays are harmless. This scales better and decouples the views, at the cost of a short lag.
5Why Not Secondary Indexes or Materialized Views
It might seem simpler to keep one orders table and add a secondary index on status, or let Cassandra maintain a materialized view keyed by warehouse and status. Both have operational caveats. A classic secondary index is local to each node, so a status query fans out across the cluster, and an index on a low-cardinality column like status creates enormous index partitions. Materialized views are maintained asynchronously by Cassandra and have long been marked experimental, with known cases where the view and base table drift apart and no simple repair. Explicit tables written by the application are more work but predictable, and we can see and fix any inconsistency.
6Follow-up questions
How do you handle a status change that fails half way?
With a logged batch, the batch log replays the remaining statements, so a coordinator crash does not leave the change half done. If the batch call itself times out, we do not know whether it applied, so we retry it; every statement is idempotent because it writes the same values to the same keys, so a repeat is harmless. With the event-driven design, the consumer retries until it succeeds and uses the order version to ignore old events. In both cases a periodic reconciliation job that compares orders_by_id with the status table catches anything left behind, for example from a bug.
Why include day in the warehouse partition key instead of just warehouse and status?
Without day, the PENDING partition of a busy warehouse would hold every pending order ever and churn constantly, and the DELIVERED partition would grow without bound. The day bucket bounds each partition and matches how staff work, one day at a time. It also limits tombstones, since each day's partition is read only for a short period.
Which consistency level would you use?
LOCAL_QUORUM for both reads and writes, so a customer who places an order and refreshes sees it, and a warehouse screen sees status changes made through another node.
7Conclusion
In this answer, we turned three queries into three tables: orders_by_id by order_id, orders_by_customer clustered by created_at descending with order_id as a tie-breaker and paged with the driver's paging state, and orders_by_warehouse_status partitioned by warehouse, day and status. Each event writes all affected tables, with a status change moving the row between status partitions, either through a logged batch that gives atomicity but not isolation, or through idempotent event consumers. We avoided secondary indexes and materialized views because of their operational caveats.
Open with
I would design one table per query. First, orders_by_id with order_id as the primary key and the full order, including items and address. Second, orders_by_customer with PRIMARY KEY ((customer_id), created_at, order_id) and CLUSTERING ORDER BY created_at descending, order_id ascending.
Then, if they want more
The order_id is a tie-breaker so two orders in the same millisecond do not overwrite each other, and it holds a summary for the list. I page it with the driver's page size and paging state, or by the last seen created_at and order_id. Third, orders_by_warehouse_status with PRIMARY KEY ((warehouse_id, day, status), created_at, order_id), where day bounds the partitions.
Creating an order inserts into all three. A status change updates the status column in the first two, but in the third, status is part of the partition key, so it is a delete from the old status partition plus an insert into the new one.
I would either put all of that in a logged batch, which guarantees every statement eventually applies but gives no isolation, or write orders_by_id and publish an event that a consumer applies idempotently to the views. Everything uses LOCAL_QUORUM.
I would avoid a secondary index on status and materialized views, because both have known operational problems in Cassandra, and add a reconciliation job to catch drift.
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 "Design Cassandra tables for an order service that must support: get order by id; list a...": https://scrivio-interview-library.netlify.app/databases/cassandra-and-dynamodb/3dc321e7d1/