{"record":{"id":"2b48db5c0138a68c","repo":"dotnet/orleans","slug":"key-length-0-is-too-long-to-be-an-dynamodb-parti","errorCode":null,"errorMessage":"Key length {0} is too long to be an DynamoDB partition key. Key={1}","messagePattern":"Key length (.+?) is too long to be an DynamoDB partition key\\. Key=(.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/AWS/Shared/AWSUtils.cs","lineNumber":45,"sourceCode":"        internal static RegionEndpoint GetRegionEndpoint(string zone = \"\")\n        {\n            //\n            // Keep the order from RegionEndpoint so it is easier to maintain.\n            // us-west-2 is the default\n            //\n\n            return RegionEndpoint.GetBySystemName(zone) ?? RegionEndpoint.USWest2;\n        }\n\n        /// <summary>\n        /// Validate DynamoDB PartitionKey.\n        /// </summary>\n        /// <param name=\"key\"></param>\n        /// <returns></returns>\n        public static string ValidateDynamoDBPartitionKey(string key)\n        {\n            if (key.Length >= 2048)\n                throw new ArgumentException(string.Format(\"Key length {0} is too long to be an DynamoDB partition key. Key={1}\", key.Length, key));\n\n            return key;\n        }\n\n        /// <summary>\n        /// Validate DynamoDB RowKey.\n        /// </summary>\n        /// <param name=\"key\"></param>\n        /// <returns></returns>\n        public static string ValidateDynamoDBRowKey(string key)\n        {\n            if (key.Length >= 1024)\n                throw new ArgumentException(string.Format(\"Key length {0} is too long to be an DynamoDB row key. Key={1}\", key.Length, key));\n\n            return key;\n        }\n    }\n}","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/dotnet/orleans/blob/fca799fa70ecb6ad975224271703ca43221f58de/src/AWS/Shared/AWSUtils.cs#L27-L63","documentation":"Thrown by AWSUtils.ValidateDynamoDBPartitionKey when the supplied partition key string is 2048 characters or longer. DynamoDB limits a partition key attribute to 2048 bytes (UTF-8), so this guard rejects oversized keys before they reach the SDK. The message reports both the offending length and the key value.","triggerScenarios":"Produced wherever a DynamoDB partition key is built and validated (clustering, persistence, reminders, transactions). Triggered when MakePartitionKey / grain id / service id / state name concatenation exceeds 2048 chars — e.g., very long grain key strings, long ServiceId, or a compound key with many segments.","commonSituations":"A grain with a long string key (GUID + namespacing); a very long ServiceId or storageProviderName in ClusterOptions; hash-prefix or namespacing schemes that inflate the key; migration that appended extra segments to keys.","solutions":["Shorten the inputs that compose the partition key (grain key, ServiceId, state name).","If a long identity is unavoidable, hash the long key (e.g., SHA-256 hex) and use the hash as the partition key, keeping the original in a secondary attribute.","Review MakePartitionKey overrides / ClusterOptions.ServiceId and remove redundant name-prefixing.","Keep the total composed key well under 2048 bytes to leave headroom for UTF-8 multi-byte characters."],"exampleFix":"// before: raw long identity used as grain key -> long partition key\nvar grain = client.GetGrain<ILongGrain>(veryLongUrl);\n\n// after: hash the long identity to a fixed-width key\nvar key = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(veryLongUrl)));\nvar grain = client.GetGrain<ILongGrain>(key);","handlingStrategy":"validation","validationCode":"string SafePartitionKey(string raw)\n{\n    if (string.IsNullOrEmpty(raw) || raw.Length >= 2048)\n        return Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(raw ?? Guid.NewGuid().ToString())));\n    return raw;\n}","typeGuard":"static bool IsValidPartitionKey(string key) => !string.IsNullOrEmpty(key) && key.Length < 2048;","tryCatchPattern":"try { AWSUtils.ValidateDynamoDBPartitionKey(key); }\ncatch (ArgumentException ax) when (ax.Message.Contains(\"partition key\"))\n{\n    key = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(key)));\n}","preventionTips":["Keep grain keys, ServiceId, and state names short; avoid stuffing long identities into the partition key.","Hash long natural keys to a fixed width before using them as a grain identity.","Audit MakePartitionKey composition when changing ClusterOptions.ServiceId.","Leave headroom under 2048 bytes for multi-byte UTF-8."],"tags":["dynamodb","partition-key","validation","key-size"],"backgroundTag":null,"analyzedSha":"fca799fa70ecb6ad975224271703ca43221f58de","analyzedAt":"2026-08-13T19:55:57.938Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}