ETCD is distributed key value store used as a core component in CoreOS. I’ve already send a post earlier this week. Here is a page describing how to use ETCD basic commands = ETCD API. Code snippets placed in a page mostly use put, but ETCD allows to use post as well. Most of us understand differences between those two commands in a notion of a REST(ful) service, but how does it work in key value store?
Example over many words.
curl -v http://127.0.0.1:2379/v2/keys/test -XPOST -D value=”some value”
curl -v http://127.0.0.1:2379/v2/keys/test/194 -XGET
How to Model Add and Update Method?
My current task is to model and implement repository using ETCD under the hood. Usual repository contains CRUD methods for particular set of entities. Reasonable approach is to separate add from update to do not replace existing object, e.g. when using optimistic locking.
I don’t want to see revision – index – numbers within keys so post command is not useful here. ETCD brings prevExist parameter for this use cases.
I want to perform add method which expect that there is no content on given key. I’ll use following statement:
curl -v http://127.0.0.1:2379/v2/keys/test?prevExist=false -XPUT -D value=”some value”
When you did not delete the key, as I did not, you can get following error:
On the other hand, use false to express update existing entity.
curl -v http://127.0.0.1:2379/v2/keys/test?prevExist=true -XPUT -D value=”some value”
The repository uses put for both add and update methods but value for prevExist is the difference.