Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to deal with race conditions in a RESTful application?

Here is what's happening in my RESTful web app:

  1. HTTP request comes in
  2. The app starts to build a response, with some initial portion of data
  3. Another requests changes the data that where used in step 2
  4. The first request understands that the data are expired

What should it do? Fail the request and return an error to a client? Or it should start from scratch (taking more time than the client expects)?

like image 807
yegor256 Avatar asked Aug 21 '12 15:08

yegor256


1 Answers

IMHO you should treat a REST request very close to how you treat a DB transaction:

  • Either make sure, you lock what to need to lock before doing some real work
  • Or prepare to fail/retry on a concurrency issue

Very often this can actually be handed down to a DB transaction - depending on how much and what non-DB work your request does.

like image 122
Eugen Rieck Avatar answered Nov 12 '22 12:11

Eugen Rieck