Clojure High Performance Programming Second Edition by Kumar Shantanu

Clojure High Performance Programming Second Edition by Kumar Shantanu

Author:Kumar, Shantanu
Language: eng
Format: epub
Publisher: Packt Publishing
Published: 2015-09-28T16:00:00+00:00


Upping transaction consistency with ensure

Clojure's transactional consistency is a good balance between performance and safety. However, at times, we may need the Serializable consistency in order to preserve the correctness of the transaction. Concretely, in the face of the transaction retries, when a transaction's correctness depends on the state of a ref, in the transaction, wherein the ref is updated simultaneously in another transaction, we have a condition called "write skew". The Wikipedia entry on the write skew, https://en.wikipedia.org/wiki/Snapshot_isolation, describes it well, but let's see a more concrete example. Let's say we want to design a flight simulation system with two engines, and one of the system level constraints is not to switch off both engines at the same time. If we model each engine as a ref, and certain maneuvers do require us to switch off an engine, we must ensure that the other engine is on. We can do it with ensure. Usually, ensure is required when maintaining a consistent relationship (invariants) across the refs is necessary. This cannot be ensured by the validator functions, because they do not come into play until the transaction commits. The validator functions will see the same value hence cannot help.

The write-skew can be solved using the namesake ensure function that essentially prevents a ref from modification by other transactions. It is similar to a locking operation, but in practice, it provides better concurrency than the explicit read-and-update operations, when the retries are expensive. Using ensure is quite simple—(ensure ref-object). However, it may be performance-wise expensive, due to the locks it holds during the transaction. Managing performance with ensure involves a trade-off between the retry latency, and the lost throughput due to the ensured state.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.