(date unknown but somewhere between 2022-12-07 and 2023-03-06)
Should idempotent operations sometimes be treated as errors? History suggests that it should.
Systems that treat idempotent operations as errors:
- File systems where deleting a file results in an error.
- API servers that return 404 on deleting an entity that doesn’t exist.
INSERT
operations in SQL databases even when the row matches the already existing row.- Double freeing heap memory in C/C++.
- Double deleting an object in C++ (a different case from freeing memory: when an object is deleted, a destructor has to run)
Systems that do not treat idempotent operations as errors:
DELETE
operations in SQL databasesdelete object.property
in JavaScript and similar operations in other langauges (std::map
will not throw if you try to remove a key that doesn’t exist butstd::vector
will throw if you try to remove an item that doesn’t exist)
The answer to my question surely must be “it depends” but what exactly does this depend on? There is always a tension between the view that “programming is about expressing intent” and the view that “programming is about describing exact operations to be correctly executed”. You can see this tension in naming as well - should my function be called synchronizeLocalStateWithRemote
or executeNeededInsertUpdateDeleteOperationsToEqualizeLocalStateWithRemoteOverAuthenticatedEndpoint
? On the other hand should I still call it synchronizeLocalStateWithRemote
if all it does is upload a file to remote?
Clearly we (should) name our entities in a way to make the most common use case the easiest one, the least surprisable, and to offer guidance rather than too many details (then again I have been known to rage against the coddling). We may be forgiven to make a mistake in predicting what is the most common use case but not in failing to try to name things well and define reasonable default behaviors.
Personally, I now feel that we should rarely if ever fail on idempotent operations. “Relax” I think: the system is already in the state that you want it, might as well enjoy it while it lasts.
Last modified on 2022-12-08