Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Pratik Ambani
Pratik Ambani

Posted on

     

Explain idempotency (in REST) like I'm Five

Top comments(8)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
andrewbenton profile image
Andrew Benton
  • Joined

Ask someone to turn a switch on, they do, and the switch is on.

Ask them to turn it on again and your command has no effect.

That's idempotency.

CollapseExpand
 
kayis profile image
K
A developer that became a full-time writer, here on dev.to!
  • Location
    Stuttgart, Germany
  • Education
    Computer Science and Media
  • Work
    I wrote "React From Zero" at \newline
  • Joined

Idempotency just means, calling the same function/endpoint multiple times, with the same arguments, will result in the same state.

if you write an endpoint like/player/increaseScoreByOne it will change the state as often as you call it.

if you write an endpoint like/player/setScoreTo?value=100 it will always result in the same state.

When your request doesn't get through, you can safely make it again without worrying about an inconsistent server state.

Not for 5 year olds, but I hope it helps xD

CollapseExpand
 
pratikaambani profile image
Pratik Ambani
Life is POC 😇
  • Work
    Junior Java Developer with 5+ Years of Professional experience
  • Joined

First of all, thank you for answering.

Considering your reply,
Idempotent: GET, TRACE, OPTIONS
Non-Idempotent: DELETE

Where do I place DELETE and PUT, and why?

CollapseExpand
 
kayis profile image
K
A developer that became a full-time writer, here on dev.to!
  • Location
    Stuttgart, Germany
  • Education
    Computer Science and Media
  • Work
    I wrote "React From Zero" at \newline
  • Joined
• Edited on• Edited

You have to think aboutone action when thinking about idempotency.

If you callDELETE 100 times, it will still be the same item that is deleted, you remove it from memory, the DB or set a fielddeleted = true or something. Same goes forPUT, you send an update to the server, but you "set" state to a new state,you don't do stuff like "increase value x", "append string to y" or "subtract 10 from z".

Sure the data will be gone for your nextGET request if you issued 1 or 100DELETEs, but the idea is that the 100DELETEs end in the same state as the 1DELETEs

Thread Thread
 
pratikaambani profile image
Pratik Ambani
Life is POC 😇
  • Work
    Junior Java Developer with 5+ Years of Professional experience
  • Joined

Gotcha!! Thanks buddy :)

CollapseExpand
 
lpasqualis profile image
Lorenzo Pasqualis
I started writing software in 1984. Over the years I worked with many languages, technologies, and tools. I have been in leadership positions since the early 2000s, and in executive roles since 2014.
  • Location
    Seattle, WA
  • Education
    Computer Science
  • Work
    VP of Engineering at DreamBox Learning
  • Joined

The 5-year-old explanation could be:

You can go to your parents and ask the same thing, in the same way, any number of times.
If nothing else changes, the act of asking something over and over will not change the answer. They won't even get annoyed.

:)

CollapseExpand
 
alainvanhout profile image
Alain Van Hout
A software developer with a passion for architecture and an affinity for Java and JavaScript
  • Location
    Antwerp
  • Joined

Very nice metaphor. Perhaps a small addition: You can go to your parents and ask the same thing, in the same way, any number of times. If you’re sure they were listeningat least once, then you can be sure that they will take care of whatever it is you asked.

Notably, the samemight also be true for non-idempotent verbs; the difference is that there you don’t have theguarantee that it will always behave idempotently.

CollapseExpand
 
machkernel profile image
David Stancu
Startup veteran & distributed systems engineer
  • Location
    New York City
  • Joined

A great example of an idempotent function is the absolute value function.abs(-4) = 4abs(4) = 4. Continuing to applyabs() onto itself will always yield the same result.

For those that have done some Elixir before:-4 |> abs |> abs |> ... will always yield4

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Life is POC 😇
  • Work
    Junior Java Developer with 5+ Years of Professional experience
  • Joined

More fromPratik Ambani

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp