Sorting DynamoDB on a Changing Attribute
You put an attribute in the sort key so you can query items in its order, and then the attribute changes: a ticket's status, a task's priority, a deploy's state.
DynamoDB won't let you edit a key attribute in place. Changing a key value moves the item to a new address, and you have to do the move yourself.
Can you change a DynamoDB sort key?
No. UpdateItem rejects any change to a partition or sort key value, and DynamoDB has no
"move item" operation.
You either delete the old item and put a new one, or you keep the changing value on a
GSI sort key, which UpdateItem can change.
- Delete + put in a transaction when the value must live in the base-table key: about 4 write units per change for a 1 KB item.
- A GSI sort key when the value changes often: one
UpdateItemthat sets the attribute and the GSI key together, about 3 write units, and the index catches up asynchronously. - An immutable base key (a ticket id, a creation timestamp) whenever the access pattern allows it.
The problem: a status you sort by keeps changing
Say you run a support desk and want to list a team's tickets ordered by status, so you put status in the sort key:
PK: TEAM#7 SK: STATUS#open#TICKET#8842
Now the ticket moves to pending. An UpdateItem that sets SK to
STATUS#pending#TICKET#8842 fails with ValidationException: "Cannot update attribute
SK. This attribute is part of the key".
A PutItem with the new key does succeed, but it creates a second item. The open copy
stays behind, and the team now sees the ticket twice.
Option 1: delete and recreate in a transaction
If the value must live in the base-table key, changing it means removing the old item and writing the new one:
1. DeleteItem PK=TEAM#7 SK=STATUS#open#TICKET#8842
2. PutItem PK=TEAM#7 SK=STATUS#pending#TICKET#8842 (same attributes)
Send both in one TransactWriteItems so they succeed or
fail together. As two unguarded calls, a crash between them loses the ticket or leaves a
duplicate.
A transaction writes every item twice (prepare, then commit), so a 1 KB ticket costs about 4 write units per change: 2 for the delete and 2 for the put. Any GSI the item appears in adds its own delete and put on top.
Fine for occasional changes, costly for a value that moves all day.
Re-keying many items
The same move works in bulk, for example when you change the key format or the padding width of a zero-padded number.
Page through the collection with Query (or a
parallel scan for the whole table), and for each item
send one TransactWriteItems:
Putof the item under the new key, withConditionExpression: attribute_not_exists(PK)so a rerun never overwrites an item already moved.Deleteof the old key.
Readers see a mix of old and new keys until the run finishes, so switch your queries to the new format only after it completes.
Option 2: keep the changing value off the base key (preferred)
Make the base-table key something immutable (the ticket id) and put the sortable value on
a GSI sort key. GSI1PK and GSI1SK are ordinary attributes on the base item, and GSI1
uses them as its keys:
Base: PK: TICKET#8842 status: "open" teamId: TEAM#7
GSI: GSI1PK: TEAM#7 GSI1SK: STATUS#open#TICKET#8842
GSI1SK is a copy of the status baked into a string, so updating status alone leaves
the ticket at its old position in the index. Set both in the same UpdateItem, which
DynamoDB allows because neither is a base-table key:
await client.send(
new UpdateItemCommand({
TableName: 'Tickets',
Key: {PK: {S: 'TICKET#8842'}},
UpdateExpression: 'SET #status = :s, GSI1SK = :sk',
ExpressionAttributeNames: {'#status': 'status'},
ExpressionAttributeValues: {
':s': {S: 'pending'},
':sk': {S: 'STATUS#pending#TICKET#8842'}
}
})
);The base write is atomic. DynamoDB then moves the index entry for you, asynchronously: it deletes the old GSI entry and puts the new one, so the index can show the old position for a moment after the call returns.
What each option costs
Write units for a 1 KB ticket (write request units on on-demand, write capacity units on provisioned):
| Pattern | API calls | Write units per change |
|---|---|---|
| Transactional delete + put on the base key | TransactWriteItems (2 ops) | ~4 (2 items × 2 for prepare and commit) |
UpdateItem of status + GSI1SK (GSI path) | One UpdateItem | ~3 (base write + GSI delete + GSI put) |
Unlike two unguarded calls, neither path has a window where a crash loses the row. The GSI path is one call instead of a transaction, and the price is eventual consistency on the index read.
When status changes fire many times a minute, model the rate in the pricing calculator.
Sparse GSI for an open-tickets queue
If only open tickets need a status-sorted queue, make it a
sparse index: set GSI1PK = TEAM#7 and
GSI1SK = STATUS#open#... only while the ticket is open.
When it closes, REMOVE GSI1PK, GSI1SK in the same UpdateItem and the item drops out of
the index. Closed tickets you never list then cost no index storage or writes.
Immutable base keys to prefer
| Volatile field | Base-table SK | Better base SK | Volatile field lives on |
|---|---|---|---|
| Deploy status | STATUS#failed#DEPLOY#99 | DEPLOY#99 | GSI sort or attribute |
| Task priority | P#1#TASK#12 | TASK#12 | GSI sort |
| User display name | NAME#alice#USER#5 | USER#5 | Non-key attribute |
Timestamps and immutable ids (CREATED#2026-06-27T10:00:00Z, TICKET#8842) make stable
base sort keys when you need chronological order on the base table itself.
More on choosing them in sort-key strategies.
Pitfalls
- Don't
UpdateItema key attribute. It's rejected. Move the item with delete + put in a transaction, or keep the value on a GSI. - Don't move an item with two unguarded writes. A crash in between loses or duplicates it.
- Update the GSI key, not only the source attribute. A composite
GSI1SKdoesn't followstatuson its own. - Read your own write from the base table. After the
UpdateItem, a strongly consistentGetItemby ticket id shows the new status at once. A GSI query may lag briefly, so a UI that redirects to the status-sorted queue should re-fetch by id when it needs the exact state. GSIs rejectConsistentRead.
Design the keys before coding
Build the key condition for the base read and the GSI read in the expression builder, then emit a paginated query with the query builder for your integration tests.
In DynoTable, you pick which index a query runs through, so you can open the GSI read and the base-table read in two tabs and watch the status sort on the index while the base item keeps its immutable key.

Related: GSI vs LSI and transactions. Download DynoTable to explore your own indexes this way.


