如何在 dynamoDB 中實現 50 次寫入的事務? (How can I implement a transaction of 50 writes in dynamoDB?)


問題描述

如何在 dynamoDB 中實現 50 次寫入的事務? (How can I implement a transaction of 50 writes in dynamoDB?)

我知道每筆交易有 25 件商品的硬性限制。但是,我確信有一種方法可以從頭開始為更多項目實施交易。我該怎麼做?

我在想,在每個項目上保留一個版本號。預先獲取所有項目,在插入期間驗證版本號是否相同。即樂觀鎖定。如果條件失敗,則還原所有失敗的項目。自然地,我可以想像還原可能會失敗,我需要對還原進行樂觀鎖定並最終陷入還原死鎖。


參考解法

方法 1:

The solution I found in the end was to implement pessimistic locking. It supports an arbitrary number of writes and reads and guarantees transactional consistency. The catch is if you're not careful, it's easy to run into deadlocks.

The idea is that you

  1. Create a lock table. Each row refers to a specific lock. The primary key of the lock table should be a string which I'll refer to as the lock‑key. Often you'll want to lock a specific entity so this is a reasonable format for the lock‑key {table_name}#{primary_key} but it might be more arbitrary so any string will do. Rows in the lock table should also auto‑delete after a certain time period as per a ttl field ie TimeToLiveSpecification.
  2. Before starting the transaction, acquire the lock. You do this by creating the row with your arbitrary lock‑key and with a conditional check that the row doesn't already exist. If it does exist the row creation should fail which means another process has already acquired the lock. You then need to poll, trying to recreate the lock row until the lock has been released.
  3. Once you have acquired the lock you need to keep the lock alive with a heartbeat to prevent other tasks from executing. The hearbeat process should increment a heartbeat property on the lock row which reflects the last‑active time of the lock. The ttl of the row should be greater than the heartbeat interval. Normally about double, so that the lock is not auto‑purged erroneously. If your process dies, the lock will be naturally released by the auto‑deletion of the ttl.
  4. If your task completes successfully it should delete the lock row freeing it up for other tasks.

(by david_adlerdavid_adler)

參考文件

  1. How can I implement a transaction of 50 writes in dynamoDB? (CC BY‑SA 2.5/3.0/4.0)

#locking #database-locking #optimistic-locking #amazon-dynamodb






相關問題

C# / ASP.NET - Web 應用程序鎖定 (C# / ASP.NET - Web Application locking)

在程序文件夾中創建鎖定文件會導致異常 (Creating Lock file in Programs Folder causes exception)

我什麼時候會使用 AutoResetEvent 和 ManualResetEvent 而不是 Monitor.Wait()/Monitor.Pulse()? (When would I use AutoResetEvent and ManualResetEvent instead of Monitor.Wait()/Monitor.Pulse()?)

鎖定一個 JavaScript 函數 (Lock a JavaScript Function)

當只有一個線程寫入共享變量時,我需要鎖嗎? (Do I need a lock when only a single thread writes to a shared variable?)

為什麼 lock(objLock) 比 lock(this) 好 (Why is it better to lock(objLock) than lock(this))

雙重檢查鎖定的修復有什麼問題? (What's wrong with this fix for double checked locking?)

在表格行上調用 Dibs (Calling Dibs on a table row)

我怎樣才能優雅地寫 lock {}? (how can I write lock {} elegantly?)

Double Check Lock 不能在這個 java 代碼上工作? (Doubly Check Lock Dosen't work on this java code?)

LINQPad / LINQ To SQL - 簡單查詢僅在循環內執行時才會引發內存不足 (LINQPad / LINQ To SQL - Simple Query Throws Out of Memory Only When Executed Inside a Loop)

如何在 dynamoDB 中實現 50 次寫入的事務? (How can I implement a transaction of 50 writes in dynamoDB?)







留言討論