Hey everyone, I have another question. I am using...
# sst
m
Hey everyone, I have another question. I am using dynamoDB in the backend and I want to support functionality where a user can see how many "total" replies exist for a specific post. dynamoDB doesn't provide
count(*)
functionality. So the only other cost effective option is to trigger kinesis stream on each insert in db and use lambda to aggregate the count. Now here are my 2 questions 1. Is this the best approach? 2. Is there an example i can use which can guide me how to setup kinesis stream on dynamodb with a target lambda?
a
Why kinesis?
Maybe you can just trigger eventBridge.
And that can trigger a lambda.
I mean, it’s easier I think.
But I think what you say it’s right.
Is there a way to inspect an specific field in Dynamo?
Well actually in your case you don’t need.
Just trigger event when new record is added.
a
It seems you can use stream on the table directly without other additional services
m
main question is how can i set it up using sst?
m
Are you using a single-table approach in DynamoDB?
r
You can add a stream and lambda consumer to calculate a running total and store as a separate entity. There are some docs on setting this up on the serverless stack website
s
A few ways to do this, if you did put batch, you could make two put requests, one to add the comment, and another to increment a counter on another record. Presumably this could be rolled into your create comment logic, and not require scanning for reply count in the stream. If your replies are a map/array on the comment itself, then even better, just one request with the insert to the array, and an
add
on the counter
j
@Muhammad Ali - why not just have an aws aurora serverless postgres db table for this? Very simple to setup and use using the DataApi We love DynamoDb, but sometimes it just not a good fit.
a
s
I think that DynamoDB Put (insert a new post) + Update (to increment the counter) in a single Transaction should be the way to go for very simple use case.
m
@Slawomir Stec that is the simplest way yes. However There is the case where the update could fail, causing the entire transaction to fail which would roll back the insert. That's less than ideal.
s
and if you use a batch operation, each operation can succeed/fail independently
m
Knowing nothing else about this, if DynamoDB is being used properly and your entities and access patterns are well thought out, I'd use the DynamoDB Stream->Lambda solution and trigger an increment on postCountByID on DynamoDB Stream records that are a
NewImage
for your PostReply entities
s
@Michael Clifford yes, Put+Update can fail, but Put alone can fail too. There is no way you can ignore handling this error and have correct count. Anyway, I think your solution using DynamoDb streams makes sense in most of the cases.
m
@Slawomir Stec agreed, 100%. But in your example, the put should be successful on its own without a dependency on count being updated. The impact of the update failing is less significant and can be retried (DLQ handling for example)
a transaction should only be used if any part of the transaction failing is critical and a single action can't stand on its own
s
@Michael Clifford right, it all depends on the client requirements.
j
As naïeve as a Lambda trigger on Ddb Insert sounds, it is the recommended best practice. I hate this approach because it doesn’t scale well if your table has many triggers then each gets executed. DynamoDB streams should introduce something to apply fine-grained triggers.
r
I would like to see a selective stream trigger - i.e. some filter criteria to apply to the stream but even at scale, the fan-out pattern of triggering a single lambda and have that as a switch to initiate workflows with other services works well