How do you “survive” DDB eventual consistency on delete

I’ve read the DDB book, where all is well for the use cases covered. But there’s not a single word about deletion and consistency in one to many relationships.

Assume I’ve the model of LIST#shopping has many ITEM#eggs ITEM#milk. A user sees a list of his lists, can click on it and see the items. All good. Now there are two parallel API calls:

  1. Delete list LIST#shopping
  2. Add new ITEM#soda to LIST#shopping

My delete process gets all items, then deletes them. The node does not have the replicated data, so has no idea about the new soda item. All known entries are deleted.

I still have the dead entry for soda now in ddb. The user won’t see it, because he has no List to click on.

Is there any book that covers patterns to overcome these issues?

Thanks!

Short answer: the automagic management of consistency concerns you would traditionally expect your RDMBS to handle, you need to handle yourself, manually, in an additional DDB call.

Well you don’t have to get items to delete them.

Or
You mean they are in an array? So get the array, clean it, put it back and at the same time adding soda?

If the later thats just not a good design anywhere, youd read a db book like db for mere mortals for sql or sometimes alike for nossql

Now there are two parallel API calls:

Delete list LIST#shopping

Add new ITEM#soda to LIST#shopping

What you are describing likes a case for strongly consistent reads (as opposed to eventual consistency)

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html

However, they cost more. You may also consider other options, but there was not enough detail in your question for me to formulate something.

You only described the user clicking on a list to see the items present - but then you say there’s a Delete list operation, and an Add new item operation.

How are those operations triggered? You didn’t say.

Sounds like a case for transactions. Only add item if list exists. When you delete list, iterate through and delete linked items afterwards

Your RDBMS likely has some “reader” replicas too - and they may well be asynchronously replicated. Aurora reader endpoints are not read-after-write consistent for example - reads from that endpoint can be stale. If you specify that you want a consistent read from DynamoDB, it will read from the leader replica (where you just made your write) and you will see a view to the data which is inclusive of all committed writes up to the time your read request was received.

You choose to not delete items, or set expiration. You can add details to mark as deleted too. There are a lot of techniques here.

  1. All writes to dynamodb are strongly consistent. Eventual consistency is only an option for reads not writes.
  2. All writes happen sequentially. If you are using Add operation you can specify condition that checks for the existence of the list. This would avoid creating a new list when you add an item.

Assume there’s a rest api
POST /list createList
POST /list/mylist/ addItem
DELETE /list/mylist deleteList

Now anyone could call addItem and deleteList in parallel

If I don’t delete items they are dead entries forever. How can I Mark them as deleted when they are added after delete due to replication?

Why don’t I have to get them? Delete needs PK and SK

Even consistent reads are a snapshot of time. Someone can still add in item just after I list but before delete is done.

This isn’t an eventual consistency problem, it’s the fact that you have two overlapping transactions. Add a lock field to the list, and make anyone who wants to modify it obtain the lock (ideally with a timeout) at the start of their transaction.

I’ve never seen people using a lock on addition to DDB to ensure this. This would be needed for any 1:n relationship every time for each insert.

Hmm, looking at it a bit more, I guess it’s not strictly necessary here. But how is the process that’s trying to add an item to the list operating on a list that’s been deleted? That shouldn’t be possible if you’re using a conditional update. Or is the update happening first but the delete is returning a stale value?

Well, of course. No operation can see into the future to perceive a future state.

However, you haven’t given us enough information to design you a solution.

Assume there’s a rest api POST /list createList POST /list/mylist/ addItem DELETE /list/mylist deleteList

Now anyone could call addItem and deleteList in parallel

OK, again there’s a lack of clarity here. What does “anyone” mean? Are you discussing a single user? It sounds like multiple people could interact with this list. Do multiple people own the list?

What should happen when a list is deleted and then an item is added to it. What should happen when an item is added to a list and then the list deleted. Do you need to maintain an archive of lists, do you need to maintain previous state etc.

In order to design a solution to a complex problem involving multiple users, parallelism etc. we need a LOT more information than you’ve provided.

A user owns this list. No one stops a user to put his credentials into postman and fire away.

Item added to deleted list: either rejected or finally deleted. I don’t care, I just don’t want dead entries in db.

Item added then list deleted: same as above. Ralf should be no item in db anymore.

What information is missing. Operations are defined. Desired end state is defined (no dead entries in db). Happy to provide more info, but seems defined for me

But how do you ensure the list is only deleted if there’s no more items in that list? There’s no transactional condition ensuring that there are no child items