Avoid duplicate POSTs with REST

前端 未结 7 1268
一生所求
一生所求 2020-12-02 06:34

I have been using POST in a REST API to create objects. Every once in a while, the server will create the object, but the client will be disconnected before it receives the

7条回答
  •  旧巷少年郎
    2020-12-02 07:21

    Design

    • Automatic (without the need to maintain a manual black list)
    • Memory optimized
    • Disk optimized

    Algorithm [solution 1]

    1. REST arrives with UUID
    2. Web server checks if UUID is in Memory cache black list table (if yes, answer 409)
    3. Server writes the request to DB (if was not filtered by ETS)
    4. DB checks if the UUID is repeated before writing
    5. If yes, answer 409 for the server, and blacklist to Memory Cache and Disk
    6. If not repeated write to DB and answer 200

    Algorithm [solution 2]

    1. REST arrives with UUID
    2. Save the UUID in the Memory Cache table (expire for 30 days)
    3. Web server checks if UUID is in Memory Cache black list table [return HTTP 409]
    4. Server writes the request to DB [return HTTP 200]

    In solution 2, the threshold to create the Memory Cache blacklist is created ONLY in memory, so DB will never be checked for duplicates. The definition of 'duplication' is "any request that comes into a period of time". We also replicate the Memory Cache table on the disk, so we fill it before starting up the server.

    In solution 1, there will be never a duplicate, because we always check in the disk ONLY once before writing, and if it's duplicated, the next roundtrips will be treated by the Memory Cache. This solution is better for Big Query, because requests there are not imdepotents, but it's also less optmized.

    HTTP response code for POST when resource already exists

提交回复
热议问题