Sub-15ms DynamoDB Scatter-Gather Parallel Queries & Write Sharding in Go
A single Amazon DynamoDB physical partition supports up to 1,000 Write Capacity Units (WCU) and 3,000 Read Capacity Units (RCU). When a flash sale, viral campaign, or high-frequency telemetry stream hammers a single partition key (such as PK = FLASH_SALE#2026_11_02), DynamoDB throttles requests with ProvisionedThroughputExceededException even if your table has plenty of global capacity. As a cloud engineer and backend engineer, I eliminate hot-partition throttling using deterministic write sharding and concurrent scatter-gather queries in Go.
1. Write Sharding Across N Virtual Partitions & Concurrent Scatter-Gather Reads
By appending a shard suffix 0..N-1 to the partition key on write (`PK = EVENT#991#SHARD#3`), write throughput scales linearly to N * 1,000 WCU. To read the aggregated leaderboard or recent activity in under 15ms, our Go service fans out N parallel Query calls using errgroup.Group and merges the sorted results in memory:
// store/sharded_query.go
package store
import (
"context"
"fmt"
"sync"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"golang.org/x/sync/errgroup"
)
const ShardCount = 8
func QueryShardedEventEntries(ctx context.Context, client *dynamodb.Client, tableName, eventID string) ([]map[string]types.AttributeValue, error) {
var (
mu sync.Mutex
results []map[string]types.AttributeValue
)
g, gCtx := errgroup.WithContext(ctx)
for shard := 0; shard < ShardCount; shard++ {
shardID := shard
g.Go(func() error {
pk := fmt.Sprintf("EVENT#%s#SHARD#%d", eventID, shardID)
out, err := client.Query(gCtx, &dynamodb.QueryInput{
TableName: aws.String(tableName),
KeyConditionExpression: aws.String("PK = :pk"),
ExpressionAttributeValues: map[string]types.AttributeValue{
":pk": &types.AttributeValueMemberS{Value: pk},
},
Limit: aws.Int32(25),
ScanIndexForward: aws.Bool(false),
})
if err != nil {
return err
}
mu.Lock()
results = append(results, out.Items...)
mu.Unlock()
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
return results, nil
}
2. Tuning HTTP/2 Connection Pools for AWS SDK Go v2
Default Go HTTP transport settings (`MaxIdleConnsPerHost: 2`) cause connection churn when firing 8–16 concurrent DynamoDB queries per request. Always configure a custom `http.Transport` with `MaxIdleConnsPerHost: 128` on your DynamoDB client.