網頁

2023/4/10

系統設計 遠端資源狀態一致性問題 - 資源已建立但系統收到錯誤

遠端資源是指本地服務外的資源,例如另一個服務的資料,或是雲端資源等。


這邊的狀況比較極端,在遠端資源已建立後並回應成功時網路故障,導致本地應用程式收到錯誤訊息誤以為建立失敗並刪除本地紀錄,因此出現遠端資源存在,但本地紀錄已被刪除的狀況。



plantuml

@startuml
participant client
participant app 
database    db
participant remote
skinparam sequenceMessageAlign center
skinparam ParticipantPadding 50

client ->> app: create request
activate app
app -->> client: accepted
app -> db : CREATE record\nwith state 'building'
activate db
db -->> app : success
deactivate db
|||
app -> remote : CREATE resource
activate remote
remote -[#red]->>x app : success
note right : network failure
deactivate remote
|||
app -> db : DELETE record
activate db
db -->> app : success
deactivate db
app ->> client: failed message
deactivate app
note over db: record deleted, but resource existed
@enduml

或許不應直接刪除本地紀錄,而改成把紀錄狀態標記為「建立失敗(create-failed)」,如此再透過一個排程固定時間(e.g.每日)去再次刪除這些狀態「建立失敗」的遠端資源。遠端刪除的API應該是冪等(idempotent)的,也就是說若先前已經刪除過的id會自動忽略。

至於遠端資源已真正刪除的那些「建立失敗」記錄原本想是否應順勢更新為「以刪除」狀態,但認為還是應留給使用者自己手動刪除。



@startuml
participant client
participant app 
database    db
participant remote
skinparam sequenceMessageAlign center
skinparam ParticipantPadding 50

client ->> app: create request
activate app
app -->> client: accepted
app -> db : CREATE record\nwith state 'building'
activate db
db -->> app : success
deactivate db
|||
app -> remote : CREATE resource
activate remote
remote -[#red]->>x app : success
note right : network failure
deactivate remote
|||
app -> db : UPDATE record state\n to 'deleted'
activate db
db -->> app : success
deactivate db
app ->> client: failed message
deactivate app
note over db: record state is 'deleted', but resource existed
@enduml

沒有留言:

張貼留言