accesspatterns.dev

Modelling relationships · Lesson 17 of 20

Keep the total where you read it

A denormalised count on the parent turns an aggregate into a single GetItem.

Next · Single-table designOne index, many questions

Why

How many replies does a thread have? You could read them all and count, but that read grows without bound as the thread fills up. Instead the count lives on the thread's META item as a denormalised attribute.

So the total is a single GetItem, however long the thread runs. When a reply is posted, ADD bumps replyCount atomically on the server, so two replies at once can never lose each other. The trade is honest: the counter and the reply are two writes, so keeping them exactly in step needs a transaction.

Store the aggregate on the parent and bump it with atomic ADD; reading the total is one GetItem, not a query-and-sum that grows.

Forum
Try an example

The thread's META item carries replyCount. The total is one item read, no children touched.

pk(pk)
sk(sk)
title
replyCount
author
body

Run an operation to see the raw engine response.

table Forumkeys pk / skitems 12