dotnet/orleans · error · ArgumentException

Key length {0} is too long to be an DynamoDB partition key.

Error message

Key length {0} is too long to be an DynamoDB partition key. Key={1}

What it means

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.

Source

Thrown at src/AWS/Shared/AWSUtils.cs:45

        internal static RegionEndpoint GetRegionEndpoint(string zone = "")
        {
            //
            // Keep the order from RegionEndpoint so it is easier to maintain.
            // us-west-2 is the default
            //

            return RegionEndpoint.GetBySystemName(zone) ?? RegionEndpoint.USWest2;
        }

        /// <summary>
        /// Validate DynamoDB PartitionKey.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string ValidateDynamoDBPartitionKey(string key)
        {
            if (key.Length >= 2048)
                throw new ArgumentException(string.Format("Key length {0} is too long to be an DynamoDB partition key. Key={1}", key.Length, key));

            return key;
        }

        /// <summary>
        /// Validate DynamoDB RowKey.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string ValidateDynamoDBRowKey(string key)
        {
            if (key.Length >= 1024)
                throw new ArgumentException(string.Format("Key length {0} is too long to be an DynamoDB row key. Key={1}", key.Length, key));

            return key;
        }
    }
}

View on GitHub (pinned to fca799fa70)

Solutions

  1. Shorten the inputs that compose the partition key (grain key, ServiceId, state name).
  2. 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.
  3. Review MakePartitionKey overrides / ClusterOptions.ServiceId and remove redundant name-prefixing.
  4. Keep the total composed key well under 2048 bytes to leave headroom for UTF-8 multi-byte characters.

Example fix

// before: raw long identity used as grain key -> long partition key
var grain = client.GetGrain<ILongGrain>(veryLongUrl);

// after: hash the long identity to a fixed-width key
var key = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(veryLongUrl)));
var grain = client.GetGrain<ILongGrain>(key);
Defensive patterns

Strategy: validation

Validate before calling

string SafePartitionKey(string raw)
{
    if (string.IsNullOrEmpty(raw) || raw.Length >= 2048)
        return Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(raw ?? Guid.NewGuid().ToString())));
    return raw;
}

Type guard

static bool IsValidPartitionKey(string key) => !string.IsNullOrEmpty(key) && key.Length < 2048;

Try / catch

try { AWSUtils.ValidateDynamoDBPartitionKey(key); }
catch (ArgumentException ax) when (ax.Message.Contains("partition key"))
{
    key = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(key)));
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/orleans@fca799fa70 (2026-08-13). Data as JSON: /api/errors/2b48db5c0138a68c. Report an issue: GitHub.