|
| 1 | +--- |
| 2 | +title: Transactions And Lock Modes in Azure Service Fabric Reliable Collections | Microsoft Docs |
| 3 | +description: Azure Service Fabric Reliable State Manager and Reliable Collections Transactions and Locking. |
| 4 | +services: service-fabric |
| 5 | +documentationcenter: .net |
| 6 | +author: mcoskun |
| 7 | +manager: timlt |
| 8 | +editor: masnider,rajak |
| 9 | + |
| 10 | +ms.assetid: 62857523-604b-434e-bd1c-2141ea4b00d1 |
| 11 | +ms.service: service-fabric |
| 12 | +ms.devlang: dotnet |
| 13 | +ms.topic: article |
| 14 | +ms.tgt_pltfrm: na |
| 15 | +ms.workload: required |
| 16 | +ms.date: 5/1/2017 |
| 17 | +ms.author: mcoskun |
| 18 | + |
| 19 | +--- |
| 20 | +# Transactions and lock modes in Azure Service Fabric Reliable Collections |
| 21 | + |
| 22 | +## Transaction |
| 23 | +A transaction is a sequence of operations performed as a single logical unit of work. |
| 24 | +A transaction must exhibit the following ACID properties. (see: https://technet.microsoft.com/en-us/library/ms190612) |
| 25 | +* **Atomicity**: A transaction must be an atomic unit of work. In other words, either all its data modifications are performed, or none of them is performed. |
| 26 | +* **Consistency**: When completed, a transaction must leave all data in a consistent state. All internal data structures must be correct at the end of the transaction. |
| 27 | +* **Isolation**: Modifications made by concurrent transactions must be isolated from the modifications made by any other concurrent transactions. The isolation level used for an operation within an ITransaction is determined by the IReliableState performing the operation. |
| 28 | +* **Durability**: After a transaction has completed, its effects are permanently in place in the system. The modifications persist even in the event of a system failure. |
| 29 | + |
| 30 | +### Isolation levels |
| 31 | +Isolation level defines the degree to which the transaction must be isolated from modifications made by other transactions. |
| 32 | +There are two isolation levels that are supported in Reliable Collections: |
| 33 | + |
| 34 | +* **Repeatable Read**: Specifies that statements cannot read data that has been modified but not yet committed by other transactions and that no other transactions can modify data that has been read by the current transaction until the current transaction finishes. For more details, see [https://msdn.microsoft.com/library/ms173763.aspx](https://msdn.microsoft.com/library/ms173763.aspx). |
| 35 | +* **Snapshot**: Specifies that data read by any statement in a transaction is the transactionally consistent version of the data that existed at the start of the transaction. |
| 36 | + The transaction can recognize only data modifications that were committed before the start of the transaction. |
| 37 | + Data modifications made by other transactions after the start of the current transaction are not visible to statements executing in the current transaction. |
| 38 | + The effect is as if the statements in a transaction get a snapshot of the committed data as it existed at the start of the transaction. |
| 39 | + Snapshots are consistent across Reliable Collections. |
| 40 | + For more details, see [https://msdn.microsoft.com/library/ms173763.aspx](https://msdn.microsoft.com/library/ms173763.aspx). |
| 41 | + |
| 42 | +Reliable Collections automatically choose the isolation level to use for a given read operation depending on the operation and the role of the replica at the time of transaction's creation. |
| 43 | +Following is the table that depicts isolation level defaults for Reliable Dictionary and Queue operations. |
| 44 | + |
| 45 | +| Operation \ Role | Primary | Secondary | |
| 46 | +| --- |:--- |:--- | |
| 47 | +| Single Entity Read |Repeatable Read |Snapshot | |
| 48 | +| Enumeration, Count |Snapshot |Snapshot | |
| 49 | + |
| 50 | +> [!NOTE] |
| 51 | +> Common examples for Single Entity Operations are `IReliableDictionary.TryGetValueAsync`, `IReliableQueue.TryPeekAsync`. |
| 52 | +> |
| 53 | +
|
| 54 | +Both the Reliable Dictionary and the Reliable Queue support Read Your Writes. |
| 55 | +In other words, any write within a transaction will be visible to a following read |
| 56 | +that belongs to the same transaction. |
| 57 | + |
| 58 | +## Locks |
| 59 | +In Reliable Collections, all transactions implement rigorous two phase locking: a transaction does not release |
| 60 | +the locks it has acquired until the transaction terminates with either an abort or a commit. |
| 61 | + |
| 62 | +Reliable Dictionary uses row level locking for all single entity operations. |
| 63 | +Reliable Queue trades off concurrency for strict transactional FIFO property. |
| 64 | +Reliable Queue uses operation level locks allowing one transaction with `TryPeekAsync` and/or `TryDequeueAsync` and one transaction with `EnqueueAsync` at a time. |
| 65 | +Note that to preserve FIFO, if a `TryPeekAsync` or `TryDequeueAsync` ever observes that the Reliable Queue is empty, they will also lock `EnqueueAsync`. |
| 66 | + |
| 67 | +Write operations always take Exclusive locks. |
| 68 | +For read operations, the locking depends on a couple of factors. |
| 69 | +Any read operation done using Snapshot isolation is lock free. |
| 70 | +Any Repeatable Read operation by default takes Shared locks. |
| 71 | +However, for any read operation that supports Repeatable Read, the user can ask for an Update lock instead of the Shared lock. |
| 72 | +An Update lock is an asymmetric lock used to prevent a common form of deadlock that occurs when multiple transactions lock resources for potential updates at a later time. |
| 73 | + |
| 74 | +The lock compatibility matrix can be found in the following table: |
| 75 | + |
| 76 | +| Request \ Granted | None | Shared | Update | Exclusive | |
| 77 | +| --- |:--- |:--- |:--- |:--- | |
| 78 | +| Shared |No conflict |No conflict |Conflict |Conflict | |
| 79 | +| Update |No conflict |No conflict |Conflict |Conflict | |
| 80 | +| Exclusive |No conflict |Conflict |Conflict |Conflict | |
| 81 | + |
| 82 | +Time-out argument in the Reliable Collections APIs is used for deadlock detection. |
| 83 | +For example, two transactions (T1 and T2) are trying to read and update K1. |
| 84 | +It is possible for them to deadlock, because they both end up having the Shared lock. |
| 85 | +In this case, one or both of the operations will time out. |
| 86 | + |
| 87 | +This deadlock scenario is a great example of how an Update lock can prevent deadlocks. |
| 88 | + |
| 89 | +## Next steps |
| 90 | +* [Working with Reliable Collections](service-fabric-work-with-reliable-collections.md) |
| 91 | +* [Reliable Services notifications](service-fabric-reliable-services-notifications.md) |
| 92 | +* [Reliable Services backup and restore (disaster recovery)](service-fabric-reliable-services-backup-restore.md) |
| 93 | +* [Reliable State Manager configuration](service-fabric-reliable-services-configuration.md) |
| 94 | +* [Developer reference for Reliable Collections](https://msdn.microsoft.com/library/azure/microsoft.servicefabric.data.collections.aspx) |
| 95 | + |
0 commit comments