liveBook · Manning

Idempotency

As explained in API Design Patterns

An idempotent method is an API operation whose repeated execution has the same effect as executing it once.

Idempotency means that repeating the same request with identical parameters produces the same effect as executing it once. It supports predictable API behavior, but not every standard method must be idempotent. section 7.3.2 section 7.6

Separate idempotency from side effects. Creating and storing an email belongs to the create operation; sending it through SMTP is an additional side effect that standard methods should avoid. Even a hidden request counter can undermine idempotency when it affects performance or failure behavior. section 7.3.2

Apply these rules when designing resource-oriented methods:

  • Keep repeated requests behaviorally predictable and avoid work that changes unrelated API state. section 7.0 section 7.6
  • Decide whether a method is declarative or imperative before defining repeated-request results. section 7.3.7
  • Document errors for requests whose desired state already holds when the operation is intended to report an action rather than a result. section 25.3.3
  • Use validation-only behavior when full request execution would introduce non-idempotent effects or external dependencies. section 27.3

Deleting a ChatRoom twice

A declarative DELETE request means “ensure this ChatRoom no longer exists.” Repeating it succeeds because the same result remains true. An imperative interpretation means “remove this resource now”; the first request succeeds, while the second fails because there is nothing left to remove. Choose and document the behavior deliberately. Soft deletion adds a recovery path: a custom undelete operation restores a resource, but calling it for a resource that is not soft-deleted returns an error such as 412 Precondition Failed. section 7.3.7 section 25.3.3

Listing 7.5 The standard delete method
abstract class ChatRoomApi {
  @delete("/{id=chatRooms/*}")                       #1
  DeleteChatRoom(req: DeleteChatRoomRequest): void;  #2
}
 
interface DeleteChatRoomRequest {
  id: string;
}

Shows a standard DELETE method targeting a ChatRoom resource by identifier and returning an empty response. section 7.3.7

How API Design Patterns covers it

How deep it goes
Explains repeated-request semantics, side effects, declarative versus imperative designs, and validation trade-offs without implementation internals.
Examples
Uses email creation, SMTP delivery, request counters, ChatRoom deletion, undelete requests, and validation-only queries.
What it assumes
Assumes familiarity with APIs, resources, methods, requests, and errors while defining idempotence and side effects explicitly.
What you can do after
Evaluate repeated-request behavior, identify harmful side effects, and choose declarative or imperative semantics for API methods.
How it connects
Connects idempotency with standard methods, side effects, soft deletion, request validation, and declarative versus imperative APIs.

Also explained in

Compare how 17 books explain idempotency →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of API Design Patterns. The section links point to the parts of the book it draws on.