How to atomically update a counter shared between Docker instances

docker, linux, performance, shared-memory

Solution

It looks like for your case database is overhead. You just need some distribute lightweight key-value storage with shared key lock support. Here are some candidates:

- etcd (https://coreos.com/etcd)

- consul (https://www.consul.io, especially https://www.consul.io/docs/commands/lock.html)

- redis (http://redis.io)

Problem

I have a simple C++ service (API endpoint) that increases a counter every time the API is called. When the caller posts data to http://10.0.0.1/add the counter has to be incremented by 1 and return the value of the counter to the caller. Things get more complicated when the service is getting dockerized. When two instances of the same service run the addition has to be done atomically, ie the counter value is stored in a database and each docker instance has to acquire a lock get the old value, add one, return to the caller and unlock. When the instances are processes in the same Linux machine, we used shared memory to efficiently lock, read, write and unlock the shared data and the performance was accepted. However when we use dockers and a database the performance is low. The results are OK, however the performance is low. What is the canonical way between instances of dockerized properties to perform operations like the one described above? Is there a "shared memory" feature for containerized processes?

Original source