1

While looking for zookeeper, the accepted answer says that concurrent writes are not allowed.
Explaining Apache ZooKeeper

Now my question is as Zookeeper has linear writes, that does not stop me to use Asynchronous APIs to create nodes and take the response in a callback ? Though internally it may not allow concurrent writes , or am I missing something ?

jdk2588
  • 782
  • 1
  • 9
  • 23

1 Answers1

1

Even though zookeeper operates in an ensemble, writes are always served through the leader. Therefore, leader is capable of queuing write requests and completing them sequentially.

Using the asynchronous API will not do any harm to the above mentioned approach. Even though the write requests are asynchronous (from the client side), leader will always make sure that they are served sequentially. Once a asynchronous write request is served, client will be notified through the callback. It is simple as that. Remember, the requests are asynchronous as viewed by the client. But from the leader's point of view, they are served sequentially.

Imesha Sudasingha
  • 3,462
  • 1
  • 23
  • 34
  • Adding to the question, does it work same for reads as well ? – jdk2588 Jun 19 '17 at 05:00
  • No. A read can be served by any node in the ensemble. Since a write is visible only after it has been committed by the leader, this won't be a problem. – Imesha Sudasingha Jun 19 '17 at 05:10
  • So, it is not strong consistency, as client may read stale values ? – jdk2588 Jun 19 '17 at 05:38
  • No. A value which is being written is considered written once it is committed. Leader first update the corresponding value in it's data. Then send the update to followers. Once the majority of the followers respond with success, leader commits the value and send a success response to the client. Therefore, unless there's a network partitioning, zookeeper is consistent. However, in distributed systems we can always achieve two of the CAP(Consistency, availability and partition tolerance) properties. Therefore, as I remember, zookeeper is focusing on availability and partition tolerance. – Imesha Sudasingha Jun 19 '17 at 10:07
  • Adding to above comment, zookeeper is eventually consistent. Therefore zookeeper is one of the best distributed coordination systems you can get. – Imesha Sudasingha Jun 19 '17 at 10:08
  • Yeah, so while leader send update to followers, a client can read from a follower, which breaks "Read your writes" rule, and not making it strongly consistent – jdk2588 Jun 21 '17 at 02:06
  • No. Once the leader commit a value, leader will send a `COMMIT` message to all the followers. Until then, followers will not consider the new value as written. Therefore, clients will not be reading uncommitted values as you mentioned above. Please refer **Phase 3: Broadcast** in [this wiki](https://cwiki.apache.org/confluence/display/ZOOKEEPER/Zab1.0) which explains zookeeper's consensus algorithm. – Imesha Sudasingha Jun 21 '17 at 02:23
  • Is not it what eventual consistency is which will happen "eventually", but meanwhile follower can serve a read request(which may not be updated) ? – jdk2588 Jun 21 '17 at 05:33
  • I though so as well. Then I read again on zookeeper. Zookeeper is providing **consistency and partition tolerance** of CAP properties. I previously mentioned it is availability and partition tolerance which is wrong. Therefore, zookeeper is consistent. You can rely on that – Imesha Sudasingha Jun 21 '17 at 06:24