Understanding Inconsistency in Azure Cosmos DB with TLA+

Finn Hackett, Joshua Rowe, Markus A. Kuppe2023

TL;DR

This paper presents a concise, formal TLA+ specification of Azure Cosmos DB's client-observable consistency semantics. This specification, significantly simpler than existing ones, uncovered previously undocumented behaviors, improved public documentation, and helped diagnose a critical Azure outage.

What Problem Does This Paper Address?

Understanding and accurately documenting the consistency guarantees of distributed databases is notoriously difficult. While systems might function correctly at an implementation level, subtle, user-observable behaviors can lead to bugs in dependent systems. Existing formal specifications often focus on implementation correctness and are too complex for easy understanding of user-facing semantics. This paper tackles the challenge of precisely defining what a client should expect to see from Cosmos DB, even under failure conditions, and how to communicate this effectively.

Methods

The core of this work is the development of a formal specification of Azure Cosmos DB's client-observable semantics using the TLA+ language. The authors focused on modeling the essential read and write operations across Cosmos DB's five consistency levels, abstracting away from low-level implementation details. This minimalist approach was achieved through:

  • Iterative Prototyping: Building the specification incrementally, with frequent feedback from a Cosmos DB developer.
  • Focus on Client Semantics: Prioritizing what a client observes rather than how the system is implemented internally.
  • Model Checking: Using the TLC model checker to verify properties of the specification and explore its behavior.
  • Refinement: Comparing their specification against existing, more detailed TLA+ models of Cosmos DB to ensure their model produced a superset of behaviors.
  • PlusCal: Using PlusCal, a high-level imperative language within TLA+, to model specific scenarios like a production outage.

Datasets

Not discussed in the paper. The authors developed a formal specification and used model checking for verification, rather than relying on traditional datasets.

Key Insights and Ideas

  • Simplicity is Powerful: A surprisingly small and conceptually simple TLA+ model can capture a vast range of complex, user-observable behaviors in a large-scale distributed system like Cosmos DB.
  • Documentation Gaps are Real: Even for well-established services, public documentation can have subtle inaccuracies or omissions regarding edge-case behaviors, which formal modeling can uncover.
  • Client-Facing Semantics Matter: Focusing on what users see, rather than internal implementation, is crucial for identifying and preventing bugs in dependent systems.
  • Consistency is a Hierarchy (Mostly): While stronger read consistency levels generally offer a subset of weaker ones, the relationship between write consistency levels and read consistency levels can be counter-intuitive, especially when considering non-durable writes.
  • Formal Models as Debugging Tools: TLA+ specifications can serve as powerful tools for diagnosing the root causes of complex production outages by precisely modeling the observed interactions.

Key Results

  • Concise Specification: A 390-line TLA+ specification of Cosmos DB's core client-facing semantics, covering all 5 consistency levels and handling arbitrary configurations of regions, replicas, and failures.
  • Documentation Improvements: Two key issues in Cosmos DB's public documentation were identified and subsequently addressed by Microsoft.
  • Outage Diagnosis: The specification was used to model a critical 28-day Azure outage, leading to the identification of a previously unknown safety issue related to session consistency and unpropagated writes. A proposed fix involving passing session tokens between processes was modeled and verified.
  • Behavioral Equivalence: The paper demonstrates that "Consistent Prefix" and "Eventual Consistency" behave identically from a client's perspective for atomic single reads and writes.
  • Anomalous Behaviors Identified: Several previously undocumented but real behaviors were discovered, including "dirty reads" (reading incomplete writes even with strong consistency), the possibility of reading values from failed writes, and weaker guarantees for bounded staleness reads under strongly consistent writes.

Limitations and Future Work

  • No Direct Implementation Linkage: The authors acknowledge that their model is not directly linked to the Cosmos DB implementation, meaning errors could theoretically exist that are only discoverable through manual review. However, they argue the model's small size makes fixing such errors feasible.
  • Transactions Not Modeled: The current specification does not cover transactions, which are layered on top of the raw reads and writes and can exhibit different behaviors. The authors suggest formalizing this as a future direction.
  • Future Work: The authors envision their model being used by the Cosmos DB development team for reasoning about client-facing behavior and for exploring refinement relationships with internal specifications. They also suggest that dependent systems could incorporate their model to prevent similar outages. The model could also serve as a foundation for building client-level testing tools specialized for Cosmos DB.

Summary updated

BibTeX
@misc{hackett2023understanding,
  author = {Finn Hackett and Joshua Rowe and Markus A. Kuppe},
  title = {{Understanding Inconsistency in Azure Cosmos DB with TLA+}},
  year = {2023},
  doi = {10.1109/icse-seip58684.2023.00006},
}
Figure