Top Banner
’Cause I’m Strong Enough: Reasoning about Consistency Choices in Distributed Systems Alexey Gotsman IMDEA Software Institute, Spain Hongseok Yang University of Oxford, UK Carla Ferreira NOVA LINCS, DI, FCT, Universidade NOVA de Lisboa, Portugal Mahsa Najafzadeh Sorbonne Universit´ es, Inria, UPMC Univ Paris 06, France Marc Shapiro Sorbonne Universit´ es, Inria, UPMC Univ Paris 06, France Abstract Large-scale distributed systems often rely on replicated databases that allow a programmer to request different data consistency guar- antees for different operations, and thereby control their perfor- mance. Using such databases is far from trivial: requesting stronger consistency in too many places may hurt performance, and request- ing it in too few places may violate correctness. To help program- mers in this task, we propose the first proof rule for establishing that a particular choice of consistency guarantees for various oper- ations on a replicated database is enough to ensure the preservation of a given data integrity invariant. Our rule is modular: it allows reasoning about the behaviour of every operation separately under some assumption on the behaviour of other operations. This leads to simple reasoning, which we have automated in an SMT-based tool. We present a nontrivial proof of soundness of our rule and illustrate its use on several examples. Categories and Subject Descriptors D.2.4 [Software Engineer- ing]: Software/Program Verification; F.3.1 [Logics and Meanings of Programs]: Specifying and Verifying and Reasoning about Pro- grams Keywords Replication; causal consistency; integrity invariants 1. Introduction To achieve availability and scalability, many modern distributed systems rely on replicated databases, which maintain multiple replicas of shared data. Clients can access the data at any of the replicas, and these replicas communicate changes to each other using message passing. For example, large-scale Internet services use data replicas in geographically distinct locations, and appli- cations for mobile devices keep replicas locally to support offline Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than the author(s) must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]. POPL’16, January 20–22, 2016, St. Petersburg, FL, USA. Copyright is held by the owner/author(s). Publication rights licensed to ACM. ACM 978-1-4503-3549-2/16/01. . . $15.00. http://dx.doi.org/10.1145/http://dx.doi.org/10.1145/2837614.2837625 use. Ideally, we would like replicated databases to provide strong consistency, i.e., to behave as if a single centralised node handles all operations. However, achieving this ideal usually requires syn- chronisation among replicas, which slows down the database and even makes it unavailable if network connections between replicas fail [2, 24]. For this reason, modern replicated databases often eschew syn- chronisation completely; such databases are commonly dubbed eventually consistent [46]. In these databases, a replica performs an operation requested by a client locally without any synchronisa- tion with other replicas and immediately returns to the client; the effect of the operation is propagated to the other replicas only even- tually. This may lead to anomalies—behaviours deviating from strong consistency. One of them is illustrated in Figure 1(a). Here Alice makes a post while connected to a replica r1, and Bob, also connected to r1, sees the post and comments on it. After each of the two operations, r1 sends a message to the other replicas in the system with the update performed by the user. If the messages with the updates by Alice and Bob arrive to a replica r2 out of order, then Carol, connected to r2, may end up seeing Bob’s comment, but not Alice’s post it pertains to. The consistency model of a repli- cated database restricts the anomalies that it exhibits. For example, the model of causal consistency [32], which we consider in this pa- per, disallows the anomaly in Figure 1(a), yet can be implemented without any synchronisation. The model ensures that all replicas in the system see causally dependent events, such as the posts by Al- ice and Bob, in the order in which they happened. However, causal consistency allows different replicas to see causally independent events as occurring in different orders. This is illustrated in Fig- ure 1(b), where Alice and Bob concurrently make posts at r1 and r2. Carol, connected to r3 initially sees Alice’s post, but not Bob’s, and Dave, connected to r4, sees Bob’s post, but not Alice’s. This outcome cannot be obtained by executing the operations in any to- tal order and, hence, deviates from strong consistency. Such anomalies related to the ordering of actions are often ac- ceptable for applications. What is not acceptable is to violate cru- cial well-formedness properties of application data, called integrity invariants. Consistency models that do not require any synchroni- sation are often too weak to ensure these. For example, consider a toy banking application where the database stores the balance of a single account that clients can make deposits to and withdrawals from. In this case, an integrity invariant may require the account balance to be always non-negative. Consider the database compu-
17

’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

Aug 31, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

’Cause I’m Strong Enough:Reasoning about Consistency Choices in Distributed Systems

Alexey GotsmanIMDEA Software Institute, Spain

Hongseok YangUniversity of Oxford, UK

Carla FerreiraNOVA LINCS, DI, FCT,

Universidade NOVA de Lisboa, Portugal

Mahsa NajafzadehSorbonne Universites, Inria,

UPMC Univ Paris 06, France

Marc ShapiroSorbonne Universites, Inria,

UPMC Univ Paris 06, France

AbstractLarge-scale distributed systems often rely on replicated databasesthat allow a programmer to request different data consistency guar-antees for different operations, and thereby control their perfor-mance. Using such databases is far from trivial: requesting strongerconsistency in too many places may hurt performance, and request-ing it in too few places may violate correctness. To help program-mers in this task, we propose the first proof rule for establishingthat a particular choice of consistency guarantees for various oper-ations on a replicated database is enough to ensure the preservationof a given data integrity invariant. Our rule is modular: it allowsreasoning about the behaviour of every operation separately undersome assumption on the behaviour of other operations. This leadsto simple reasoning, which we have automated in an SMT-basedtool. We present a nontrivial proof of soundness of our rule andillustrate its use on several examples.

Categories and Subject Descriptors D.2.4 [Software Engineer-ing]: Software/Program Verification; F.3.1 [Logics and Meaningsof Programs]: Specifying and Verifying and Reasoning about Pro-grams

Keywords Replication; causal consistency; integrity invariants

1. IntroductionTo achieve availability and scalability, many modern distributedsystems rely on replicated databases, which maintain multiplereplicas of shared data. Clients can access the data at any of thereplicas, and these replicas communicate changes to each otherusing message passing. For example, large-scale Internet servicesuse data replicas in geographically distinct locations, and appli-cations for mobile devices keep replicas locally to support offline

Permission to make digital or hard copies of all or part of this work for personal orclassroom use is granted without fee provided that copies are not made or distributedfor profit or commercial advantage and that copies bear this notice and the full citationon the first page. Copyrights for components of this work owned by others than theauthor(s) must be honored. Abstracting with credit is permitted. To copy otherwise, orrepublish, to post on servers or to redistribute to lists, requires prior specific permissionand/or a fee. Request permissions from [email protected]’16, January 20–22, 2016, St. Petersburg, FL, USA.Copyright is held by the owner/author(s). Publication rights licensed to ACM.ACM 978-1-4503-3549-2/16/01. . . $15.00.http://dx.doi.org/10.1145/http://dx.doi.org/10.1145/2837614.2837625

use. Ideally, we would like replicated databases to provide strongconsistency, i.e., to behave as if a single centralised node handlesall operations. However, achieving this ideal usually requires syn-chronisation among replicas, which slows down the database andeven makes it unavailable if network connections between replicasfail [2, 24].

For this reason, modern replicated databases often eschew syn-chronisation completely; such databases are commonly dubbedeventually consistent [46]. In these databases, a replica performsan operation requested by a client locally without any synchronisa-tion with other replicas and immediately returns to the client; theeffect of the operation is propagated to the other replicas only even-tually. This may lead to anomalies—behaviours deviating fromstrong consistency. One of them is illustrated in Figure 1(a). HereAlice makes a post while connected to a replica r1, and Bob, alsoconnected to r1, sees the post and comments on it. After each ofthe two operations, r1 sends a message to the other replicas in thesystem with the update performed by the user. If the messages withthe updates by Alice and Bob arrive to a replica r2 out of order,then Carol, connected to r2, may end up seeing Bob’s comment,but not Alice’s post it pertains to. The consistency model of a repli-cated database restricts the anomalies that it exhibits. For example,the model of causal consistency [32], which we consider in this pa-per, disallows the anomaly in Figure 1(a), yet can be implementedwithout any synchronisation. The model ensures that all replicas inthe system see causally dependent events, such as the posts by Al-ice and Bob, in the order in which they happened. However, causalconsistency allows different replicas to see causally independentevents as occurring in different orders. This is illustrated in Fig-ure 1(b), where Alice and Bob concurrently make posts at r1 andr2. Carol, connected to r3 initially sees Alice’s post, but not Bob’s,and Dave, connected to r4, sees Bob’s post, but not Alice’s. Thisoutcome cannot be obtained by executing the operations in any to-tal order and, hence, deviates from strong consistency.

Such anomalies related to the ordering of actions are often ac-ceptable for applications. What is not acceptable is to violate cru-cial well-formedness properties of application data, called integrityinvariants. Consistency models that do not require any synchroni-sation are often too weak to ensure these. For example, consider atoy banking application where the database stores the balance of asingle account that clients can make deposits to and withdrawalsfrom. In this case, an integrity invariant may require the accountbalance to be always non-negative. Consider the database compu-

Page 2: ’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

add(post)

r1 r2

add(comment)

query:comment

(a)

add(postA)r1 r2 r3 r4

query: postA

add(postB)

query: postB

(b)

withdraw(100):

r1 r2

query: -100query: -100

σinit = 100(c)

withdraw(100):

Figure 1. Illustrations of replicated database computations.

add(post)

add(comment) query:post, comment

(a)

add(postA)

query: postA

add(postB)

(b)

query: postB

withdraw(100):τ,

query: 0query: 0

σinit = 100, τ ⋈ τ(c)

withdraw(100):τ,

Figure 2. Examples illustrating Definition 1. We omit return values when they are ⊥ and token sets when they are empty.

tation in Figure 1(c), allowed by causal consistency. Initially allreplicas store the same balance of 100. Alice and Bob, connectedto r1 and r2, both withdraw 100, thinking that there are sufficientfunds available. Once the two replicas exchange the updates, thebalance becomes −100, violating the integrity invariant. To ensurethe integrity invariant in this example, we have to introduce syn-chronisation between replicas, and, since synchronisation is expen-sive, we would like to introduce it sparingly. To allow this, someresearch [9, 31, 41, 43] and commercial [6, 10, 34] databases nowprovide hybrid consistency models that allow the programmer torequest stronger consistency for certain operations and thereby in-troduce synchronisation. For example, a consistency model mayexecute some operations under causal consistency, and some understrong consistency [31]. To preserve the integrity invariant in ourbanking application when using this model, only withdrawal op-erations need to use strong consistency, and hence, synchronise toensure that the account is not overdrawn; deposit operations mayuse causal consistency and hence proceed without synchronisation.Requesting stronger consistency in hybrid models is similar to theuse of fences in weak memory models of shared-memory multipro-cessors and programming languages [11] (see §7 for a comparison).

Even though hybrid consistency models allow the programmerto fine-tune consistency level, using these models effectively is farfrom trivial. Requesting stronger consistency in too many placesmay hurt performance and availability, and requesting it in toofew places may violate correctness. Striking the right balance re-quires the programmer to reason about the application behaviouron the subtle semantics of the consistency model, taking into ac-count which anomalies are disallowed by a particular consistencystrengthening and whether disallowing these anomalies is enoughto ensure correctness. This difficulty is compounded by the peren-nial challenge of reasoning about concurrency, present even withstrong consistency—having to consider the huge number of possi-ble interactions between concurrently executing operations.

To help programmers exploit hybrid consistency models, wepropose the first proof rule and tool for proving integrity invariantsof applications using replicated databases with a range of hybridmodels. In more detail, our first contribution is a generic hybridconsistency model (§2) that is flexible enough to encode a varietyof consistency models for replicated databases proposed in the lit-erature [9, 31, 32, 41]. It guarantees causal consistency by defaultand allows the programmer to additionally specify which pairs ofoperations may not execute without synchronisation by means of a

special conflict relation. For example, to ensure the non-negativityof balances in the banking application, the conflict relation may re-quire any pair of withdrawals to synchronise, so that one of themis aware of the effect of the other. This is equivalent to execut-ing withdrawals under strong consistency. In general, different in-stances of the conflict relation correspond to different interfacesfor strengthening consistency proposed in the literature. Our proofrule is developed for the generic consistency model and, hence,applies to existing models that can be represented as its instanti-ations. We specify our consistency model formally (§3) using theapproach previously proposed for specifying variants of eventualconsistency [15]. In this approach, a database computation is de-noted by a partial order on client operations, representing causality,and the conflict relation imposes additional constraints on this or-der.

Our next, and key, technical contribution is a proof rule forshowing that a set of operations preserves a given integrity invari-ant when executed on our consistency model with a given choice ofconflict relation (§4). For example, we can prove that withdrawalsand deposits preserve the non-negativity of balances when executedwith the conflict relation described above. To avoid explicit reason-ing about all possible interactions between operations, our proofrule is modular: it allows us to reason about the behaviour of everyoperation separately under some assumption on the behaviour ofother operations, which takes into account the conflict relation. Inthis way, our proof rule allows the programmer to reason preciselyabout how strengthening or weakening consistency of certain oper-ations affects correctness.

The modular nature of our proof rule allows it to reason interms of states of a single database copy, just like in proof rules forstrongly consistent shared-memory concurrency. We have provedthat this simple reasoning is sound, despite the weakness of theconsistency model (§5). As part of this proof we have identifieda more general event-based rule that reasons directly in termsof partial orders on events representing database computations,instead of database states that these events lead to. The soundnessof the original state-based rule is proved by deriving it from theevent-based one. In this way, the event-based rule explicates thereasons for the soundness of the state-based rule.

We have also developed a prototype tool that automates ourproof rule by reducing checking its obligations to SMT queries(§6). Using the tool, we have verified several example applicationsthat require strengthening consistency in nontrivial ways. These

Page 3: ’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

include an extension of the above banking application, an onlineauction service and a course registration system. In particular, wewere able to handle applications using replicated data types (akaCRDTs [39]), which encapsulate policies for automatically merg-ing the effects of operations performed without synchronisation atdifferent replicas. The fact that we can reduce checking the correct-ness properties of complex computations in our examples to query-ing off-the-shelf SMT tools demonstrates the simplicity of reason-ing required by our approach.

2. Consistency Model, InformallyWe start by presenting our generic consistency model. Even thoughthis model is not implemented in its full generality by an existingdatabase, it can encode a variety of models that have in fact beenimplemented. In this section we present the programming interfaceof our consistency model and describe its semantics informally,from an operational perspective. We give a formal semantics in §3.

2.1 Causal Consistency and Its ImplementationOur hybrid model guarantees at least causal consistency [32], al-ready mentioned in §1. We therefore start by presenting informallyhow a typical implementation of a causally consistent database op-erates. Let State be the set of possible states of the data managedby the database system. We denote states by σ and let σinit be adistinguished initial state. Applications define a set of operationsOp = o, . . . on the data and interact with the database by issu-ing these operations. For simplicity, we assume that an operationalways terminates and returns a single value from a set Val. We usea value ⊥ ∈ Val to model operations that return no value. We donot consider operation parameters, since these can be part of theoperation name.

The database implementation consists of a set of replicas, eachmaintaining a complete copy of the database state; we identifyreplicas by r1, r2, . . . For the purposes of the informal explanation,we assume that replicas never fail. A client operation is initially ex-ecuted at a single replica, which we refer to as its origin replica. Atthis replica, the execution of the operation is not interleaved withthat of others. This execution updates the replica state determinis-tically, and immediately returns a value to the client. After this, thereplica sends a message to all other replicas containing the effectof the operation, which describes the updates done by the opera-tion to the database state. The replicas are guaranteed to receive themessage at most once. Upon receipt, the replicas apply the effect totheir state.

In this paper, we abstract from a particular language in whichoperations may be written and assume that their semantics is givenby a function

F ∈ Op→ (State→ (Val× (State→ State))). (1)

To aid readability, for o ∈ Op we write Fo instead of F(o) and let

∀o, σ. Fo(σ) = (F valo (σ),F eff

o (σ)).

Given a state σ of o’s origin replica, F valo (σ) ∈ Val determines the

return value of the operation and F effo (σ) ∈ State → State its

effect. The latter is a function, to be applied by every replica to itsstate to incorporate the operation’s effect: immediately at the originreplica, and after receiving the corresponding message at all otherreplicas.

For example, states in the toy banking application of §1 areintegers, representing the account balance: State = Z. We define

deposit(20)

r1 r2

interest

query:125

query: 125

(a)

deposit(20)

interest

query: 125

query: 125

(b)

Figure 3. (a) An illustration of a database computation; (b) thecorresponding execution of Definition 1. We assume σinit = 100.

the semantics of operations for depositing an amount a > 0,accruing a 5% interest and querying the balance:

Fdeposit(a)(σ) = (⊥, (λσ′. σ′ + a));

Finterest(σ) = (⊥, (λσ′. σ′ + 0.05 ∗ σ));

Fquery(σ) = (σ, skip),

(2)

where skip = (λσ′. σ′). Figure 3(a) illustrates a database compu-tation involving these operations. Note that interest first computesthe interest 0.05 ∗σ based on the balance σ at the origin replica; itseffect then adds the resulting amount to the balance at each replica.In particular, in Figure 3(a) interest at r2 does not take into accountthe deposit made at r1. This behaviour is the price to pay for avoid-ing synchronisation between replicas. The good news is that, oncethe replicas r1 and r2 exchange the effects of deposit and interest,they converge to the same balance, which is returned by the queryoperations.

Such convergence is not guaranteed for arbitrary operations. Forexample, we could implement interest so that its effect multipliedthe balance by 1.05 at each replica where it is applied:

F effinterest(σ) = (λσ′. (1.05 ∗ σ′)). (3)

In the scenario in Figure 3(a), this would lead the query operationsto return different values, 126 at r1 and 125 at r2. In this case,even after all messages are delivered, replicas end up in differentstates. This is undesirable for database users: we would like theimplementation to be convergent, i.e., such that two replicas thatsee the same set of operations are in the same state. In particular,if users stop performing updates to the database, then once alloutstanding messages are delivered, all replicas should reach thesame state [46]. To ensure convergence, for now we require that theeffects of all operations commute (we relax this condition slightlyin §2.2):

∀o1, o2, σ1, σ2. F effo1 (σ1) F eff

o2 (σ2) = F effo2 (σ2) F eff

o1 (σ1). (4)

For example, this condition holds of the effects defined by (2). Therequirement of commutativity is not very taxing: as we elaboratein §6, to satisfy (4), programmers can exploit ready-made repli-cated data types (aka CRDTs [39]). These encapsulate commuta-tive implementations of policies for merging concurrent updates tothe database.

As we explained in §1, asynchronous operation processing maylead to anomalies, and causal consistency disallows some of them.It ensures that message propagation between replicas is causal: if areplica sends a message containing the effect of an operation o2

after it sends or receives a message containing the effect of anoperation o1, then no replica will receive the message about o2

before it receives the one about o1. In this case we say that theinvocation of o2 causally depends on that of o1. Causal propagationdisallows the computation in Figure 1(a), but allows the one inFigure 1(b).

Page 4: ’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

Token = τ./ = (τ, τ)

Fdeposit(a)(σ) = (⊥, (λσ′. σ′ + a), ∅)Finterest(σ) = (⊥, (λσ′. σ′ + 0.05 ∗ σ), ∅)Fquery(σ) = (σ, skip, ∅)

Fwithdraw(a)(σ) = if σ ≥ a then (X, (λσ′. σ′ − a), τ)else (7, skip, τ)

Figure 4. Operation semantics for the banking application. Notethat a > 0.

2.2 Strengthening ConsistencyThe guarantees provided by causal consistency are too weak toensure certain integrity invariants. For example, in our bankingapplication we would like the state at each replica to satisfy theinvariant

I = σ | σ ≥ 0. (5)To ensure this, an operation for withdrawing an amount a > 0could check whether the account has sufficient funds and return Xor 7 depending on the result:

Fwithdraw(a)(σ) = if σ ≥ a then (X, (λσ′. σ′ − a)) else (7, skip).

This is enough to maintain the invariant when all operations areprocessed at the same replica, but not when they are processedasynchronously at different replicas. This is illustrated by the com-putation in Figure 1(c), already explained in §1.

The problem in this example arises because two particular op-erations update the database concurrently, without being aware ofeach other. To address this, our consistency model allows the pro-grammer to strengthen causal consistency by specifying explic-itly which operations may not be executed in this way. Namely,the model is parameterised by a token system T = (Token, ./),consisting of a set of tokens Token and a symmetric conflict re-lation ./ ⊆ Token × Token. Tokens are ranged over by τ andtheir sets, by T . For sets T1 and T2 of tokens we let T1 ./ T2

if there exists a pair of conflicting tokens coming from these sets:∃τ1 ∈ T1.∃τ2 ∈ T2. τ1 ./ τ2.

Each operation may acquire a set of tokens. To account for this,we redefine the type of F in (1) as

F ∈ Op→ (State→ (Val× (State→ State)× P(Token)))(6)

and let

∀o, σ.Fo(σ) = (F valo (σ),F eff

o (σ),F toko (σ)).

Thus, F toko (σ) ∈ P(Token) gives the set of tokens acquired by

the operation o when executed in the state σ. Informally, our con-sistency model guarantees that operations that acquire tokens con-flicting according to ./ have to be causally dependent one way oranother: the origin replica of one operation must have incorpo-rated the effect of the other by the time the former operation ex-ecutes. Ensuring this in implementations requires replicas to syn-chronise [9, 31].

In our consistency model, we can guarantee the preservation ofinvariant (5) in the banking application by defining operation se-mantics as in Figure 4. Thus, withdraw acquires a token τ con-flicting with itself, and all other operations do not acquire any to-kens. Then the scenario in Figure 1(c) cannot happen: one with-drawal would have to be aware of the other and would thereforefail. However, deposits and interest accruals can be causally inde-pendent with all operations, and replicas can therefore execute themwithout any synchronisation [9, 31]. In this example, the token τ is

analogous to a mutual exclusion lock in shared-memory concur-rency. Our proof method (§4) establishes that this use of the tokenis indeed sufficient to preserve the integrity invariant (5).

Since operations acquiring conflicting tokens have to becausally dependent, causal message propagation (§2.1) ensures thatall replicas see such operations in the same order. This allows us toweaken (4) to require commutativity only for operations that do notacquire conflicting tokens:

∀o1, o2, σ1, σ2. (F toko1 (σ1) ./ F tok

o2 (σ2)) ∨(F eff

o1 (σ1) F effo2 (σ2) = F eff

o2 (σ2) F effo1 (σ1)). (7)

As we show in §3, this is sufficient to ensure the property of con-vergence that we introduced in §2.1. For example, the operationsin Figure 4 satisfy (7). Furthermore, if all operations except queryacquired the token τ , then we would be able to implement interestby the effect given by (3) without compromising convergence.

3. Formal SemanticsWe now formally define the semantics of our consistency model,i.e., the set of all client-database interactions it allows. To keepthe presentation as simple as possible, we define the semanticsdeclaratively: our formalism does not refer to implementation-levelconcepts, such as replicas or messages, even though we do usethese concepts in informal explanations. We build on an approachpreviously used to specify forms of eventual consistency [15].Namely, our denotations of database computations consist of aset of events, representing operation invocations by clients, and arelation on events, describing abstractly how the database processesthe corresponding operations.

Assume a countably infinite set Event of events, ranged overby e, f, g. A relation is a strict partial order if it is transitive andirreflexive. For a relation R we write (e, f) ∈ R and e R−→ finterchangeably.

DEFINITION 1. Given a token system T = (Token, ./), an execu-tion is a tuple X = (E, oper, rval, tok, hb), where:

• E is a finite subset of Event;• oper : E → Op gives the operation whose invocation a given

event denotes;• rval : E → Val gives the return value of the operation;• tok : E → P(Token) gives the set of tokens acquired by the

operation;• hb ⊆ E × E, called happens-before, is a strict partial order

such that

∀e, f ∈ E. tok(e) ./ tok(f) =⇒ (ehb−→ f ∨ f hb−→ e). (8)

Operationally, each event represents an invocation of an opera-tion at its origin replica. The applications of the operation’s effect atother replicas are not recorded in an execution explicitly. Instead,the happens-before relation records causal dependencies betweenoperations arising from such applications: e hb−→ f means that ei-ther the operations denoted by e and f were executed at the samereplica in this order, or they were executed at different replicas andthe message containing the effect of e had been delivered to thereplica performing f before f was executed. Hence, if we havee

hb−→ f , then the effect of e is incorporated into the state to whichf is applied and may influence its return value. We give examples ofexecutions in Figures 2 and 3(b). The ones in Figures 2(b) and 3(b)model the computations of the database informally illustrated inFigures 1(b) and 3(a), respectively.

The transitivity of hb in Definition 1 reflects the guaranteeof causal message propagation in implementations explained in

Page 5: ’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

§2.1 [15]. For example, in the execution of Figure 2(a), the tran-sitivity of hb mandates the edge between the addition of a postand the query (cf. Figure 1(a)). The condition (8) formalises thestronger consistency guarantee provided by tokens: operations ac-quiring conflicting tokens have to be causally dependent. For ex-ample, since the two withdraw operations in Figure 2(c) acquire atoken τ with τ ./ τ , they have to be related by happens-before.Finally, we require executions to contain only finitely many events,because in this paper we are only concerned with safety propertiesof applications.

We write Exec(T ) for the set of all executions over the tokensystem T . In the following, we denote components of X andsimilar structures as in X.E. We let Xinit be the unique executionwith Xinit.E = ∅.

We now define the semantics of our consistency model as theset of all executions X ∈ Exec(T ) over a token system T whosereturn valuesX.rval and token setsX.tok are computed usingF asinformally described in §2. To define this set, we first let the contextof an event e in an execution X be

ctxt(e,X) = (E, (X.oper)|E , (X.rval)|E , (X.tok)|E , (X.hb)|E),

where E = (X.hb)−1(e) and ·|E is the restriction to events in E.Operationally-speaking, the context consists of those events whoseeffects have been incorporated into the state of the replica wherethe operation X.oper(e) executes; it is these events that influencethe outcomes of e—the return value X.rval(e) and the token setX.tok(e). For example, the context of each of the query events inFigure 3(b) consists of the deposit and interest events. This reflectsthe events that the corresponding replica has seen before executingquery in Figure 3(a).

It is technically convenient for us to initially formulatedefinitions without assuming effect commutativity (7). In thiscase, X.rval(e) and X.tok(e) are not determined by ctxt(e,X)uniquely. In operational terms, this is because the state that a replicawill be in after seeing the events in ctxt(e,X) depends on the orderin which the replica finds out about these events: although causalmessage propagation ensures that messages about causally depen-dent events in ctxt(e,X) will be delivered to the replica in the orderconsistent with X.hb, messages about causally independent eventsmay be delivered in arbitrary order. We therefore first define a func-tion

eval†F : Exec(T )→ P(State)

that yields the set of all possible states that a replica may end upin after seeing the events in a given execution, such as ctxt(e,X).For an execution Y , we define eval†F (Y ) inductively on the size ofY.E. If Y.E = ∅, then eval†F (Y ) = σinit. Otherwise,

eval†F (Y ) = F effY.oper(e)(σ

′)(σ) | e ∈ max(Y ) ∧σ ∈ eval†F (Y |Y.E−e) ∧ σ′ ∈ eval†F (ctxt(e, Y )),

where

max(Y ) = e ∈ Y.E | ¬∃f ∈ Y.E. (e, f) ∈ Y.hb. (9)

Thus, to compute eval†F (Y ) for a non-empty Y , we choose an hb-maximal event e in Y . Operationally, this is the event whose effectis incorporated last by the replica r whose state we are determining.We then pick a state σ that r could be in right before incorporatingthe effect of e. The set of such states is obtained by invoking eval†Fon the execution Y |Y.E−e, describing the events r knew aboutwhen it incorporated e. To determine the effect of e’s operation,we pick a state σ′ that the replica r′ that generated e could be inat the time of this generation. The set of such states is computedby invoking eval†F on the execution ctxt(e, Y ), describing theevents that replica r′ knew about when it generated e. Then theeffect of e’s operation is F eff

Y.oper(e)(σ′), and we determine the

state of the replica r after e by applying this effect to the state σ:F effY.oper(e)(σ

′)(σ).To illustrate eval†F , consider the execution Y consisting of the

deposit and interest events in Figure 3(b) and the operation se-mantics F in Figure 4. Recall that in this case σinit = 100. Wecan evaluate Y in two ways, corresponding to the orders in whichreplicas r1, respectively r2, apply the effects of the events in thecomputation in Figure 3(a):

eval†F (Y ) = F effinterest(σinit)(F eff

deposit(20)(σinit)(σinit)),

F effdeposit(20)(σinit)(F eff

interest(σinit)(σinit))= 100 + 20 + 5, 100 + 5 + 20 = 125.

Both ways of evaluation lead to the same outcome. This would notbe the case if we used a function F ′ identical to F , but with theeffect of interest defined by (3), which violates (7). In this case,

eval†F′(Y ) = 100 + 20 + 6, 100 + 5 + 20 = 126, 125,which corresponds to the diverging database computation we ex-plained in §2.1.

We note that, for notational convenience, eval†F takes as a pa-rameter a whole execution including return values (rval) and tokensets (tok) associated with its events. However, the function as wedefined it does not depend on these: the state is determined solelybased on the operations performed (oper) and happens-before rela-tionships among them (hb).

DEFINITION 2. An execution X ∈ Exec(T ) is consistent with Tand F , denoted X |= T ,F , if

∀e ∈ X.E. ∃σ ∈ eval†F (ctxt(e,X)).

(X.rval(e) = F valX.oper(e)(σ)) ∧ (X.tok(e) = F tok

X.oper(e)(σ)).

We let Exec(T ,F) = X | X |= T ,F be the set of executionsallowed by our consistency model.

PROPOSITION 3.

∀X ∈ Exec(T ,F). ∀e ∈ X.E. (ctxt(e,X) ∈ Exec(T ,F)).

Operationally,X |= T ,F means that the outcomes inX can beproduced by the database implementation sketched in §2 with someorder of message delivery. The executions in Figures 2 and 3(b)are consistent with the parameters in Figure 4 or the expectedsemantics of operations on posts and comments. In particular, theexecution in Figure 2(c) is consistent because the context of theright-hand-side withdraw includes the left-hand-side withdraw.Evaluating this context yields a zero balance, which causes theright-hand-side withdraw to generate skip as its effect.

LEMMA 4. If X |= T ,F , then eval†F (X) is a singleton set. Fur-thermore, so is eval†F (ctxt(e,X)) for any e ∈ X.E.

The lemma shows that in Definition 2 it does not matter howwe choose the order of evaluation in eval†F . When viewed oper-ationally, this independence implies the convergence property from§2.1: two replicas that see the same events will end up in the samestate. The proof of Lemma 4, given in §A, exploits properties (7)and (8). This proof is subtle because (7) does not require commu-tativity for the effects of pairs of operations that acquire conflictingtokens.

Motivated by Lemma 4, we define the evaluation of consistentexecutions

evalF : Exec(T ,F)→ State

as follows: evalF (X) is the unique σ such that eval†F (X) = σ.To illustrate the flexibility of our consistency model, we show

how it can represent some of the existing models; we provide moreinstantiations in §6.

Page 6: ’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

Causal consistency [16, 32] is the baseline model we obtainwithout using any tokens: Token = ∅ and ∀o, σ.F tok

o (σ) = ∅.Then (8) is a tautology and (7) is equivalent to (4), so that all effectshave to commute.

Sequential consistency [28] is a form of strong consistency andthe strongest consistency model we can obtain from ours. It re-quires every operation to acquire a mutual exclusion token:

Token = τ; ./ = (τ, τ); ∀o, σ.F toko (σ) = τ.

Then in any execution X ∈ Exec((Token, ./),F), the happens-before X.hb is total, and each event in X is aware of the effects ofall events preceding it in X.hb.

RedBlue consistency [31] is a hybrid consistency model thatclassifies operations as either red or blue: Op = Opr ] Opb.Red operations are guaranteed sequential consistency, and blueoperations, only causal consistency. To express this in our model,we again use a mutual exclusion token: Token = τ and ./ =(τ, τ). Red operations acquire τ , and blue operations acquire notokens:

(∀o ∈ Opr. ∀σ.Ftoko (σ) = τ) ∧ (∀o ∈ Opb.∀σ.F

toko (σ) = ∅).

Then red operations are totally ordered by happens-before, and blueones are ordered only partially. The token assignment in our bank-ing application (Figure 4) is an instance of the RedBlue consis-tency, where withdraw operations are red, and all others are blue.

Our framework cannot express some of common consistencymodels, such as prefix consistency [42], which is stronger thancausal consistency. However, the framework could be adjusted toassume prefix consistency as a baseline following [17].

4. State-based Proof RuleWe consider the following verification problem: given a tokensystem T = (Token, ./), prove that operations F maintain anintegrity invariant I ⊆ State over database states. Formally, weestablish that any execution consistent with T and F evaluates to astate satisfying I:

Exec(T ,F) ⊆ eval−1F (I).

By Proposition 3 this implies that the return value of every eventin an execution X ∈ Exec(T ,F) can be obtained by applying itsoperation to a state satisfying I:

∀e ∈ X.E. ∃σ ∈ I. (X.rval(e) = F valX.oper(e)(σ)).

For example, we show that any execution consistent with Fig-ure 4 evaluates to a state satisfying the invariant (5). Hence, a queryoperation will always return a non-negative balance.

The key challenge of the above verification problem is theneed to consider infinitely many executions consistent with T andF . Our main technical contribution is the proof rule for solvingthis problem that avoids considering all such executions explicitly.Instead, the proof rule is modular in that it allows us to reasonabout the behaviour of every operation separately. Our proof ruleis also state-based in that it reasons in terms of states obtained byevaluating parts of executions or, from the operational perspective,in terms of replica states.

We give our proof rule in Figure 5 and explain it from the op-erational perspective. The rule assumes that the invariant I holdsof the initial database state σinit (condition S1). Consider a compu-tation of the database implementation from §2 and a state σ of areplica r at some point in this computation. The proof rule assumesthat σ ∈ I and aims to establish that executing any operation o at rwill preserve the invariant I . This is easy if we only consider how

∃G0 ∈ P(State× State), G ∈ Token→ P(State× State)such that

S1. σinit ∈ IS2. G0(I) ⊆ I ∧ ∀τ. G(τ)(I) ⊆ IS3. ∀o, σ, σ′. (σ ∈ I ∧ (σ, σ′) ∈ (G0 ∪G((F tok

o (σ))⊥))∗)

=⇒ (σ′,F effo (σ)(σ′)) ∈ G0 ∪G(F tok

o (σ))

Exec(T ,F) ⊆ eval−1F (I)

Figure 5. State-based proof rule for a token system T =(Token, ./). For T ⊆ Token we let G(T ) =

⋃τ∈T G(τ) and

T⊥ = τ | τ ∈ Token ∧ ¬∃τ ′ ∈ T. τ ./ τ ′. We denote by R∗

the reflexive and transitive closure of a relation R. For a relationR ∈ P(A × B) and a predicate P ∈ P(A), the expression R(P )denotes the image of P under R.

0

r

r0

(a) (b)

e

XX 0

X 00F e↵

o ()

Figure 6. Graphical illustrations of (a) the state-based rule; and (b)the event-based rule.

o’s effect changes the state of r, since this effect is applied to thestate σ where it was generated:

∀σ. (σ ∈ I =⇒ F effo (σ)(σ) ∈ I). (10)

The difficulty comes from the need to consider how o’s effectchanges the state of any other replica r′ that receives it; see Fig-ure 6(a). At the time of the receipt, r′ may be in a different stateσ′, due to operations executed at r′ concurrently with o. We canshow that it is sound to assume that this state σ′ also satisfies theinvariant. Thus, to check that the operation o preserves the invariantwhen applied at any replica, it is sufficient to ensure

∀σ, σ′. (σ, σ′ ∈ I =⇒ F effo (σ)(σ′) ∈ I). (11)

However, establishing this without knowing anything about the re-lationship between σ and σ′ is a tall order. In the bank accountexample, both σ = 100 and σ′ = 0 satisfy the integrity invari-ant (5). Then F eff

withdraw(100)(σ)(σ′) = −100, which violates theinvariant. Condition (11) fails in this case because it does not takeinto account the tokens acquired by withdraw.

The proof rule in Figure 5 addresses the weakness of (11) by al-lowing us to assume a certain relationship between the state wherean operation is generated (σ) and where its effect is applied (σ′),which takes into account the tokens acquired by the operation. Toexpress this assumption, the rule uses a form of rely-guarantee rea-soning [26]. Namely, it requires us to associate each token τ with aguarantee relation G(τ), describing all possible state changes thatan operation acquiring τ can cause. Crucially, this includes not onlythe changes that the operation can cause on the state of its originreplica, but also any change that its effect causes at any other replicait is propagated to. We also have a guarantee relationG0, describingthe changes that can be performed by an operation without acquir-ing any tokens. Condition S2 requires the guarantees to preservethe invariant.

Page 7: ’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

Like (11), condition S3 considers an arbitrary state σ of o’sorigin replica r, assumed to satisfy the invariant I . The conditionthen considers any state σ′ of another replica r′ to which the effectof o is propagated. The conclusion of S3 requires us to prove thatapplying the effectF eff

o (σ) of the operation o to the state σ′ satisfiesthe union of G0 and the guarantees associated with the tokensF toko (σ) that the operation o acquires. By S2, this implies that the

effect of the operation preserves the invariant. Condition S3 furtherallows us to assume that the state σ′ of r′ can be obtained from thestate σ of r by applying a finite number of changes allowed by G0

or the guarantees for those tokens that do not conflict with any ofthe tokens acquired by the operation o, i.e., G0 ∪G((F tok

o (σ))⊥).Informally, acquiring a token denies other replicas permissions toconcurrently perform changes that require conflicting tokens.

We now use our proof rule to show that the operations in thebanking application (Figure 4) preserve the integrity invariant (5).We assume that the initial state σinit satisfies the invariant. Theguarantees are as follows:

G(τ) = (σ, σ′) | 0 ≤ σ′ < σ;G0 = (σ, σ′) | 0 ≤ σ ≤ σ′. (12)

Since withdrawals acquire the token τ , the guarantee G(τ) for thistoken allows decreasing the balance without turning it negative;the guarantee G0 allows increasing a non-negative balance. Thencondition S2 is satisfied. We show how to check the condition S3in the most interesting case of o = withdraw(a). Consider σ andσ′ satisfying the premiss of S3:

σ ∈ I ∧ (σ, σ′) ∈ (G0 ∪G((F toko (σ))⊥))∗.

Since F toko (σ) = τ, we have (F tok

o (σ))⊥ = ∅. Thus, (σ, σ′) ∈G∗0. This and σ ∈ I imply

0 ≤ σ ≤ σ′. (13)

If σ < a, then F effo (σ)(σ′) = σ′. Furthermore, σ′ ≥ 0 by

(13). Thus, (σ′,F effo (σ)(σ′)) = (σ′, σ′) ∈ G0, which implies the

conclusion of S3.If σ ≥ a, then F eff

o (σ)(σ′) = σ′− a. Since σ ≤ σ′ by (13), wehave σ′ ≥ a. Thus, (σ′,F eff

o (σ)(σ′)) = (σ′, σ′ − a) ∈ G(τ),which implies the conclusion of S3. Operationally, in this caseour proof rule establishes that, if there was enough money in theaccount at the replica where the withdrawal was made, then therewill be enough money at any replica the withdrawal is delivered to.This completes the proof of our example.

In a banking application with multiple accounts, we could en-sure non-negativity of balances by associating every account c witha token τc such that τc ./ τc, but τc 6./ τc′ for another accountc′. Thus, withdrawals from the same account would have to syn-chronise, while withdrawals from different accounts could proceedwithout synchronisation. Our proof rule easily deals with this gen-eralisation by associating every token τc with a guarantee describ-ing the changes to the corresponding account. As we elaborate in§6, the banking application we verify with the aid of our tool allowsmultiple accounts. There we also provide more complex examplesof using our proof rule. For now, it is instructive to see how theproof rule is specialised for some of the simpler instantiations ofour consistency model from §3.

Sequential consistency. Recall that for sequential consistency,./ = (τ, τ) and we always have F tok

o (σ) = τ, so that(F tok

o (σ))⊥ = ∅. Let G0 = ∅, so that we always have σ = σ′

in S3. Then S2 and S3 require us to find G(τ) such that

G(τ)(I) ⊆ I ∧ ∀o, σ. (σ ∈ I =⇒ (σ,F effo (σ)(σ)) ∈ G(τ)).

It is easy to show that we can find such a G(τ) if and only if (10)holds for all o. Thus, in this case it is sufficient to check that the

deposit(20)

(a)

query:-20

σinit = 0

withdraw(20):τ,

deposit(20)

withdraw(126):τ,

interest

(b) σinit = 100

query:-1

Figure 7. Executions illustrating the unsoundness of the state-based proof rule on weaker consistency models.

effect of an operation preserves the invariant when applied to thesame state where it was generated.

Causal consistency. We have Token = ∅ and the conditions S2and S3 become equivalent to

G0(I) ⊆ I ∧ (∀o, σ, σ′. (σ ∈ I ∧ (σ, σ′) ∈ G∗0)

=⇒ (σ′,F effo (σ)(σ′)) ∈ G0).

In this case the effects of all operations are described by a singleguarantee relation G0. We need to show that every operation satis-fies this guarantee while assuming that concurrently executing op-erations at other replicas do. Note that (11), for all o, is a specialcase of the above obligation for G0 = I × I . Thus, (11) is aninvariant-based version of the above rely-guarantee proof rule.

As we elaborate in §7, our proof rule bears a lot of similar-ity to proof rules for strongly consistent shared-memory concur-rency [21, 26, 35]. The reasons for the soundness of our proof in thesetting of weak consistency are subtle. Its soundness relies cruciallyon the fact that our consistency model guarantees at least causalconsistency and on the commutativity of operation effects (7). Forexample, some consistency models do not guarantee the transitiv-ity of happens-before [8, 46] and thus allow the execution in Fig-ure 7(a), which uses the operations in Figure 4. Here a withdrawalhb-follows a deposit; a query sees only the withdrawal, thus vio-lating the integrity invariant (5). Since we have proved these op-erations to preserve the invariant using our proof rule, this rule isunsound over a consistency model allowing the execution in Fig-ure 7(a). We note that the obligation (11), for all o, establishes theinvariant I even for a consistency model where hb is only acyclic,but not necessarily transitive.

To illustrate that our rule becomes unsound if we drop the re-quirement of effect commutativity (7), consider the operations inFigure 4, but with the effect of interest defined by (3). It is easyto show that the premiss of the rule holds for the invariant (5) evenwith this change. At the same time, the execution in Figure 7(b)violates the invariant, yet is consistent with the operations in Fig-ure 4 according to Definition 2. This is because the evaluation de-termining the effect of withdraw(126) can order deposit(20) be-fore interest, whereas the evaluation determining the outcome ofquery can order these operations the other way round, resulting ina smaller balance. Again, the obligation (11) establishes the invari-ant even without (7): it ensures

∀X ∈ Exec(T ,F). eval†F (X) ⊆ I.

5. Event-based Proof Rule and SoundnessWe now prove the soundness of the state-based proof rule. To thisend, we present an event-based proof rule (Figure 8), from whichthe state-based one is derived. This event-based rule highlights thereasons for the soundness of the state-based one. Instead of rea-soning about replica states, the event-based rule reasons about ex-ecutions describing the events that replicas know about; the eval-uation of the corresponding effects yields the replica states in the

Page 8: ’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

∃G ∈ P(Exec(T )× Exec(T )) such that

E1. Xinit ∈ IE2. G(I) ⊆ IE3. ∀X,X ′, X ′′. ∀e ∈ X ′′.E.

(X ∈ I ∧X ′ = X ′′|X′′.E−e ∧X ′′ ∈ Exec(T ,F) ∧e ∈ max(X ′′) ∧X = ctxt(e,X ′′) ∧ (X,X ′) ∈ G∗)=⇒ (X ′, X ′′) ∈ G

Exec(T ,F) ⊆ I

Figure 8. Event-based proof rule.

state-based rule. In particular, we specify the desired integrity in-variant as a predicate on executions: I ⊆ Exec(T ). The event-based rule establishes that any execution consistent with givenT = (Token, ./) and F belongs to I: Exec(T ,F) ⊆ I.

As before, we explain the event-based rule from the operationalperspective. The rule again uses rely-guarantee reasoning, but withthe guarantee G represented by a relation on executions. The guar-antee describes the change to a replica’s knowledge brought on bythe replica executing a new operation or receiving the effect of anoperation originally executed elsewhere.

Conditions E1 and E2 are similar to S1 and S2: E1 requiresthe invariant I to allow an empty execution Xinit (§3), which eval-uates to the initial database state σinit; E2 requires the guaranteeto preserve the invariant. Condition E3 is graphically illustrated inFigure 6(b). Similarly to S3, the condition E3 considers any op-eration, denoted by an event e, and checks that the change to thedatabase state made by the operation satisfies the guarantee. Thischeck is done not only at the origin replica r of e, but also at anyother replica r′ that receives its effect. The execution X can bethought of as describing the events known to the replica r whenit executed the operation denoted by e. We assume that the ex-ecution X satisfies the invariant I. The execution X ′ describesthe events known to the replica r′ just before it receives the ef-fect of e; X ′′ describes the events known to r′ after this, so thatX ′ = X ′′|X′′.E−e. The execution X ′′ is consistent with T andF ; the conditions in the proof rule imply that so areX andX ′. Thecondition e ∈ max(X ′′) (see (9)) reflects the fact that e is the latestevent received by r′. The condition X = ctxt(e,X ′′) ensures thatX is a part of X ′ = X ′′|X′′.E−e. This reflects the guarantee ofcausal message propagation: when r′ receives the effect of e, thisreplica is guaranteed to know about all the events that the replica rknew about when it executed e.

Even though the rule allows us to assume that X is part of X ′,the latter may contain additional events that the replica r′ foundout about by the time it received the effect of e. The rule allowsus to assume that the changes in the knowledge of r′ broughton by adding these events satisfy the guarantee: (X,X ′) ∈ G∗.In exchange, the rule requires us to ensure that adding the evente to the knowledge of replica r′ will also satisfy the guarantee:(X ′, X ′′) ∈ G.

In the following, we use the fact that the premiss of the impli-cation in E3 entails that all events in X ′.E − X.E are causallyindependent with e.

PROPOSITION 5. For all X,X ′, X ′′ and e ∈ X ′′.E,

(X ′ = X ′′|X′′.E−e ∧ e ∈ max(X ′′) ∧X = ctxt(e,X ′′))

=⇒ ¬∃f ∈ (X ′.E −X.E). (eX′′.hb−−−−→ f ∨ f X′′.hb−−−−→ e).

PROOF. Consider f ∈ (X ′.E −X.E). Since e ∈ max(X ′′), we

cannot have e X′′.hb−−−−→ f . If f X′′.hb−−−−→ e, then f ∈ X.E due toX = ctxt(e,X ′′). But this contradicts f ∈ (X ′.E −X.E). ut

We now give the proof of soundness of the event-based rule andsketch the derivation of the state-based one (we give a full proof ofthe latter in §A).

Let v be the following partial order on executions:

X v X ′ ⇐⇒ (X = X ′|X.E ∧ ((X ′.hb)−1)(X.E) ⊆ X.E).(14)

When X v X ′, we say that X is a causal cut of X ′; any eventis included into X together with its causal dependencies in X ′.Operationally, X v X ′ means that X and X ′ can describe theknowledge of a replica at different points in the same databasecomputation.

PROPOSITION 6.

∀X ∈ Exec(T ,F).∀Y. (Y v X =⇒ Y ∈ Exec(T ,F)).

THEOREM 7. The event-based proof rule in Figure 8 is sound.

PROOF. Assume E1-E3 hold. We prove that

∀X ′′ ∈ Exec(T ,F).∀Y. (Y v X ′′ =⇒ (Y,X ′′) ∈ G∗), (15)

i.e., that the guarantee G allows us to transition into a consistentexecutionX ′′ from any of its causal cuts Y . The desired conclusionExec(T ,F) ⊆ I follows from (15): it implies (Xinit, X

′′) ∈ G∗,but Xinit ∈ I (E1) and G preserves I (E2).

The proof of (15) is done by induction on the size of X ′′.In the base case, we must have Y = X ′′ = Xinit, which im-plies (Y,X ′′) ∈ G∗. In the induction step, we consider X ′′ ∈Exec(T ,F) and Y v X ′′ such that Y 6= X ′′. We pick an evente ∈ (X ′′.E − Y.E) such that e ∈ max(X ′′) and define X and X ′

as in E3:

X = ctxt(e,X ′′) ∧X ′ = X ′′|X′′.E−e.

ThenY v X ′ ∧X v X ′. (16)

By Proposition 6 we have X,X ′ ∈ Exec(T ,F). Thus, we canapply the induction hypothesis to X ′ and its causal cuts X and Y ,as well as to X and its causal cut Xinit, getting:

(Y,X ′) ∈ G∗ ∧ (X,X ′) ∈ G∗ ∧ (Xinit, X) ∈ G∗.By E1 and E2, (Xinit, X) ∈ G∗ implies X ∈ I. Together with(X,X ′) ∈ G∗, this allows us to apply E3 and obtain (X ′, X ′′) ∈G. This and (Y,X ′) ∈ G∗ imply (Y,X ′′) ∈ G∗, as required. ut

In operational terms, the statement (15) established in the proofensures that any sequence of changes in the knowledge of a replicaduring a database computation is described by G∗. The above proofrelies crucially on the fact that our consistency model guaranteesat least causal consistency. For example, in (16) we can deduceX v X ′ from X = ctxt(e,X ′′) because happens-before istransitive.

COROLLARY 8. The state-based proof rule in Figure 5 is sound.

PROOF SKETCH. Assume a state-based invariant I ⊆ State. Weconstruct the corresponding event-based invariant I as the set of allexecutions that evaluate to a state in I: I = eval−1

F (I). Then theconclusion Exec(T ,F) ⊆ I of the event-based rule implies theconclusion Exec(T ,F) ⊆ eval−1

F (I) of the state-based rule.We now show that the premiss of the state-based rule implies

that of the event-based rule. Assume state-based guaranteesG0 andG that satisfy S1-S3. We construct the corresponding event-basedguarantee G by describing the change to the knowledge of a replica

Page 9: ’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

brought on by incorporating the effect of an operation satisfyingthe state-based guarantees G0 and G:

G = (X,Y ) | ∃e. (Y.E −X.E) = e ∧X v Y ∧(evalF (X), evalF (Y )) ∈ G0 ∪G(Y.tok(e)). (17)

Thus, the guarantee G consists of pairs (X,Y ), where Y extendsX by a single event e representing the operation, and the twoexecutions evaluate to a pair of states inG0 orG(τ) for some tokenτ acquired by e.

It remains to prove that the event-based guarantee G satisfiesconditions E1-E3. Conditions E1 and E2 trivially follow from con-ditions S1 and S2; we thus only need to show that S3 implies E3.Assume that for some X,X ′, X ′′ and e ∈ X ′′.E, the premiss ofE3 holds:

X ∈ I ∧X ′ = X ′′|X′′.E−e ∧X ′′ ∈ Exec(T ,F) ∧e ∈ max(X ′′) ∧X = ctxt(e,X ′′) ∧ (X,X ′) ∈ G∗. (18)

Let σ = evalF (X), σ′ = evalF (X ′) and o = X ′′.oper(e). Wenow show that the premiss of S3 holds:

σ ∈ I ∧ (σ, σ′) ∈ (G0 ∪G((F toko (σ))⊥))∗. (19)

First of all, σ ∈ I follows from X ∈ I by the definition of I.Furthermore, by Proposition 5, all events in (X ′.E − X.E) areunrelated to e in (X ′′.hb ∪ (X ′′.hb)−1). But then by (8), theycannot acquire tokens that conflict with the ones acquired by e:

∀f ∈ (X ′.E −X.E).¬(X ′′.tok(e) ./ X ′′.tok(f)).

Using this fact, (X,X ′) ∈ G∗ given by (18) and the definition ofG given by (17), we can show that

(σ, σ′) = (evalF (X), evalF (X ′)) ∈ (G0 ∪G((X ′′.tok(e))⊥))∗

= (G0 ∪G((F toko (σ))⊥))∗,

thus establishing (19). Then the conclusion of S3 yields(σ′,F eff

o (σ)(σ′)) ∈ G0 ∪G(F toko (σ)), so that

(evalF (X ′), evalF (X ′′)) = (σ′,F effo (σ)(σ′))

∈ G0 ∪G(F toko (σ))

= G0 ∪G(X ′′.tok(e)).

(20)

This implies the conclusion of E3: (X ′, X ′′) ∈ G. utThe above proof relies crucially on Lemma 4, which allows us

to define evalF . The lemma guarantees that, when evaluating exe-cutions, choosing different orders for causally independent eventsdoes not affect the resulting state. In (20) this allows us to choose aparticular convenient order of evaluating X ′′ that applies the oper-ation o last. Lemma 4 holds due to the commutativity condition (7),and this illustrates the importance of this condition for the sound-ness of the state-based rule.

6. Examples and AutomationWe have developed a prototype tool that automates the state-basedproof rule by reducing its obligations to SMT queries. Using thetool, we have verified three applications: an extended version ofthe banking application in Figure 4, an auction service and a courseregistration system. Our results are summarised in Figure 9. Inthe following, we first show more sophisticated uses of our proofrule using fragments of the auction and courseware applications,as well as the consistency model of parallel snapshot isolation [37,41]. We then present our automation approach and the completeapplications that we verified.

6.1 Auction ServiceFigure 10 shows a fragment of an auction application. An auctioncan be either open or closed. While the auction is open, a client can

Application # ops # tokens # invariants time (ms)Banking 5 1 1 385Auction 14 9 12 5297

Courseware 5 5 2 512

Figure 9. Characteristics of the applications verified and the timetaken by the tool. The numbers of operations are given ignoring op-eration parameters. The numbers of tokens are similarly given with-out taking into account tokens associated with different instances ofthe same object, such as different bank accounts. The tool was runon a Mac Mini, 3 GHz Intel Core i7.

State = P(N)× (N ∪ ⊥)σinit = (∅,⊥)

I = (B,w) | w 6= ⊥ =⇒ B 6= ∅ ∧ w = max(B)Token = τc, τp

./ = (τc, τc), (τc, τp), (τp, τc)Fplace(b)((B,w)) = if w 6= ⊥ then (7, skip, τp)

else (X, (λ(B′, w′). (B′ ∪ b, w′)), τp)Fclose((B,w)) = if (w 6= ⊥ ∨B = ∅) then (7, skip, τc)

else (X, (λ(B′, w′). (B′,max(B))), τc)Fquery((B,w)) = ((B,w), skip, ∅)

Figure 10. A fragment of an auction application.

place a bid with the amount b using the place(b) operation. A clientcan also close the auction at any time using the close operation,which declares the winner. Finally, clients can query the databasestate using query.

The database state is of the form (B,w). Here B contains theamounts of the bids placed; for simplicity, we do not distinguishtwo bids with the same amount. The component w is either ⊥,signifying that the auction is still open, or the winning bid. Asuccessful place(b) operation has the effect of adding b to B. Theclose operation writes the winning bid into w. Note that the effectsof two close operations do not commute. To satisfy (7), and toensure that clients can only close the auction once, we let closeoperations acquire a token τc such that τc ./ τc.

The integrity invariant I we would like to maintain in the auc-tion application is that, if the auction is closed, then the winningbid is the maximal of all the bids placed. Without using any othertokens than τc, this invariant can be violated: Alice can close theauction and declare the winner, e.g., 100, without being aware of ahigher bid 105 placed concurrently by Bob. A query aware of bothoperations will return the bid set containing 105 and 100 but mark100 as the winning bid in the set.

To preserve the invariant in the RedBlue consistency model(§3), we would have to use strong consistency for both place andclose operations, i.e., let them acquire the mutually exclusive tokenτc. To address this inefficiency, Balegas et al. [9] proposed a hybridmodel where consistency can be strengthened using multi-levellocks, analogous to readers-writer locks from shared memory. Inour example, we represent such a lock by a pair of tokens: τc,introduced before, and τp. Each close operation acquires τc, andeach place operation, τp. We have τc ./ τp. Hence, for every pairof close and place(b) operations, either close is aware of the bid band takes it into account when computing the winner, or place(b)is aware that the auction has been closed and, hence, does notplace the bid. However, we do not have τp ./ τp and, hence,bid placements can be causally independent. In our analogy witha readers-writer lock, bid placements play the role of readers andclosing the auction, the role of a writer.

Page 10: ’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

State = P(Student)×RWset(Course)×P(Student×Course)

σinit = (∅, ∅RWset, ∅)I = (S,C,E) | E ⊆ P(S × contents(C))

Token = τe(c), τr(c) | c ∈ Course./ = (τe(c), τr(c)), (τr(c), τe(c)) | c ∈ Course

Fregister(s)((S,C,E)) =

(⊥, (λ(S′, C′, E′). (S′ ∪ s, C′, E′)), ∅)FaddCourse(c)((S,C,E)) =

(⊥, (λ(S′, C′, E′). (S′, add(c, C′), E′)), ∅)Fenrol(s,c)((S,C,E)) =

if (s 6∈ S ∨ c 6∈ contents(C)) then (7, skip, τe(c))else (X, (λ(S′, C′, E′). (S′, C′, E′ ∪ (s, c))), τe(c))FremCourse(c)((S,C,E)) =

if (c 6∈ contents(C) ∨ ∃s. (s, c) ∈ E)) then (7, skip, τr(c))else (X, (λ(S′, C′, E′). (S′, remove(c, C′), E′)), τr(c))Fquery((S,C,E)) = ((S, contents(C), E), skip, ∅)

RWset(Course) = P(Course)× P(Course)

∅RWset = (∅, ∅)add(c, (A, T )) = (A ∪ c, T )

remove(c, (A, T )) = (A, T ∪ c)contents((A, T )) = A− T

Figure 11. A fragment of a courseware application.

Balegas et al. [9] show how to implement multi-level locks sothat a replica can place a bid without any synchronisation; only anoperation closing the auction has to synchronise with other replicasto make sure that no bids are placed concurrently. Thus, the mostfrequent operation of bid placement is the least expensive.

We now use our proof rule to show that the above consistencychoice is indeed sufficient to preserve the invariant I . Let

G0 = ((B,w), (B,w)) | (B,w) ∈ I;G(τp) = ((B,⊥), (B′,⊥)) | B ⊂ B′;G(τc) = ((B,⊥), (B,max(B))) | B 6= ∅.

Then the condition S2 in Figure 5 is satisfied. We show how tocheck the condition S3 in the most interesting case of o = place(b).

Consider σ = (B,w) and σ′ = (B′, w′) satisfying the premissof S3. Then σ ∈ I . Also, since

(σ, σ′) ∈ (G0 ∪G((F toko (σ))⊥))∗

and (F toko (σ))⊥ = τp, we get

w′ = w ∧B ⊆ B′ ∧ (w 6= ⊥ =⇒ B′ = B). (21)

If w 6= ⊥, then F effo (σ)(σ′) = σ′ and, by (21), σ = σ′. Since

σ ∈ I , we have

(σ′,F effo (σ)(σ′)) = (σ′, σ′) = (σ, σ) ∈ G0.

This implies the conclusion of S3.If w = ⊥, then F eff

o (σ)(σ′) = (B′ ∪ b, w′). In this case (21)implies w′ = w = ⊥. We then get

(σ′,F effo (σ)(σ′)) = ((B′, w′), (B′ ∪ b, w′)) ∈ (G0 ∪G(τp)),

the desired conclusion of S3. Operationally, our proof rule estab-lishes that, if the auction was open at the replica where the bid wasplaced, then it will be open at any replica the bid is delivered to.

Similarly to our banking application (§4), we can deal with mul-tiple auctions by using a pair of tokens (τc, τp) for every auction.The above proof generalises straightforwardly to this case.

addCourse(Java) addCourse

(Java)remCourse

(Java)

query: ?

(a)

addCourse(Java)

enrol(Carol, Java)

(b)

addStudent(Carol)

remCourse(Java)

query: (Carol,∅,(Carol, Java))

Figure 12. Executions illustrating the need for (a) replicated datatypes and (b) tokens in the courseware application.

6.2 CoursewareOur next example illustrates a different kind of an integrity invari-ant and the use of replicated data types [39] to construct commu-tative operations. Figure 11 shows a fragment of a courseware ap-plication. We assume sets of courses Course and students Student.A client can add a course c using addCourse(c) and register a stu-dent s using register(s). A registered student s can be enrolledinto a course c using enrol(s, c). In the application fragment weconsider, student registrations and enrolments cannot be cancelled.However, a course c that has not secured any student enrolment canbe removed using remCourse(c). As usual, we also have a queryoperation.

A database state (S,C,E) consists of the set of students S, theset of courses C and the enrolment relation E between studentsand courses. The set of courses is actually not just an ordinary set,but a replicated remove-wins set RWset(Course), explained in thefollowing. The effects of operations are mostly as expected, withcourses accessed using special functions add, remove and contentson the replicated set. Note that the operation enrol(c, s) takes effectonly if the student s is registered and the course c exists. Theoperation remCourse(c) removes the course c only when it existsand has no students enrolled into it.

Using a replicated data type for the set of courses is needed tosatisfy (7), because additions to and removals from a usual set donot commute. To illustrate, consider the execution in Figure 12(a).There Alice adds a course on Java and then changes her mind andremoves the course; concurrently, Bob adds the same Java course.If we maintained the information about courses using a usual set,then the outcome of the query in the figure would depend on theorder in which we evaluate the effects of the causally indepen-dent operations addCourse(Java) by Bob and remCourse(Java)by Alice: the query would return ∅ if the addition was evaluated be-fore removal, and Java otherwise (see Definition 2). In an actualdatabase, implementing the operations using ordinary sets wouldviolate the replica convergence property (§2.1).

Replicated data types [39] provide implementations of opera-tions on data structures with commutative effects. They differ in theway in which they resolve conflicting updates to the data structure,such as those in Figure 12(a): when using an add-wins set, the queryin the figure will return Java, and when using a remove-wins set,∅ [38]. The decision which data type to use ultimately depends onapplication requirements. To keep presentation manageable, in ourexample we use one of the simplest set data types, which providesa rudimentary version of the remove-wins semantics.

The data type represents the replicated set of courses using apair of sets (A, T ). The function add(c, ·) puts c into the set ofA, and the function remove(c, ·) puts c into the set T , called thetombstone set. To get the contents of the replicated set, we justtake the difference between A and T . The functions add(c, ·) andremove(c, ·) commute: even if the removal is evaluated first, it will

Page 11: ’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

still cancel the subsequent addition1. This ensures that the effectsof all operations in Figure 11 commute and thus satisfy (7).

The integrity invariant I we would like to maintain in this appli-cation is that the enrolment relation refers to existing courses andstudents only. This property is an instance of referential integrity,which requires an object referenced in one part of the database toexist in another. Without using tokens, the operations in our appli-cation can break the invariant. This is illustrated by the executionin Figure 12(b). There a Java course initially has no students en-rolled. Then Alice removes the course and concurrently Bob enrolsCarol into it, thinking that the course is still available. This resultsin Carol being enrolled into a non-existent course.

To ensure that such situations do not happen, we use a pairof conflicting tokens for each course c ∈ Course: τe(c) andτr(c). The operation enrol(s, c) acquires τe(c), and the operationremCourse(c) acquires τr(c). Then for every pair of operationsenrol(s, c) and remCourse(c), either the enrolment operation isaware that the course has been removed, or the removal is awarethat there are still students enrolled into the course; in either casethe corresponding operation takes no effect. However, other pairsof operations can be causally independent and, hence, do not haveto synchronise. This includes pairs of operations enrolling studentsinto courses and pairs of operations manipulating courses, such asthose in Figure 12(a). The above use of tokens is equivalent to as-sociating every course with a multi-level lock [9] that can be in oneof two modes, one of which allows enrolling students into a course(τe(c)) and the other removing the course (τr(c)). Unlike in the auc-tion application above, neither of the tokens τe(c) or τr(c) conflictswith itself, and thus, neither of the above lock modes is exclusive.

Our proof rule can establish that the above consistency choice issufficient to preserve the integrity invariant. To this end, we use thefollowing guarantees, associating changes with tokens as expected:

G0 = (I × I) ∩ ((S,C,E), (S′, C′, E)) |S ⊆ S′ ∧ contents(C) ⊆ contents(C′);

G(τe(c)) = (I × I) ∩ ((S,C,E), (S,C,E′)) |∃s. E′ = E ] (s, c);

G(τr(c)) = (I × I) ∩ ((S,C,E), (S,C′, E)) |contents(C) = contents(C′) ] c.

The actual proof is similar to that of the auction application aboveand is omitted.

6.3 Parallel Snapshot IsolationWe show that our generic consistency model (§3) can be instan-tiated to capture parallel snapshot isolation (PSI), a consistencymodel recently proposed for replicated databases [37, 41], whichstrengthens causal consistency in a way different from the modelswe have considered so far. We then give a proof rule specific to PSI.

We assume that the database consists of a finite set Obj of ob-jects, ranged over by x, y. The objects store values from a set Val,so that we let State = Obj→ Val. Operations in PSI perform com-putations that read and write objects, and any two operations writ-ing to the same object acquire conflicting tokens. Thus, by (8) up-dates to the same object can never be concurrent, and the program-mer does not have to merge them explicitly. However, PSI doesnot provide strong consistency, since it allows updates to differentobjects to be concurrent. For example, PSI allows the outcome inFigure 2(b) when the operations add(postA) and add(postB) writeto different objects, e.g., representing the feeds of different users.

1 In fact, once an element was removed, it can never be successfully addedagain, which may not be a desirable behaviour. There are replicated sets thatprovide a more sophisticated semantics [38].

∃G ∈ P(State× State) such that

PSI1. σinit ∈ IPSI2. G(I) ⊆ IPSI3. ∀o, σ, σ′. (σ ∈ I ∧ (σ, σ′) ∈ (G ∩ (=

dom(Supdateso (σ))

))∗)

=⇒ (σ′,F effS,o(σ)(σ′)) ∈ G

PSIExec(S) ⊆ eval−1FS

(I)

Figure 13. Proof rule for PSI. For a set of objects Ω, we define therelation (=Ω) of type State× State as follows: for all states σ, σ′,σ =Ω σ′ iff ∀x ∈ Ω. σ(x) = σ′(x).

To represent PSI in our framework, we consider a token systemTPSI = (Token, ./), that associates every object x with a mutualexclusion token τx:

Token = τx | x ∈ Obj; τx ./ τy ⇐⇒ x = y.

To define the semantics of operations o ∈ Op, we assume afunction

S ∈ Op→ (State→ Val× (Obj Val))

and let∀o, σ. So(σ) = (Sval

o (σ),Supdateso (σ)).

Thus, Svalo (σ) gives the return value of an operation o executed in

a state σ, and Supdateso (σ) gives the values that o writes to objects.

We lift S to a function FS of the type (6) as follows:

FS,o(σ) = (Svalo (σ),

(λσ′. λx. if x ∈ dom(S) then S(x) else σ′(x)),

τx | x ∈ dom(S)),

where S = Supdateso (σ). Thus, the effect of an operation o is limited

to writing the values specified by So; this is unlike the generalcase of our consistency model, which allows arbitrary effects. Theacquired tokens are those for the objects written according to So.Note that the effect specified by FS,o(σ) changes only the valuesof the objects written to by the operation, but the converse is nottrue: an operation can write to an object the same value it originallystored. This will still trigger token acquisitions and, hence, createcausality relationships with other operations writing to the sameobject.

PROPOSITION 9. For any S, the function FS satisfies (7).

The proof is given in §A. Note that FS does not always satisfy (4):the flexibility allowed by (7) is crucial to represent PSI as aninstance of our generic consistency model.

We write PSIExec(S) = X | X |= TPSI,FS for the setof all PSI executions with operation semantics given by S. It iseasy to show that this definition is equivalent to a recently-proposeddeclarative definition of PSI [17].

Figure 13 gives a proof rule for checking that operations Spreserve an integrity invariant I when executed on PSI. The rulerequires us to specify the changes performed by all operationsusing a single guarantee G, which has to preserve I (conditionPSI2). Condition PSI3 then requires us to check that the effect ofan operation o generated in a state σ ∈ I satisfies the guaranteewhen applied to another state σ′. This state σ′ can be assumedto result from σ by a finite number of changes allowed by theguarantee G that do not modify the objects written by the operationo. Intuitively, the latter constraint comes from the fact that suchoperations acquire tokens conflicting with those of o.

THEOREM 10. The rule in Figure 13 is sound.

Page 12: ’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

∃G0 ∈ P(State× State), G ∈ Token→ P(State× State)such that

T1. σinit ∈ IT2. G0(I) ⊆ I ∧ ∀τ. G(τ)(I) ⊆ IT3. ∀o. ∃T.∃P1, . . . , Pn, Q1, . . . , Qn ∈ P(State).

T3a. T =⋂F tok

o (σ) | σ ∈ I ∧T3b. I ⊆

⋃ni=1 Pi ∧

T3c. ∀i = 1..n. Pi ⊆ Qi ∧T3d. (G0 ∪G(T⊥))(Qi) ⊆ Qi ∧T3e. (Qi × (F eff

o (Pi)(Qi))) ⊆ (G0 ∪G(T ))

Exec(T ,F) ⊆ eval−1F (I)

Figure 14. Proof rule used by our tool. We assume a token systemT = (Token, ./) and use the same notation as in Figure 5. We letF effo (Pi)(Qi) = F eff

o (σ)(σ′) | σ ∈ Pi ∧ σ′ ∈ Qi.

The proof, given in §A, derives the rule for PSI directly from theevent-based rule in Figure 8. We could derive the rule for PSI froma generalisation of the state-based rule in Figure 5 that associatesguarantees with sets of tokens. However, to simplify presentationwe opted for the simpler version of the state-based rule at theexpense of a more complex derivation of the rule for PSI.

6.4 AutomationOur tool uses the proof rule in Figure 14, which is derived fromthe one in Figure 5 and is more amenable to automation. Thepremisses T1 and T2 are identical to S1 and S2; T3 changes S3in two ways. A minor change is motivated by the fact that ourtool currently handles only operations that acquire the same setof tokens regardless of the state they are executed in. Hence, T3precomputes the set of tokens T acquired by an operation o (T3a).The key way in which T3 changes S3 is that it eliminates thetransitive closure of the guarantees, which is hard to automate.Whereas S3 quantifies over states σ where the effect of an operationo is generated and σ′ where it is applied, T3 considers propertiesof these states, respectively denoted by predicates Pi and Qi, i =1..n. T3b requires the predicates Pi to cover all possible states inwhich o can be executed. T3c requires Qi to cover Pi, reflectingthe fact that the effect of o can be applied in a state different fromthe one where it was generated. T3d requires Qi to be stable underthe changes allowed by the guarantees [26]. Finally, T3e checksthat, if the effect of o is generated in a state satisfying Pi, thenapplying this effect to a state satisfying Qi is consistent with theguarantees. Note that the constraints T3c and T3d have the sameeffect as relating the states σ and σ′ in S3 by a transitive closure ofguarantees.

For example, consider the operation o = withdraw(a) fromthe banking application in Figure 4. We let T = τ and use theguarantees (12). We use two predicates:

P1 = σ | σ ≥ a; P2 = σ | 0 ≤ σ < a.

These are motivated by the condition of the if-then-else in F effo , as

well as the invariant I . We then letQ1 = P1 andQ2 = I . It is easyto check that the obligations in T3 are fulfilled.

Our tool accepts as input a token system T , the semantics ofoperations F and an integrity invariant I , the latter two in theSMT-LIB format (we leave a programming language for writingoperations as future work). The tool generates predicates Pi frompreconditions of branches in F . As Qi, the tool takes either Pior the invariant I . Finally, the tool generates guarantees G0 and

G by intersecting the semantics of operations F with the invari-ant I . The required obligations are then discharged using the Z3SMT solver [1]. Currently our tool assumes that the condition (7)of operation commutativity is fulfilled: checking commutativity au-tomatically is nontrivial [27].

Applications verified. The applications verified using our tool(Figure 9) are more realistic versions of the examples we discussedbefore (Figures 4, 10 and 11).

The banking application extends the one in Figure 4 by con-sidering multiple accounts and allowing clients to transfer moneybetween accounts. We preserve the non-negativity of all balancesby associating a mutual exclusion token with each account, as de-scribed in §4.

The auction application extends the one in Figure 10 by addi-tionally maintaining information about buyers, sellers and prod-ucts, and by allowing clients to sell multiple product items in asingle auction. Buyers and sellers can register and unregister. Reg-istered buyers can bid in open auctions, and registered sellers canadd products, create auctions consisting of these and close auctions.The complex data model of this application requires multiple in-tegrity invariants, including referential integrity constraints span-ning multiple parts of the database. This makes it nontrivial to seeif enough synchronisation has been added to the application to pre-serve these invariants, and our tool copes with this task.

The courseware application extends the one in Figure 11 byallowing clients to cancel student registrations and enrolments. Italso imposes an additional integrity invariant limiting the numberof students that can register for a course; maintaining this invariantrequires extra synchronisation.

The above case studies demonstrate the feasibility of applyingour proof rule to realistic applications.

7. Related WorkReasoning in strongly consistent shared memory. Our state-based proof rule interprets tokens as permissions to perform certainstate changes. Such interpretations have been used in various logicsfor strongly consistent shared memory [20, 21, 35]. For example,such a logic could allow threads to modify the memory in a partic-ular way only when holding a mutual exclusion lock, similar to ouruse of a token in the banking application (§4).

This similarity suggests that existing work in shared memorymay be helpful in exploring the novel area of replicated databases.However, the distributed and weakly consistent setting in whichour proof rule is applied makes the reasons for its soundness sub-tle. In this setting, we do not have an illusion of a single copy of thedatabase state and a global notion of time this copy would evolvewith: as Figure 1(b) illustrates, different processes can see events asoccurring in different orders. The usual justification for the sound-ness of the proof rules for strong consistency relies on the con-cepts of global time and state: when considering a thread holding amutex lock, such proof rules reason that no other thread can holdthe lock at the same time and, hence, modify the memory state inthe way associated with the lock. In this setting, locks constrainthe global order on events. In contrast, tokens in our consistencymodel provide a more subtle guarantee (8), only constraining thepartial happens-before relation.

Reasoning about consistency in distributed systems anddatabases. Several papers have considered reasoning about cor-rectness properties on weak consistency models of replicated andcentralised databases.

Bailis et al. [7] have proposed a criterion for checking when anintegrity invariant is preserved by running operations without usingany synchronisation at all. But they do not provide guidelines onhow to introduce synchronisation if the invariant is violated.

Page 13: ’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

Li et al. [30, 31] have proposed a static analysis that uses theproof rule (11) to check if executing operations on causal con-sistency preserves a given integrity invariant. In case when (11)fails for some operation o, the analysis suggests to execute o understrong consistency in the RedBlue consistency model (§3). How-ever, the analysis does not check that the result will indeed validatethe invariant, and our proof rule fills this gap.

Sivaramakrishnan et al. [40] have proposed a static analysis thatautomatically chooses consistency levels in a replicated databasegiven programmer-supplied contracts. However, these contracts aremore low-level than our invariants, since they typically constrainthe happens-before relation. For example, in the banking applica-tion (Figure 4) their contract requires happens-before to totally or-der all withdrawal operations. The static analysis then ensures thatthe contract is followed, but not that it ensures the integrity invari-ant (5).

Lu et al. [33] proposed proof rules for establishing correctnessproperties of transactions running on non-hybrid weak consistencymodels of classical relational databases, such as snapshot isola-tion [12]. In contrast, we concentrate on hybrid consistency modelsof modern replicated databases, which are more sophisticated.

Fekete [22] considered a hybrid consistency model for relationaldatabases where some transactions execute under snapshot isola-tion [12] and some under serialisability, a form of strong consis-tency. He proposed conditions determining which transactions inan application need to execute under serialisability for the wholeapplication to be robust, i.e., produce only behaviours that would beobtained by executing all transactions under serialisability. In con-trast, our proof rule only checks integrity invariants while allow-ing the application to produce weakly consistent behaviours and,hence, benefit from the resulting performance gains.

Weak memory models. Strong consistency is forgone not onlyby modern databases, but also by shared-memory multiprocessorsand programming languages, which provide weak memory models.All such models used in practice are hybrid, in that they allowthe programmer to strengthen consistency on demand, e.g., usingmemory fences. However, weak memory models usually provideonly a limited number of operations on data, such as reads, writesand compare-and-swaps on single memory cells. Concurrent writesto the same memory cell result in one value being overwritten bythe other. In contrast, we deal with arbitrary operations (6) thatmerge concurrent updates in a user-defined way.

That said, in the future there may be a fruitful exchange of ideasbetween program logics for applications using weakly consistentdatabases and those running on weak memory models. In particular,there have been recent proposals of program logics for the “release-acquire” fragment of the C/C++ memory model [44, 45]. This frag-ment is analogous to causal consistency, with the above caveatsabout the operations allowed. However, the published logics do notmeaningfully handle operations requesting the stronger “sequen-tially consistent” level of C/C++. Reasoning about on-demand re-quests of stronger-than-causal consistency is precisely the goal ofthe present paper.

Several papers [3–5, 13, 18, 19] have verified application cor-rectness on weak memory models using model checkers andabstract interpreters. These papers thus explore verification ap-proaches different from the one considered in this paper. Addi-tionally, most of the papers have focused on models similar toTSO [3, 13, 18, 19], which is stronger than the causal consistencymodel we consider as a baseline. As the target correctness property,papers on weak memory models have often considered robustness(see above), which is too strong a requirement for our setting. Onthe other hand, some of the papers [3, 4, 13, 19] automatically in-ferred fences required to satisfy a correctness property. We do notaddress the inference of consistency choices, although in the future

our state-based proof rule can serve as a basis for this. In particular,the proof in Figure 6 can be used to refine a given conflict relation:when the stability check T3d fails, the relation can be extended toso as to shrink the set G(T⊥) and make the check pass.

Consistency models. Our conflict relations are similar to thoseused by Pedone and Schiper [36] to specify constraints on messagedelivery in a broadcast algorithm. We use the conflict relationsto define a high-level consistency model, which abstracts from amessage-based database implementation.

In a position paper, Li et al. [29] independently proposed anidea of a hybrid consistency model similar to ours. Their modeldoes not have a formal semantics and is less flexible than ours,since their analogue of the conflict relation is defined directly onoperations, instead of indirectly using tokens. This does not allowthe synchronisation mandated for an operation to depend on thestate it is executed in and, hence, does not allow expressing parallelsnapshot isolation (§6).

Specifying consistency models. The formal specification of ourconsistency model (§3) builds on a framework previously proposedto specify forms of eventual consistency [15]. Despite this simi-larity, we take a somewhat different approach to specifying the se-mantics of operations. Previous work [15] specified the return valueof an event by an arbitrary function of its context in the execution(§3). In contrast, our Definition 2 uses a particular function eval†F ,itself constructed from more primitive functions F eff

o , operating onstates. This choice allows us to define the semantics of operations interms of states, as opposed to events, which can then be used in ourstate-based proof rule. The use of states also allows to use off-the-shelf SMT solvers to discharge the required verification conditions.However, it is likely that our event-based rule may be adapted to theoperation specifications used in [15].

8. Conclusion and Future WorkWe presented the first proof rule establishing that a given consis-tency choice in a replicated database is sufficient to preserve a givenintegrity invariant. Our proof rule is modular and simple to use. Wedemonstrated this by small but nontrivial examples, and by reduc-ing the verification conditions of the proof rule to SMT checks. De-spite this simplicity, the soundness of our proof rule is nontrivial:the rule fully exploits the guarantees provided by our consistencymodel while correctly accounting for anomalies it allows.

Our results represent only an initial step in building an infras-tructure of reasoning methods for applications using modern repli-cated databases. They open several avenues for future work. First,our generic consistency model is not implemented by any databasein its full generality; we use it only as a means to compactly rep-resent a selection of more specific models in existing implementa-tions. However, in the future the generic model can serve as a basisfor exploring the space of possible hybrid consistency models. Onecould also consider a database that implements our model in itsgeneral form.

Second, the soundness of our proof rule relies on the fact thatour consistency model guarantees at least causal consistency (§4).Even though causal consistency can be implemented without anysynchronisation between replicas, this model has its cost [14]. Inthe future, we plan to propose proof rules for weaker models wherecausality preservation is not guaranteed for all operations. We alsohope to generalise our methods to more expressive correctnessproperties than integrity invariants.

Finally, in this paper we used the event-based proof rule just tostructure the proof of soundness of the state-based one. However,the event-based rule is also interesting in its own right. In the futureit can be used in cases when, to prove a correctness property,

Page 14: ’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

we need to maintain information about the computation history.For example, this is often necessary when reasoning about shared-memory concurrency [23, 25].

Acknowledgements. We thank Valter Balegas for helpful discus-sions and Vincent Gramoli for comments on an earlier draft of thispaper. Gotsman was supported by an EU FET project ADVENT.Ferreira, Najafzadeh, and Shapiro were supported in part by anEU project SyncFree (FP7 609 551, 2013–2016). Yang was sup-ported by EPSRC and an Institute for Information & communi-cations Technology Promotion (IITP) grant funded by the Koreagovernment (MSIP, No. R0190-15-2011).

References[1] https://github.com/Z3Prover/z3.

[2] D. Abadi. Consistency tradeoffs in modern distributed database sys-tem design: CAP is only part of the story. IEEE Computer, 45(2),2012.

[3] P. A. Abdulla, M. F. Atig, and N. T. Phong. The best of both worlds:Trading efficiency and optimality in fence insertion for TSO. In ESOP,2015.

[4] J. Alglave, D. Kroening, V. Nimal, and D. Poetzl. Don’t sit on thefence - A static analysis approach to automatic fence insertion. InCAV, 2014.

[5] J. Alglave, D. Kroening, V. Nimal, and M. Tautschnig. Softwareverification for weak memory via program transformation. In ESOP,2013.

[6] Amazon. Supported operations in DynamoDB.http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/APISummary.html, 2015.

[7] P. Bailis, A. Fekete, M. J. Franklin, A. Ghodsi, J. M. Hellerstein, andI. Stoica. Coordination avoidance in database systems. PVLDB, 2015.

[8] P. Bailis, A. Fekete, A. Ghodsi, J. M. Hellerstein, and I. Stoica. Scal-able atomic visibility with RAMP transactions. In SIGMOD, 2014.

[9] V. Balegas, N. Preguica, R. Rodrigues, S. Duarte, C. Ferreira, M. Na-jafzadeh, and M. Shapiro. Putting the consistency back into eventualconsistency. In EuroSys, 2015.

[10] Basho Inc. Using strong consistency in Riak.http://docs.basho.com/riak/latest/dev/advanced/strong-consistency/,2015.

[11] M. Batty, S. Owens, S. Sarkar, P. Sewell, and T. Weber. MathematizingC++ concurrency. In POPL, 2011.

[12] H. Berenson, P. Bernstein, J. Gray, J. Melton, E. O’Neil, and P. O’Neil.A critique of ANSI SQL isolation levels. In SIGMOD, 1995.

[13] A. Bouajjani, E. Derevenetc, and R. Meyer. Checking and enforcingrobustness against TSO. In ESOP, 2013.

[14] M. Bravo, N. Diegues, J. Zeng, P. Romano, and L. E. T. Rodrigues.On the use of clocks to enforce consistency in the cloud. IEEE DataEng. Bull., 38(1), 2015.

[15] S. Burckhardt, A. Gotsman, H. Yang, and M. Zawirski. Replicateddata types: specification, verification, optimality. In POPL, 2014.

[16] S. Burckhardt, D. Leijen, M. Fahndrich, and M. Sagiv. Eventuallyconsistent transactions. In ESOP, 2012.

[17] A. Cerone, G. Bernardi, and A. Gotsman. A framework for transac-tional consistency models with atomic visibility. In CONCUR, 2015.

[18] A. M. Dan, Y. Meshman, M. T. Vechev, and E. Yahav. Predicateabstraction for relaxed memory models. In SAS, 2013.

[19] A. M. Dan, Y. Meshman, M. T. Vechev, and E. Yahav. Effectiveabstractions for verification under relaxed memory models. In VMCAI,2015.

[20] T. Dinsdale-Young, M. Dodds, P. Gardner, M. J. Parkinson, andV. Vafeiadis. Concurrent abstract predicates. In ECOOP, 2010.

[21] M. Dodds, X. Feng, M. Parkinson, and V. Vafeiadis. Deny-guaranteereasoning. In ESOP, 2009.

[22] A. Fekete. Allocating isolation levels to transactions. In PODS, 2005.[23] M. Fu, Y. Li, X. Feng, Z. Shao, and Y. Zhang. Reasoning about op-

timistic concurrency using a program logic for history. In CONCUR,2010.

[24] S. Gilbert and N. Lynch. Brewer’s conjecture and the feasibility ofconsistent, available, partition-tolerant web services. SIGACT News,33(2), 2002.

[25] A. Gotsman, N. Rinetzky, and H. Yang. Verifying concurrent memoryreclamation algorithms with grace. In ESOP, 2013.

[26] C. B. Jones. Specification and design of (parallel) programs. In IFIPCongress. North-Holland, 1983.

[27] D. Kim and M. C. Rinard. Verification of semantic commutativityconditions and inverse operations on linked data structures. In PLDI,2011.

[28] L. Lamport. How to make a multiprocessor computer that correctlyexecutes multiprocess programs. IEEE Trans. Comput., 28(9), 1979.

[29] C. Li, J. Leitao, A. Clement, N. Preguica, and R. Rodrigues. Minimiz-ing coordination in replicated systems. In Workshop on Principles andPractice of Consistency for Distributed Data (PaPoC), 2015.

[30] C. Li, J. Leitao, A. Clement, N. M. Preguica, R. Rodrigues, andV. Vafeiadis. Automating the choice of consistency levels in replicatedsystems. In USENIX ATC, 2014.

[31] C. Li, D. Porto, A. Clement, R. Rodrigues, N. Preguica, and J. Gehrke.Making geo-replicated systems fast if possible, consistent when nec-essary. In OSDI, 2012.

[32] W. Lloyd, M. J. Freedman, M. Kaminsky, and D. G. Andersen. Don’tsettle for eventual: scalable causal consistency for wide-area storagewith COPS. In SOSP, 2011.

[33] S. Lu, A. J. Bernstein, and P. M. Lewis. Correct execution of trans-actions at different isolation levels. IEEE Trans. Knowl. Data Eng.,16(9), 2004.

[34] Microsoft. Consistency levels in DocumentDB.http://azure.microsoft.com/en-us/documentation/articles/documentdb-consistency-levels/, 2015.

[35] P. W. O’Hearn. Resources, concurrency and local reasoning. Theor.Comput. Sci., 375(1-3), 2007.

[36] F. Pedone and A. Schiper. Generic broadcast. In DISC, 1999.[37] M. Saeida Ardekani, P. Sutra, and M. Shapiro. Non-monotonic

snapshot isolation: Scalable and strong consistency for geo-replicatedtransactional systems. In SRDS, 2013.

[38] M. Shapiro, N. Preguica, C. Baquero, and M. Zawirski. A comprehen-sive study of Convergent and Commutative Replicated Data Types.Technical Report 7506, INRIA, 2011.

[39] M. Shapiro, N. M. Preguica, C. Baquero, and M. Zawirski. Conflict-free replicated data types. In SSS, 2011.

[40] K. Sivaramakrishnan, G. Kaki, and S. Jagannathan. Declarative pro-gramming over eventually consistent data stores. In PLDI, 2015.

[41] Y. Sovran, R. Power, M. K. Aguilera, and J. Li. Transactional storagefor geo-replicated systems. In SOSP, 2011.

[42] D. Terry. Replicated data consistency explained through baseball.Commun. ACM, 56(12), 2013.

[43] D. B. Terry, V. Prabhakaran, R. Kotla, M. Balakrishnan, M. K. Aguil-era, and H. Abu-Libdeh. Consistency-based service level agreementsfor cloud storage. In SOSP, 2013.

[44] A. Turon, V. Vafeiadis, and D. Dreyer. GPS: Navigating weak memorywith ghosts, protocols, and separation. In OOPSLA, 2014.

[45] V. Vafeiadis and C. Narayan. Relaxed separation logic: A programlogic for C11 concurrency. In OOPSLA, 2013.

[46] W. Vogels. Eventually consistent. CACM, 52(1), 2009.

Page 15: ’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

A. ProofsA.1 Proof of Propositions 3 and 6Proposition 3 follows from Proposition 6, and we thus only prove the latter. Pick X ∈ Exec(T ,F) and Y such that Y v X . We need toshow that

∀f ∈ Y.E. ∃σ ∈ eval†F (ctxt(f, Y )). (Y.rval(f) = F valY.oper(f)(σ)) ∧ (Y.tok(f) = F tok

Y.oper(f)(σ)). (22)Consider f ∈ Y.E. Then, since Y v X , we have f ∈ X.E. But X ∈ Exec(T ,F). Thus, from f ∈ X.E it follows that

∃σ ∈ eval†F (ctxt(f,X)). (X.rval(f) = F valX.oper(f)(σ)) ∧ (X.tok(f) = F tok

X.oper(f)(σ)).

The state σ here is the witness required in (22). This is because

(ctxt(f,X) = ctxt(f, Y )) ∧ (X.rval(f) = Y.rval(f)) ∧ (X.tok(f) = Y.tok(f)) ∧ (X.oper(f) = Y.oper(f)).

ut

A.2 Proof of Lemma 4If X |= T ,F , then by Proposition 3,

∀e ∈ X.E. (ctxt(e,X) |= T ,F).

Thus, the second part of this lemma on ctxt(e,X) follows from the first part on X . In the remainder of the proof, we will focus on provingthe first part.

Consider an execution X such that X |= T ,F . Our proof uses induction on the size of X.E. If X.E = ∅, then eval†F (X) = σinitindeed contains one element. When X.E is a singleton set, it easily follows from the definition of eval†F that eval†F (X) contains only oneelement, as desired. Otherwise, pick two different events e1, e2 ∈ max(X). Now let

X1 = X|X.E−e1, X2 = X|X.E−e2, X3 = X|X.E−e1,e2,Y1 = ctxt(e1, X), Y2 = ctxt(e2, X).

By Lemma 6, all of the executions above are consistent with F . By the induction hypothesis, there exist σ1, σ2, σ3, σ′1, σ′2 such that

eval†F (X1) = σ1, eval†F (X2) = σ2, eval†F (X3) = σ3, eval†F (Y1) = σ′1, eval†F (Y2) = σ′2.Let

FX.oper(e1)(σ′1) = ( , f1, T1), FX.oper(e2)(σ

′2) = ( , f2, T2).

Since X |= T ,F we haveX.tok(e1) = T1, X.tok(e2) = T2.

Because e1 and e2 are different X.hb-maximal events of X , by (8) we have ¬(T1 ./ T2). Together with (7), this implies

f1; f2 = f2; f1. (23)

We need to show that eval†F (X) contains at most one element. Since e1 and e2 were chosen arbitrarily, in order to discharge this obligation,it is sufficient to show that

f1(σ1) = f2(σ2). (24)Since e1 and e2 are different X.hb-maximal events in X ,

ctxt(e1, X2) = ctxt(e1, X) = Y1 ∧ ctxt(e2, X1) = ctxt(e2, X) = Y2.

Thus, by the definition of eval†F we have

σ1 = eval†F (X1) = f2(σ3) ∧ σ2 = eval†F (X2) = f1(σ3).Hence, (24) follows from

f1(f2(σ3)) = f2(f1(σ3)),

which itself holds because of (23). ut

A.3 Proof of Corollary 8Assume a state-based invariant I ⊆ State. We construct the corresponding event-based invariant I as the set of all executions that evaluate toa state in I: I = eval−1

F (I). Then the conclusion Exec(T ,F) ⊆ I of the event-based rule implies the conclusion Exec(T ,F) ⊆ eval−1F (I)

of the state-based rule.We now show that the premiss of the state-based rule implies that of the event-based rule. Assume state-based guarantees G0 and G that

satisfy S1-S3. We construct the corresponding event-based guarantee G by describing the change to the knowledge of a replica brought onby incorporating the effect of an operation satisfying the state-based guarantees G0 and G:

G = (X,Y ) | ∃e. (Y.E −X.E) = e ∧X v Y ∧ (evalF (X), evalF (Y )) ∈ (G0 ∪G(Y.tok(e))). (25)

Thus, the guarantee G consists of pairs (X,Y ), where Y extends X by a single event e representing the operation, and the two executionsevaluate to a pair of states in G0 or G(τ) for some token τ acquired by e.

It remains to prove that the event-based guarantee G satisfies conditions E1-E3. The first condition E1 holds because

evalF (Xinit) = σinit ∈ I.

Page 16: ’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

The next E2 holds for a similar reason that I is preserved by G0 and G(τ) for all τ . Formally, the following derivation shows E2:

evalF (G(I)) = evalF (Y ) | ∃X. X ∈ I ∧ (X,Y ) ∈ G⊆ evalF (Y ) | ∃X. evalF (X) ∈ I ∧ (evalF (X), evalF (Y )) ∈ (G0 ∪

⋃τ

G(τ))

⊆ I.

We will now show that S3 implies E3. Assume that for some X,X ′, X ′′ and e ∈ X ′′.E, the premiss of E3 holds:

X ∈ I ∧X ′ = X ′′|X′′.E−e ∧X ′′ ∈ Exec(T ,F) ∧ e ∈ max(X ′′) ∧X = ctxt(e,X ′′) ∧ (X,X ′) ∈ G∗. (26)

We need to show that(X ′, X ′′) ∈ G. (27)

Leto = X ′′.oper(e), T = X ′′.tok(e), σ = evalF (X), σ′ = evalF (X ′), and σ′′ = evalF (X ′′).

Note that since X ′′ ∈ Exec(T ,F),

T = X ′′.tok(e) = F toko (σ). (28)

Furthermore, X ′ = X ′′|X′′.E−e and e ∈ max(X ′′). Thus, the desired relationship in (27) follows if we show

(σ′, σ′′) = (evalF (X ′), evalF (X ′′)) ∈(G0 ∪G(F tok

o (σ))). (29)

We will prove the relationship in (29).Since X ′′ ∈ Exec(F) and e ∈ max(X ′′), by Lemma 4 we have

σ′′ = evalF (X ′′) = F effo (σ)(σ′),

so our proof obligation in (29) means the same thing as

(σ′,F effo (σ)(σ′)) ∈

(G0 ∪G(F tok

o (σ))).

By the condition S3 of our state-based proof rule and the fact that T = F toko (σ), this relationship follows if we prove

σ ∈ I ∧ (σ, σ′) ∈ (G0 ∪G(T⊥))∗.

The first conjunct holds because of X ∈ I, which is equivalent to

σ = evalF (X) ∈ I.The second conjunct will be shown in the rest of this proof.

Since (X,X ′) ∈ G∗, there exist a sequence of executions X1, . . . , Xn and a sequence of events e2, . . . , en for n ≥ 1, such that

(X1 = X) ∧ (Xn = X ′) ∧(∀1 < i ≤ n. Xi−1 v Xi ∧ ei = (Xi.E −Xi−1.E) ∧ (evalF (Xi−1), evalF (Xi)) ∈ (G0 ∪G(Xi.tok(ei)))).

(30)

Notice that this implies∀1 ≤ i ≤ n. Xi v X ′. (31)

By the choice of X,X ′, X ′′ and e, no events from X ′.E −X.E are related to e via (X ′′.hb ∪ (X ′′.hb−1)). So no events in X ′.E −X.Eare labelled with tokens conflicting with some in T (which is the set of tokens for e). In particular, this means that

∀e′ ∈ (X ′.E −X.E). (X ′.tok(e′)) ⊆ T⊥.From this, (30) and (31), it follows that

∀1 < i ≤ n. Xi.tok(ei) ⊆ T⊥.Thus, (30) implies

∀1 < i ≤ n. (evalF (Xi−1), evalF (Xi)) ∈ (G0 ∪G(Xi.tok(ei))) ⊆ (G0 ∪G(T⊥)).

Since X1 = X and Xn = X ′, the above formula implies the desired

(σ, σ′) = (evalF (X), evalF (X ′)) ∈ (G0 ∪G(T⊥))∗.

ut

A.4 Proof of Proposition 9Consider PSI operations o1, o2 and database states σ1, σ2 such that

¬(F tokS,o1(σ1) ./ F tok

S,o2(σ2)). (32)

LetS1 = Supdates

o1 (σ1); D1 = dom(S1); f1 = F effo1 (σ1);

S2 = Supdateso2 (σ2); D2 = dom(S2); f2 = F eff

o2 (σ2).

Page 17: ’Cause I’m Strong Enough: Reasoning about Consistency Choices …software.imdea.org/~gotsman/papers/logic-popl16-ext.pdf · 2015. 12. 20. · Reasoning about Consistency Choices

We should show that∀σ, x. (f1 f2)(σ)(x) = (f2 f1)(σ)(x). (33)

Note that by (32), D1 ∩ D2 = ∅. Thus, it suffices to show the equation in (33) for the following three cases: (1) x 6∈ D1 ∪ D2; (2)x ∈ D1 −D2; (3) x ∈ D2 −D1. Pick a state σ and an object x. If x 6∈ D1 ∪D2,

(f1 f2)(σ)(x) = f2(σ)(x) = σ(x) = f1(σ)(x) = (f2 f1)(σ)(x).

If x ∈ D1 −D2,(f1 f2)(σ)(x) = S1(x) = f1(σ)(x) = (f2 f1)(σ)(x).

If x ∈ D2 −D1,(f1 f2)(σ)(x) = f2(σ)(x) = S2(x) = (f2 f1)(σ)(x).

ut

A.5 Proof of Theorem 10In our proof, we use the liftings of the PSI token system TPSI = (Token, ./), its operation semantics FS and executions X:

T † = (Token†, ./†), F†, X†

which are defined as follows:Token† = P(Token);

./† = (T, T ′) | ∃τ, τ ′. (τ ∈ T ∧ τ ′ ∈ T ′ ∧ τ ./ τ ′);F†o (σ) = (F val

S,o(σ),F effS,o(σ), F tok

S,o(σ));X† = (X.E, X.oper, X.rval, ((λT. T) X.tok), X.hb).

The only thing that this lifting does is to enrich the conflict algebra by populating it with subsets of tokens. It is easy to see that T † is indeeda conflict algebra. Note that T ./ T ′ if and only if T ./† T ′, and that every event in X† is annotated with the singleton set T forsome T ⊆ Token. Thus, F† inherits the commutativity property from FS , and X† all the required properties to be an execution from X .Furthermore, the lifting preserves the consistency of an execution, and it commutes with the evaluation operator:

1. X ∈ Exec(TPSI,FS) ⇐⇒ X† ∈ Exec(T †,F†) for all X .

2. evalFS (X) = evalF†(X†) for all X ∈ Exec(TPSI,FS).

Thus, in order to show Exec(TPSI,FS) ⊆ eval−1FS

(I) for a given invariant I ⊆ State, we just need to prove that

Exec(T †,F†) ⊆ eval−1

F†(I). (34)

This can be done using our state-based rule. In particular, if the conditions PSI1-PSI3 of the PSI rule hold for some G, we can establish (34)by instantiating the state-based rule with the following G†0 and G†:

G†0 = (σ, σ) | σ ∈ I;G†(T ) = (σ, σ′) ∈ G | ∀x. τx 6∈ T =⇒ σ(x) = σ′(x).

The condition S1 is identical to PSI1. The condition S2 holds because G(I) ⊆ I by PSI2. To show that S3 holds, consider o, σ, σ′ such that

σ ∈ I ∧ (σ, σ′) ∈ (G†0 ∪G†((F†o )tok(σ)⊥))∗. (35)

ButG†((F†o )tok(σ)⊥) = G†(Token−F tok

S,o(σ)).

Thus, the second conjunct of (35) is equivalent to

(σ, σ′) ∈ (G ∩=x|τx∈F tokS,o

(σ))∗ = (G ∩=

dom(Supdateso (σ))

)∗.

This means that (35) is equivalent to the hypothesis of PSI3, and implies that we can use PSI3. We do so and get

(σ′,F effS,o(σ)(σ′)) ∈ G.

This implies the desired

(σ′, (F†o )eff(σ)(σ′)) ∈ G†((F†o )tok(σ)),

because F effS,o(σ) = (F†o )eff(σ) and G†((F†o )tok(σ)) is the same as

(σ, σ′) ∈ G | ∀x. τx 6∈ F tokS,o(σ) =⇒ σ(x) = σ′(x).

ut