|
| 1 | +# The Watch System |
| 2 | + |
| 3 | +This document explains the "Watch" backend system, which powers the real-time capabilities and standard read/write operations of the Firestore SDKs. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +"Watch" is the internal name for the high-scale system that the SDKs interact with. It serves two primary purposes: |
| 8 | + |
| 9 | +1. **Reads & Writes**: It is the main entry point for standard document operations. |
| 10 | +2. **Real-Time Listeners**: It powers the `onSnapshot` live updates by tracking database changes and pushing them to clients. |
| 11 | + |
| 12 | +## Architecture: The Reverse Proxy |
| 13 | + |
| 14 | +Watch functions as a massive **Reverse Proxy**. |
| 15 | + |
| 16 | +1. **Connection**: Massive numbers of end-user devices (phones, browsers) connect directly to Watch. |
| 17 | +2. **Routing**: Watch takes the incoming query or write and forwards it to the appropriate underlying storage backend (partition). |
| 18 | +3. **Observation**: For queries, Watch doesn't just fetch data once. It "watches" the backend for any writes that would affect the query results and pushes those changes to the subscribed client. |
| 19 | + |
| 20 | +This architecture allows Firestore to handle millions of concurrent connections while abstracting the complexity of sharding and storage from the client. |
| 21 | + |
| 22 | +## Consistency Guarantees |
| 23 | + |
| 24 | +The Watch system (exposed via the `Listen` endpoint) enables strong consistency between reads and writes. |
| 25 | + |
| 26 | +* **Consistency**: All reads and writes performed through this system are consistent with each other. If you write a document and then immediately read it (or listen to it) via Watch, you will see the latest version. |
| 27 | +* **Authentication**: Watch interacts directly with Firebase Auth to identify the user and enforces Firestore Security Rules for every operation. |
| 28 | + |
| 29 | +> [!IMPORTANT] |
| 30 | +> **Transactions** use a different endpoint (`runTransaction`) and **do not** guarantee consistency with the Watch stream. See [Transactions](./transactions.md) for details. |
| 31 | +
|
| 32 | +### Scalability and Client-Side Logic |
| 33 | +The Watch system operates at a massive scale. To maintain performance, the backend may rely on the SDK to handle certain query complexities, particularly for **local** execution and consistency. |
| 34 | + |
| 35 | +> [!NOTE] |
| 36 | +> **Composite Queries (OR/IN)** |
| 37 | +> While the Watch system supports complex queries (including `OR` and `IN`), the SDK performs significant client-side logic to support them efficiently **locally**. |
| 38 | +> * **Local Indexing**: For local cache execution, the SDK transforms composite queries into **Disjunctive Normal Form (DNF)** (breaking them into simpler sub-queries) to utilize simple field indexes. |
| 39 | +> * **consistency**: The SDK merges results from the Watch stream and local cache to ensure a consistent view. |
| 40 | +
|
| 41 | +This architectural decision explains why you see complex logic like **Composite Query** execution in the [Query Engine](./query-execution.md). The SDK implements this logic to bridge the gap between user intent and Watch's scalability constraints. |
0 commit comments