Skip to content

Commit 5ef55fa

Browse files
committed
docs(firestore): Introducing "Watch" as a concept
1 parent b92beda commit 5ef55fa

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

packages/firestore/devdocs/architecture.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ The SDK is composed of several key components that work together to provide the
2626
* **Query Engine**: Determines the most efficient strategy (Index vs. Scan) to identify documents matching a query in the local cache.
2727
* **Overlays**: A performance-optimizing cache that stores the calculated effect of pending mutations from the Mutation Queue on documents. Instead of re-applying mutations every time a document is read, the SDK computes this "overlay" once and caches it, allowing the Local View to be constructed more efficiently.
2828
* For a detailed breakdown of the IndexedDB structure and tables, see [Persistence Schema](./persistence-schema.md).
29-
* **Remote Store**: The component responsible for all network communication with the Firestore backend. It manages the gRPC streams for reading and writing data, and it abstracts away the complexities of the network protocol from the rest of the SDK.
29+
* **Remote Store**: The component responsible for all network communication with the Firestore backend.
30+
* It manages the **Watch Stream** (see **[The Watch System](./watch.md)**) for reading and listening to data.
31+
* It manages the gRPC streams for writing data.
32+
* It abstracts away the complexities of the network protocol from the rest of the SDK.
3033
* **Persistence Layer**: The underlying storage mechanism used by the Local Store to persist data on the client. In the browser, this is implemented using IndexedDB.
3134

3235
The architecture and systems within the SDK map closely to the directory structure, which helps developers navigate the codebase. Here is a mapping of the core components to their corresponding directories.

packages/firestore/devdocs/query-execution.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ To support efficient querying without blocking the main thread, the SDK utilizes
3232

3333
## Composite Queries (OR / IN)
3434

35-
Queries using `OR` or `IN` are not executed as a single monolithic scan. The SDK transforms these into **Disjunctive Normal Form (DNF)**—essentially breaking them into multiple sub-queries.
35+
Queries using `OR` or `IN` are not executed as a single monolithic scan.
36+
37+
> [!NOTE]
38+
> **Scalability & Watch**: While functionality exists to run these queries against the backend, the SDK implements **Disjunctive Normal Form (DNF)** transformation primarily to enable efficient **local** execution using simpler indexes (as seen in `IndexedDbIndexManager`). This allows the SDK to support complex queries offline or against the cache without requiring full table scans. [See Watch System](./watch.md) for more on the backend interaction.
39+
40+
The SDK transforms these into **Disjunctive Normal Form (DNF)**—essentially breaking them into multiple sub-queries.
3641

3742
* **Execution**: Each sub-query is executed independently using the strategies above (Index vs. Scan).
3843
* **Union**: The resulting sets of document keys are unioned together in memory to produce the final result.

packages/firestore/devdocs/transactions.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ Transactions utilize a dedicated pathway in the `RemoteStore`/`Datastore` layer.
6262
* Standard writes use `CommitStream` (requires `mutation_queue` persistence).
6363
* **Transactions** use direct Unary RPCs (`BatchGetDocuments` and `Commit`) via the underlying `Datastore` helper.
6464

65+
> [!WARNING]
66+
> **Consistency Warning**: Transactions use a different endpoint (`runTransaction`) than the standard `Listen` (Watch) system. As a result, they **do not** guarantee consistency with the Watch stream. A write committed via Watch might not be immediately visible to a transaction, and vice versa, due to the distributed nature of the backend.
67+
6568
## Constraints
6669

6770
* **Online Only**: Because they bypass the local cache and require server-side verification, transactions require an active network connection. They will fail if the client is offline.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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

Comments
 (0)