Firestore是否支持ACID事务?Serverless场景下ACID支持程度咨询
Hey there! Let’s break down your questions about Firestore and ACID transactions clearly:
Absolutely yes. Firestore provides transactional operations that fully adhere to ACID principles for operations within a single transaction scope. You can wrap multiple read/write operations into a transaction, and Firestore handles the ACID guarantees for you out of the box.
Firestore’s support for each ACID property is robust, with a few practical caveats to keep in mind:
- Atomicity: Every operation in a transaction is all-or-nothing. If any step fails (whether due to a concurrency conflict, permission error, or other issue), Firestore automatically rolls back the entire transaction—no partial changes will be left lingering in your database. This ensures you never end up with inconsistent data from half-completed workflows.
- Consistency: Firestore maintains strict data consistency throughout transactions. It enforces your security rules and any data validation constraints (like required fields or field types) during the transaction’s execution, so you can’t end up with an invalid database state before or after the transaction completes.
- Isolation: Firestore uses snapshot isolation for transactions. When you initiate a transaction, you get a consistent snapshot of the relevant data. Changes from other concurrent transactions won’t be visible to your transaction until it commits successfully. This eliminates dirty reads, non-repeatable reads, and phantom reads within the transaction’s context.
- Durability: Once a transaction is committed successfully, Firestore guarantees those changes are persisted to durable, replicated storage. Even if there’s a system failure immediately after commit, your changes won’t be lost—they’ll be restored once the system recovers.
A key practical limit to note: each Firestore transaction can handle a maximum of 500 document operations (creates, updates, deletes, or reads used for transaction checks), and all documents involved must live in the same database. If you need to work across more documents or separate databases, you’ll have to split your logic into multiple transactions and manage eventual consistency between them manually.
内容的提问来源于stack exchange,提问作者Devices For

