Aprendiz Vasya y sus historias sobre idempotencia API

Idempotencia: suena complicado, rara vez hablan de ello, pero esto se aplica a todas las aplicaciones que utilizan la API en su trabajo.


Mi nombre es Denis Isaev, y lidero uno de los grupos de backend en Yandex.Taxi. Hoy compartiré con los lectores de Habr una descripción de los problemas que pueden surgir si no tienen en cuenta la idempotencia de los sistemas distribuidos en mi proyecto. Para hacer esto, elegí el formato de historias de ficción sobre el interno Vasya, que está aprendiendo a trabajar con la API. Será más visual y útil. Vamos


imagen


Acerca de API


API . API POST /v1/orders:


{
  "from": ", .   822",
  "to": " "
}

API , : ? , , . API GET /v1/orders:


{
  "orders": [
    {
      "id": 1,
      "from": ", .   822",
      "to": " "
    }
  ]
}

API :


  1. GET /v1/orders, , UI ;
  2. « » POST /v1/orders ;
  3. .

, , 2 . , . . , , . .



image


8 : , , . , , VPN , . , . : 7 . , , -. : « » , , , , , , .


image


: . .



, « ». , . , , .


, , . , : , . -: , . « » . , , ?


, , . : from to 5 . , 500. , , , : . , . , «», , , . , .



, :


  1. ;
  2. UPDATE active_orders SET n=1 WHERE user_id={user_id} AND n=0;

  3. update 0 , HTTP 409;
  4. ;
  5. .

409 . , , . .



, : «»: ? : , , , ?! , . : 1 2? . , , .


image



, , . API, . : . , API — , API 200, 400. API ( , ), .


, HTTP GET, PUT, DELETE , POST PATCH . , GET , POST . , , , - POST PATCH , GET PUT .


idempotency key API.


. (POST) Idempotency-Key , API. UUID V4. Stripe (POST) Idempotency-Key , API. 24. client tokens AWS.


POST /v1/orders idempotency_key, :


{
  "from": ", .   822",
  "to": " ",
  "idempotency_key": "786706b8-ed80-443a-80f6-ea1fa8cc1b51"
}

UUID v4 . . , . , 409. : 409, 200, , 409.


image



1 2 . :


  1. , , , ;
  2. , - , ;
  3. ;
  4. , 200;
  5. , , .

. , : , .


, , , . , AWS IdempotentParameterMismatch .


: , 5xx . , .


-


- .


1:


  1. , - , ;
  2. , , ;
  3. , GET /v1/orders, , ;
  4. , , ;
  5. , ;
  6. .

2:


  1. , , , ;
  2. , - : ;
  3. : , , , .

, :


  1. 1: . , , .
  2. 2: deleted_at=now() — soft delete. .
  3. 3: , , 24.

: . API GET /v1/orders . , . If-Match , . (, , ). , . () , , « , ». , . , ( ), : , , fingerprint Google Cloud API .



, API . .



, API 404. , API .


DELETE /v1/orders/:id. . soft delete ( deleted_at=now()) .


, . , , 404: . « ».


, API , , — .


200 , DELETE . . soft delete API :


  1. , id;
  2. , n ( , ), 200;
  3. 410 « ». 404 410 , 404 , , . 410 , , .

.



B


, API : , API.


image


B. PATCH /v1/orders/:id:


{
  "to": "  "
}

update :


UPDATE orders SET to={to} WHERE id={id}

— . , / , .



API : , . API , . - . API


UPDATE user_counters SET orders_finished = {orders_finished+1} WHERE user_id={user_id}

, API , 1.


image


: , ? , -, , -, API, .


- :


  1. ;
  2. , , ;
  3. API : API , .

: ? , . .



-, , API . , ?



SMS


: , - SMS. , .


image


SMS: , SMS . , . , ? , consumer . : , , .


: , SMS, email , : . , "at least once delivery" "at most once delivery". , , , .



, , API . , .


. API. . . . - API. - , , , API . , . , .


, , , ? , . : , . . .

Source: https://habr.com/ru/post/442762/


All Articles