# Composite Keys

A composite key combines two or more attributes into a single key whose full value identifies a record or defines how an index can find it.

Cloud concept. Updated 2026-12-01. Page: https://blowstack.com/tools/cloud-concepts-hub/composite-keys

> A composite key treats multiple attribute values as a tuple rather than as independent keys. It can enforce uniqueness within a scope, such as an order number within a tenant, and support efficient lookups and range queries when the key order matches how the application reads data.

Practice exams covering Composite Keys: [DVA-C03 AWS Certified Developer - Associate](https://blowstack.com/practice-exams/aws-certified-developer-associate).

## 6 key aspects of Composite Keys

A good composite key enforces the intended identity rule, supports frequent access patterns, and balances partition load, index cost, and read freshness.

1. **Uniqueness across the full tuple**: A composite primary key identifies a record by the combination of its component values. Individual values can repeat, so a tenant-scoped identifier need not be globally unique.
2. **Key order and query patterns**: Choose component order to match the equality, range, and ordering requirements of frequent reads. A query on a trailing component alone may not benefit from the same access path.
3. **Partition cardinality and traffic skew**: Check which components determine data placement and measure traffic per partition key value. A highly unique full key does not prevent hot partitions if the routing values are few or unevenly accessed.
4. **Alternate indexes and scan avoidance**: Add a secondary index when recurring queries cannot constrain the existing key efficiently. Compare the reduction in records read with the index's storage and write overhead.
5. **Read consistency by access path**: Key structure controls how records are found, not how fresh the result is. Verify whether the chosen base-table or index operation supports the application's required read consistency.
6. **Stable components and safe encoding**: Use stable identity attributes to avoid costly key changes. If values must be packed into one key field, prevent encoding collisions and test sort behavior against expected range queries.

## Cloud services for Composite Keys

These services apply the same design principle without needing to be used together. DynamoDB and Amazon Keyspaces make the distinction between partition routing and ordering within a partition explicit. Spanner orders rows by primary key across distributed key ranges, so key order shapes both range reads and write distribution. Azure Database for PostgreSQL uses composite keys to enforce relational identity and lets engineers add indexes for alternate lookup patterns. When transferring a design between these services, carry over the uniqueness rule and required queries, but reassess which columns govern placement, which searches can use the index, and what consistency the selected read path provides.

- **[Amazon DynamoDB](https://blowstack.com/tools/cloud-services-hub/amazon-dynamodb)**: A composite primary key consists of a partition key and a sort key. A Query requires partition-key equality and can optionally constrain the sort key; base-table and local secondary index queries can use strongly consistent reads, while global secondary index queries are eventually consistent.
- **[Amazon Keyspaces (for Apache Cassandra)](https://blowstack.com/tools/cloud-services-hub/amazon-keyspaces-(for-apache-cassandra))**: A CQL primary key can combine partition-key columns that determine data placement with clustering columns that order rows within a partition. For example, partitioning by device and day and clustering by timestamp supports time-range reads within a device-day partition.
- **[Azure Database for PostgreSQL](https://blowstack.com/tools/cloud-services-hub/azure-database-for-postgresql)**: PostgreSQL supports multi-column primary keys, foreign keys, and indexes. A primary key such as (tenant_id, order_number) enforces scoped uniqueness, while a separate multi-column index can support a different filter and sort order.

## What is Composite Keys

A composite key is a key made from two or more attributes, such as (tenant\_id, order\_number). If that pair is the primary key, two tenants can use the same order number, and one tenant can have many orders, but the same pair cannot identify two records. A composite secondary index also uses multiple attributes, but it does not enforce uniqueness unless the database supports that constraint and the index is defined as unique. An attribute can be a key component in one index and ordinary record data elsewhere.

Design the key around the application's most important reads and writes. In a store with a partition-and-sort key model, using tenant\_id as the partition key and order\_number as the sort key lets the application fetch one order with both values or query a range of order numbers for a specific tenant. In a relational database, a B-tree index on the same pair typically supports efficient lookups by tenant alone or by tenant and order number. An order-number-only lookup generally needs a different index for predictable performance. The uniqueness rule is the same if the pair is reversed, but the access paths can change substantially.

In a distributed store, evaluate the cardinality and traffic distribution of the partitioning attributes, not just the uniqueness of the full composite key. Millions of unique order numbers do not spread writes if most orders belong to one tenant and the store routes by tenant\_id. A high-cardinality partition key gives the store more values across which to spread traffic, but it does not prevent a popular value from becoming hot. For a very busy tenant, adding a shard suffix to the partitioning value can spread writes, at the cost of querying multiple shards and merging results for a tenant-wide read.

Distinguish a key-based query from a scan with a filter. A key-based query uses the key structure to narrow what the database reads; a scan can read many records and discard nonmatching ones afterward. If an application frequently lists orders by creation time but the primary key uses order number, an alternate index on (tenant\_id, created\_at, order\_number) can support time ranges and provide a deterministic order for records with the same timestamp. That index adds storage and write work. It may also offer different read consistency from the base table: where an index is updated asynchronously, a query can temporarily omit a recent write even when a strongly consistent base-table lookup would see it. Choose a read path that meets the application's freshness requirements as well as its lookup pattern.

Prefer native multi-attribute keys when they fit the access pattern. If a store exposes a single key field and the application encodes several values into it, define a collision-free encoding and verify that its sort order matches the intended queries. For example, plain text places 10 before 2 in lexicographic order, so a numeric range requires an order-preserving representation. Choose stable identity attributes when possible: changing a primary-key component may require moving or rewriting a record, and in a relational design every foreign key that references the composite key must carry all of its components.

## Composite Keys exam questions

Real practice exam questions on Composite Keys, with the correct answer and its explanation.

### Question 1 (DVA-C03)

**Question:**

A document conversion service is designing an Amazon DynamoDB table with one item per conversion job. Workers update job status, and clients retrieve individual jobs by using GetItem. Each request has the job ID, workspace ID, source document ID, and submission hour. The service has millions of workspaces and source documents, but most peak traffic comes from one workspace converting the same source document into different formats. During those peaks, requests are spread roughly evenly across thousands of distinct job IDs, most of which were submitted in the same hour. Which primary key design best reduces the risk of throttling from hot partitions while preserving direct GetItem lookups?

**Correct answer:** Use the job ID as the partition key with no sort key.

**Explanation:** Each job ID uniquely identifies an item, so GetItem needs only that value. DynamoDB hashes the partition key, and peak requests are spread across thousands of job IDs rather than concentrated on one partition key value.

## Practice Composite Keys in a full exam

Questions on Composite Keys in each practice exam, with every answer option and an explanation for each:

- [DVA-C03](https://blowstack.com/practice-exams/aws-certified-developer-associate): 4 questions

## Learn Composite Keys in lessons

2 short lessons on Composite Keys in 1 certification course.

- DVA-C03: Composite Keys: DynamoDB partition and sort keys (Premium), https://app.blowstack.com/lessons/composite-keys-dynamodb-partition-and-sort-keys-dva-c03
- DVA-C03: Data Partitioning vs. Composite Keys in DynamoDB (Premium), https://app.blowstack.com/lessons/data-partitioning-vs-composite-keys-in-dynamodb-dva-c03
