FeifanZ
FeifanZ

Reputation: 16316

DynamoDB performance/cost difference between map or separate attributes

Since DynamoDB now supports JSON documents (the map type) and projections into documents, is there a performance or cost difference between storing a map as one attribute vs storing the fields as separate attributes?

For example, I have a table for API access to different sites. Most use a client_id and client_secret, some have an additional field like a server_token, and a few use something else. Is there a difference between storing items with a single map attribute or with multiple attributes?

id | name   | data
———————————————————————————————————————————————————————————————————————
1  | Google | {client_id: XXX, client_secret: XXX}
2  | Uber   | {client_id: XXX, client_secret: XXX, server_token: XXX}

versus

id | name   | client_id | client_secret
————————————————————————————————————————
1  | Google | XXX       | XXX
========================================
id | name | client_id | client_secret | server_token
—————————————————————————————————————————————————————
2  | Uber | XXX       | XXX           | XXX

Upvotes: 5

Views: 1794

Answers (2)

Daniel777
Daniel777

Reputation: 871

Thinking about the extensibility of your model: storing the data as multiple attributes allows you to update them atomically with no hassle, even in a distributed clients scenario.

If you store the data as a map, each time you want to update any value inside that map you have to re-write the entire map. That could be a lock nightmare in case there are many distributed clients attempting to modify different keys the same map at the same time.

Upvotes: 0

Ryan Fitzgerald
Ryan Fitzgerald

Reputation: 829

In terms of performance or cost there shouldn't be any difference between using top level attributes vs nesting them under a data attribute.

However, as of today DyanmoDB does not support creating secondary indexes on nested attributes. Say in the future you needed to query this table by client_id, if you nested that attribute then you wouldn't be able to add a global secondary index on that attribute.

Upvotes: 7

Related Questions