amazon-web-servicesnosqlamazon-dynamodbdynamodb-queriesaws-billing

DynamoDB - Single item vs Multiple item, what is more cost-effective


Approach #1: (Single item)

Single partition key, with all data included in the item, example:

PK: Item#1
object_attribute_1: full json item
object_attribute_2: full json item
object_attribute_3: full json item
object_attribute_4: full json item

Advantages I see: Less write costs, we write entire item at same time, so the round ups are more effective.

Disadvantages: Read costs might be bigger, because there is no option to filter, let's imagine I just need to query object_attribute_2 and not entire item.

Approach #2: (Multiple items)

Partition key + sort key combination, example:

Item#1:
PK: Object#1
SK: object_attribute_1_SK
object_attribute_1: full json item
Item#2:
PK: Object#1
SK: object_attribute_2_SK
object_attribute_2: full json item
Item#3:
PK: Object#1
SK: object_attribute_3_SK
object_attribute_3: full json item
Item#4:
PK: Object#1
SK: object_attribute_4_SK
object_attribute_4: full json item

Advantages I see: Can reduce read costs, in situation we only need specific thing, using SK, instead of returning entire object information, returns only what needed, less KB read

Disadvantages I see: Might increase write costs?

1 - It's 4 writes for single object (does the writes also depend in query transactions or only KB size?)

2 - If the full json item is less than 1KB, let's say 500b, is rounded up to 1kb , or 1.5kb rounded up to 2kb, etc

Which approach you advise me? I'm thinking well?

Thanks a lot


Solution

  • It's 4 writes for single object (does the writes also depend in query transactions or only KB size?)

    DynamoDB has this concept of WCU (Write Capacity Unit). This can be calculated as such:

    1 WCU = 1 write of up to 1 KB/s
    or
    2 WCU = 1 transactional write request up to 1 KB/s
    

    So, if you consume 4 WCU, you either write 4 KB of data per second using non-transactional write operations or 2 KB of data using transactional write operations.

    2 - If the full json item is less than 1KB, let's say 500b, is rounded up to 1kb , or 1.5kb rounded up to 2kb, etc

    The WCU/RCU is rounded up, meaning if you write a json document of 1.5 KB if will cost 2 WCU with non-transactional write and 4 WCU for transactional write.

    Regarding your approaches, you probably would want to use smaller objects and avoid having consistent reads/transactional write if they are not really necessary.

    Also, DynamoDB has Projection Expressions for retrieving parts of a document. Unfortunately, this does not reduces RCU consumption, as far as I know.