Apache Cassandra Data Modeling: A Customer Orders Table

Design a customer-orders table in Apache Cassandra 5.0.9 with month-bucketed partitions, a runnable CQL lab, and an honest read/write quorum tradeoff.

published: reading time: 9 min read author: Geek Workbench updated: November 4, 2026
Quick Summary

Model one access pattern in Cassandra: fetch a customer’s recent orders from a calendar month. This guide follows that request through a partition key, a small CQL lab, the write and read paths, consistency choices, and the operational work that keeps replicas and SSTables in shape.

Apache Cassandra Data Modeling: A Customer Orders Table

This guide builds one Cassandra table for one request: fetch a customer’s recent orders within a calendar month. The example runs in cqlsh on Apache Cassandra 5.0.9. The release and versioned documentation were checked on 2026-11-04; Apache lists 5.0.9, released on 2026-08-07, as the current GA release on its download page.

Cassandra tables work best when their primary keys follow the queries the application needs. We will start from that request, choose a partition key that includes a month bucket, then follow a write and read through the storage engine. Your production replication factor, data-center layout, and maintenance plan depend on your cluster.

Start with the access pattern

Write down the exact request before writing CQL:

Given a customer ID and a calendar month, return that customer’s orders from a bounded time interval, newest first.

For example, the application asks for customer cust-42 from October 10 through the start of November. This is one customer and one month, so the table can target a single partition and then read a time range inside it.

The table is not intended to answer show every order for every customer or an arbitrary search by status. Cassandra does not turn a relational scan into an efficient distributed query automatically. If the product needs another access pattern, describe it separately and design a table for it. The Cassandra 5.0.9 data-modeling guide follows the same query-first approach.

Choose the partition and clustering keys

Use customer_id and month_bucket as a composite partition key. The month bucket is the first day of the calendar month, calculated by the application in a consistent timezone. created_at sorts orders within that partition, and order_id gives rows with the same timestamp a stable unique position.

CREATE TABLE orders_by_customer_month (
    customer_id text,
    month_bucket date,
    created_at timestamp,
    order_id text,
    status text,
    amount_cents int,
    PRIMARY KEY ((customer_id, month_bucket), created_at, order_id)
) WITH CLUSTERING ORDER BY (created_at DESC, order_id ASC);

The key definition has two parts:

  • (customer_id, month_bucket) is the partition key. It chooses the partition Cassandra routes to its replica nodes, so the query must supply both values.
  • (created_at, order_id) are clustering columns. They order rows inside the partition; a time predicate on created_at narrows the read, and the declared order puts newer orders first.

Putting only customer_id in the partition key would accumulate that customer’s orders over their entire lifetime. Including month_bucket splits that growth across calendar months and makes the request’s time boundary part of the key. It does not impose a fixed partition size: a very active customer can still write many rows in one month. Watch actual partition sizes and workload, and choose a smaller time bucket if the access pattern and observed volume require it. This example uses UTC for both the month bucket and query bounds so orders near midnight do not land in different buckets on different application hosts.

A monthly bucket also makes the query contract visible. The request must identify a month. For a rolling interval that crosses a month boundary, the application queries each touched month partition and combines the results. Do not omit the bucket and expect Cassandra to find matching rows across all partitions.

Cassandra’s CQL primary-key syntax places the partition key first, followed by clustering columns. Clustering columns determine the order within a partition and support a range query after the partition key is restricted. See the versioned CQL table-definition documentation for the full rules.

Run the local CQL lab

The following setup is for a single-node local lab only. SimpleStrategy with replication factor 1 does not provide production fault tolerance. Apache recommends NetworkTopologyStrategy for production clusters so replication can be configured by data center and rack. Use the actual data-center names and replication goals from your deployment; do not copy the lab keyspace settings into production. See Apache’s production recommendations before setting up a cluster.

Start cqlsh connected to a local Cassandra 5.0.9 node, then run:

CREATE KEYSPACE IF NOT EXISTS orders_lab
WITH REPLICATION = {
    'class': 'SimpleStrategy',
    'replication_factor': 1
};

USE orders_lab;

CREATE TABLE IF NOT EXISTS orders_by_customer_month (
    customer_id text,
    month_bucket date,
    created_at timestamp,
    order_id text,
    status text,
    amount_cents int,
    PRIMARY KEY ((customer_id, month_bucket), created_at, order_id)
) WITH CLUSTERING ORDER BY (created_at DESC, order_id ASC);

CONSISTENCY ONE;

INSERT INTO orders_by_customer_month
    (customer_id, month_bucket, created_at, order_id, status, amount_cents)
VALUES
    ('cust-42', '2026-10-01', '2026-10-20T14:30:00Z', 'o-1001', 'shipped', 10050);

INSERT INTO orders_by_customer_month
    (customer_id, month_bucket, created_at, order_id, status, amount_cents)
VALUES
    ('cust-42', '2026-10-01', '2026-10-11T09:15:00Z', 'o-1002', 'delivered', 4200);

INSERT INTO orders_by_customer_month
    (customer_id, month_bucket, created_at, order_id, status, amount_cents)
VALUES
    ('cust-42', '2026-10-01', '2026-10-02T10:00:00Z', 'o-0999', 'delivered', 1099);

SELECT customer_id, month_bucket, created_at, order_id, status, amount_cents
FROM orders_by_customer_month
WHERE customer_id = 'cust-42'
  AND month_bucket = '2026-10-01'
  AND created_at >= '2026-10-10T00:00:00Z'
  AND created_at < '2026-11-01T00:00:00Z';

The half-open timestamp range includes orders from October 10 onward and excludes the start of November. The October 2 row exists in the same partition but falls outside the query interval. With the selected columns and clustering order, the result values are:

customer_id month_bucket created_at order_id status amount_cents
cust-42 2026-10-01 2026-10-20T14:30:00Z o-1001 shipped 10050
cust-42 2026-10-01 2026-10-11T09:15:00Z o-1002 delivered 4200

o-0999 is excluded because its timestamp is earlier than the lower bound. The query supplies equality for both partition-key columns, then uses a range on the first clustering column. That matches this table’s key order. It avoids filtering unrelated partitions to find the requested customer and month.

The lab uses CONSISTENCY ONE because the keyspace has one replica. A production consistency choice needs to be made against the production replication strategy and topology, not inferred from this local exercise.

Follow the write and read paths

A CQL write first reaches a coordinator node. The coordinator identifies replicas for the partition using the keyspace’s replication strategy and sends the mutation to them. Each receiving replica records the mutation in its commit log for recovery and applies it to an in-memory memtable. When a memtable is flushed, Cassandra writes an immutable SSTable to disk. The write response waits for the configured consistency level’s required acknowledgements; it does not wait for SSTables to be rewritten by compaction.

The example’s composite partition key lets the coordinator route the request to replicas for (cust-42, 2026-10-01). Clustering columns organize the orders inside that partition. A read can locate the partition, use its clustering order to traverse the requested timestamp range, and return the selected rows newest first. The Cassandra 5.0.9 storage-engine documentation describes commit logs, memtables, SSTables, and reads in more depth.

Choose consistency with replica overlap in mind

Consistency levels say how many replicas must respond to a particular read or write. They do not have a universal fast or strong meaning independent of replication factor and data-center layout. The replication strategy determines where replicas are placed; the consistency level determines how many of those replicas must participate in the operation.

Assume a production keyspace uses replication factor 3 in one data center. A QUORUM write requires responses from two replicas, and a QUORUM read also requires two. Those sets must overlap because R + W > RF (2 + 2 > 3). That overlap is the reason operators often choose quorum for both operations when they want subsequent reads to include a replica that acknowledged the write.

Replica overlap does not provide serializable transactions or promise the same freshness across every topology and failure case. If read and write levels do not overlap, a read can contact replicas that did not acknowledge a write. Concurrent writes, failed operations, timestamp reconciliation, and multi-data-center placement also affect what the application observes. Cassandra’s consistency documentation explains the replica-count relationship.

For a multi-data-center keyspace using NetworkTopologyStrategy, LOCAL_QUORUM counts replicas in the coordinator’s local data center. It can suit clients that read and write in one local data center, but it does not mean every data center has already acknowledged the operation. If clients switch between data centers, or if reads and writes use different consistency levels, analyze whether their replica sets intersect under the actual replication factors. Choose levels from the application’s read-after-write needs and availability goals, then test the failure cases that matter.

Understand where repair and compaction fit

Cassandra can accept writes while a replica is unavailable, but hints are best effort. Anti-entropy repair compares replica ranges and streams differences so missed updates can be reconciled. Repair is a planned maintenance operation that uses disk and network resources. Its coverage and timing need to fit the table’s tombstone policy and cluster capacity. It is not a substitute for a backup.

Compaction merges immutable SSTables, reconciles stored versions, and may reclaim obsolete data when it is safe. It also consumes I/O and can need disk headroom while new SSTables are written. A compaction strategy should match the table workload; compaction will not correct a hot partition, a query that reads too much data, or a key that does not match the access pattern.

For repair plans, compaction behavior, monitoring, backups, and restore drills, use the Cassandra operations companion. The right schedule and settings vary with cluster topology and workload. The versioned Apache references for repair and compaction provide command and strategy details.

Summary

  • Write the application query first, then choose partition and clustering keys that support its predicates and ordering.
  • Add a month bucket to bound the time span in each partition; watch actual data volume because the bucket does not cap partition size.
  • Pick read and write consistency levels from the replica placement and the intersection your application needs.
  • Plan repair, compaction, and restore checks around the cluster’s workload and failure risks.

Further reading

Category

Related Posts

Operating Apache Cassandra: Repair, Backups, and Compaction

Operate Apache Cassandra 5.0.9 with workload-aware repair and compaction, tested backups, practical monitoring signals, and a safe incident workflow.

#Apache Cassandra #databases #operations

DynamoDB Operations: Capacity, Backups, and Recovery

Choose a DynamoDB capacity mode, diagnose throttling, and plan backup and recovery steps with current AWS guidance for your workload.

#dynamodb #aws #operations

Amazon DynamoDB Data Modeling: Start with Access Patterns

Design DynamoDB tables from concrete access patterns with a worked orders example, a global secondary index, capacity estimates, and a throttling failure.

#dynamodb #databases #nosql