How to make write operation idempotent?

Posted by Morgan Cheng on Stack Overflow See other posts from Stack Overflow or by Morgan Cheng
Published on 2010-04-08T06:04:41Z Indexed on 2010/04/22 7:33 UTC
Read the original article Hit count: 223

Filed under:
|
|

I'm reading article about recently release Gizzard sharding framework by twitter(http://engineering.twitter.com/2010/04/introducing-gizzard-framework-for.html). It mentions that all write operations must be idempotent to make sure high reliability.

According to wikipedia, "Idempotent operations are operations that can be applied multiple times without changing the result." But, IMHO, in Gazzard case, idempotent write operation should be operations that sequence doesn't matter.

Now, my question is: How to make write operation idempotent?

The only thing I can image is to have a version number attached to each write. For example, in blog system. Each blog must have a $blog_id and $content. In application level, we always write a blog content like this write($blog_id, $content, $version). The $version is determined to be unique in application level. So, if application first try to set one blog to "Hello world" and second want it to be "Goodbye", the write is idempotent. We have such two write operations:

write($blog_id, "Hello world", 1);
write($blog_id, "Goodbye", 2);

These two operations are supposed to changed two different records in DB. So, no matter how many times and what sequence these two operations executed, the results are same.

This is just my understanding. Please correct me if I'm wrong.

© Stack Overflow or respective owner

Related posts about idempotent

Related posts about scalability