Mr. Mars
Mr. Mars

Reputation: 840

Concurrency control in REST APIs

Should a REST API implement concurrency control to control almost simultaneous modifications to a resource?

For example, just imagine an application that acts like a Wiki, where users read, write, and edit articles. Now imagine that user Bob and user Mary modify the same article at the very same time, but Bob updates the article faster than Mary and submits his changes. When Mary submits her changes, she overwrites Bob’s. That’s bad news for Bob because his changes are lost.

What would be the best way to avoid this situation? What techniques are usually used for this?

Upvotes: 0

Views: 1066

Answers (1)

VoiceOfUnreason
VoiceOfUnreason

Reputation: 57194

What would be the best way to avoid this situation? What techniques are usually used for this?

In the general case, conditional requests.

Short version: you combine validators with precondition headers to ensure "first writer wins" semantics. The unsafe request to a resource includes a reference to the expected version. If the resource isn't on that expected version when your request is handled, you get a 412 Precondition Failed response, and then you choose the appropriate remediation.

Specifically for a shared authoring scenario, like the one you described? You might want to look into Conflict-free Replicated Datatypes (CRDT). Dr. Kleppmann's talks are pretty approachable: you might start with this one from Goto 2016; or look for his book Designing Data-Intensive Applications.

Upvotes: 1

Related Questions