rest - Best practices for RESTful API for records with version numbers. Do I use PUT? -
need guidance on best practices building restful api in node.js
let's have person record so:
{ id: 1, name: 'jon', age: 25, recordversion: 1 }
if need increment recordversion every time value gets changed, still use http put update record? i've researched on how put should idempotent , should contain newly-updated representation of original resource, no sure of do.
i increment recordversion property on first put call , send error on second put call same versionnumber of 1 (because have incremented 2 @ point), follow restful api standards?
representation != state
the resources sent on wire representation of state, not actual state.
it's fine remove recordversion , update behind scenes - if that, best remove representation returned resource well. understand why: idempotency happen if applied operation multiple times in row (it isn't guaranteed if other operations happen in between...), , observable side effects.
- put data without version
- the data updated
- version code incremented
- if did get data had put (with no version)
- put same data again without version
- the data updated
- version code incremented
- if did get same data had put (with no version)
idempotent, because resource representation has not changed result of calling put twice, though internal entity state has changed - no observable side effects.
see http://restcookbook.com/http%20methods/idempotency/ bit more detail.
using version codes detect conflicts
as note, use inspect version , throw error if has changed - , in fact restful, , in opinion best way approach put helps avoid (often inexplicable) concurrency errors. if detect case, appropriate return 409 conflict http status code.
how work is:
- put data version (v1)
- the data updated
- version code incremented
- if did get data had put new version (v2) (this side effect, it's ok have side effect first time operation).
- put same data again version (v1)
- conflict detected because
v1 != v2
- 409 conflict returned
- if did get same result of first operation - data put version v2
- conflict detected because
this idempotent, because there have been no observable side effects result of calling operation twice.
the client should, in response 409, get latest version code, , possibly offer user opportunity merge changes whatever else has changed in meantime.
often people confuse idempotency thinking response operation must same result of multiple calls, not case - there being no observable side effects result of multiple sequential calls.
Comments
Post a Comment