liveBook · Manning

Idempotency

As explained in Amazon Web Services in Action, Third Edition: An in-depth guide to AWS

An idempotent operation produces the same effective result when performed multiple times as when performed once.

An operation is idempotent when performing it multiple times has the same effective outcome as performing it once. This matters when an application retries work after a failure: the first attempt may have succeeded even if the caller did not receive a response. Without idempotency, retrying a blog-post request could create several identical database entries. section 16.2.2 section 16.2.1

Idempotency also helps with SQS message processing. SQS may deliver a message again if it is not deleted within its visibility timeout, or, rarely, if deletion does not remove every copy. A worker must therefore be prepared to process the same message more than once. In a URL-to-image workflow, uploading the result to the same S3 location replaces the existing image, leaving the intended result unchanged. section 14.2.6

For a retryable blog-post request:

  • Have the client generate a UUID and send it with the post’s title and text. Use that UUID as the database primary key. section 16.2.2
  • Insert the post only if its primary key is absent. Alternatively, attempt the insert and treat a duplicate-key error as successful completion; a different database error remains a failure. section 16.2.2
  • Consider every subsequent action separately. The workflow also invalidates a cache and posts a link to a Twitter feed; making the database insert idempotent does not, by itself, establish the same property for the whole workflow. section 16.2.2

Retry when failure may be temporary, such as an unreachable database; retrying invalid request content will not make it valid. Some effects are harder to make idempotent: sending an email on every delivery sends multiple emails. Check whether at-least-once processing fits the application’s requirements rather than assuming retries are harmless. section 16.2.1 section 14.2.6

How Amazon Web Services in Action, Third Edition: An in-depth guide to AWS covers it

How deep it goes
Defines the property and works through duplicate-key handling in a multistep blog-post workflow, without detailing every step’s implementation.
Examples
Uses duplicate blog posts, repeatable S3 image uploads, and repeated emails to distinguish safe retries from unwanted side effects.
What it assumes
Defines idempotence from zero while using database records, requests, and SQS messages as practical context.
What you can do after
Build it from scratch by assigning a client-generated UUID and treating an existing database primary key as a completed insert.
How it connects
Connects retries and fault tolerance to database primary keys, cache invalidation, SQS delivery, and S3 uploads.

Also explained in

Compare how 17 books explain idempotency →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Amazon Web Services in Action, Third Edition: An in-depth guide to AWS. The section links point to the parts of the book it draws on.