ejsmont-artur / php-circuit-breaker

PHP implementation of circuit breaker pattern.
MIT License
169 stars 29 forks source link

Storage does not support atomic operations #1

Open giorgiosironi opened 10 years ago

giorgiosironi commented 10 years ago

The interface for storage: https://github.com/ejsmont-artur/php-circuit-breaker/blob/master/src/Ejsmont/CircuitBreaker/Storage/StorageInterface.php seems not to support atomic operations. Multiple processes on the same machine can interleave like:

1. load value X
2. load value X
1. store value X+1
2. store value X+1

so that X+1 is stored instead of X+2 due to this race condition. Are you aware of this? I was planning to integrate a MongoDB storage but with this interface it won't solve the problem.

sdepablos commented 9 years ago

It would be as easy as to implement a lock that is removed at the end of the operation. The good thing is that working with APC / memcache / Redis / Mongo it would be possible to expire the lock automatically after some time is something goes wrong.

jeff-minard-ck commented 8 years ago

apc, memcache, and redis all support increment/decrement operations which would be a much safer and performant option than custom locks. A quick look says that even mongodb supports some kind of increment too.

giorgiosironi commented 8 years ago

Yes it does, but incrementing a counter makes it difficult to reset the circuit breaker after a certain time, so I use a capped collection containing documents with a created_at field. I insert one for every failure and query for the number of them happened in the last X seconds, opening the circuit breaker if they are more than a threshold.