What is the maximum primary key size allowed in DynamoDB?

In Amazon DynamoDB, the maximum primary key size depends on whether the table uses a simple primary key (partition key only) or a composite primary key (partition key and sort key), with attribute values allowed up to a maximum length of 2,048 bytes (2 KB) for the partition key and 1,024 bytes (1 KB) for the sort key. This guide explains the exact constraints for primary keys, how data types affect size limits, and best practices for key design in DynamoDB.

DynamoDB Primary Key Size Limits

DynamoDB supports two types of primary keys, each with specific size boundaries:

  • Simple Primary Key (Partition Key): Consists of a single attribute. The maximum size of a partition key value is 2,048 bytes (2 KB). The minimum size is 1 byte.
  • Composite Primary Key (Partition Key + Sort Key): Consists of two attributes. The partition key value can be up to 2,048 bytes, while the sort key value can be up to 1,024 bytes. Combined, the maximum theoretical total size for a composite primary key is 3,072 bytes (3 KB).

Data Types and Key Constraints

Primary key attributes in DynamoDB must be of type String, Number, or Binary. The maximum length calculation depends on the data type selected:

  • String: The size is measured in UTF-8 encoded bytes. A string partition key can contain up to 2,048 UTF-8 bytes, and a string sort key can contain up to 1,024 UTF-8 bytes.
  • Binary: Binary key attributes follow the exact same limits measured in raw bytes (2,048 bytes for partition key, 1,024 bytes for sort key).
  • Number: Numbers are stored with up to 38 digits of precision and take between 1 and 21 bytes depending on the value, remaining well within the key size bounds.

Impact on Item Limits and Partitioning

While the primary key attributes themselves have limits of 2 KB and 1 KB, they count toward the total item size limit in DynamoDB:

  • Total Item Size: A complete DynamoDB item (including attribute names and values) cannot exceed 400 KB.
  • Partition Distribution: DynamoDB uses the partition key value as an input to an internal hash function to distribute items across physical storage partitions. Keeping partition keys concise improves memory usage and throughput efficiency.
  • Index Overhead: If Local Secondary Indexes (LSIs) or Global Secondary Indexes (GSIs) are configured, primary key values are projected into those indexes, making concise key design critical for storage efficiency.