- Published on
How to execute Transactions in Azure Table Storage
Table of Contents
We can perform transactions in Azure Table Storage using Entity Group Transactions (EGTs). EGTs allow us to execute operations on multiple entities within the same partition atomically. Whether we need to perform insertions, deletions, updates, or a combination of these actions on multiple entities concurrently, EGTs provide the capability to ensure that all these operations either succeed as a whole or fail together.
Limitations of EGTs
- EGTs can only be executed on entities within the same partition.
- An entity can appear only once in the transaction, and only one operation can be performed on it.
- A transaction is restricted to a maximum of 100 entities, and total size of the payload should be less than 4.1943 MB.
- Unlike SQL transactions, EGTs do not support rollbacks.
- Cross-table transactions are not supported.
EGT examples in .NET
Let's view some code examples of EGTs in .NET. If you are unfamiliar with Table Storage's SDK, please refer to Getting Started with Azure Table Storage in .NET.
Adding multiple entities
One of the common example is to insert multiple entities together. Assume we have Order
and a list of OrderItem
and we need to insert them in a single atomic operation. Here's how we can do it.
var userId = "01";
var orderId = "02";
var entities = new List<ITableEntity>
{
new Order
{
PartitionKey = userId,
RowKey = orderId,
TotalPrice = 5.00
},
new OrderItem
{
PartitionKey = userId,
RowKey = $"{orderId}-01",
Product = "Book",
Price = 3.00
},
new OrderItem
{
PartitionKey = userId,
RowKey = $"{orderId}-02",
Product = "Pen",
Price = 2.00
}
};
var batch = new List<TableTransactionAction>();
batch.AddRange(entities.Select(e => new TableTransactionAction(TableTransactionActionType.Add, e)));
await client.SubmitTransactionAsync(batch);
The main thing to note here is that we are converting our list of entities to a list of TableTransactionAction
. This list represents the operations that we want to carry out within a transaction.
Performing mixed operations
EGTs are not limited to operations of a single type. We can combine any kind of operation within a single batch. Let's take a look at an example.
var userId = "01";
var orderId = "02";
var order = new Order
{
PartitionKey = userId,
RowKey = orderId,
TotalPrice = 6.00
};
var orderItem = new OrderItem
{
PartitionKey = userId,
RowKey = $"{orderId}-03",
Product = "Pencil",
Price = 1.00
};
var batch = new List<TableTransactionAction>();
batch.Add(new TableTransactionAction(TableTransactionActionType.UpdateReplace, order));
batch.Add(new TableTransactionAction(TableTransactionActionType.Add, orderItem));
await client.SubmitTransactionAsync(batch);
Deleting multiple entities
Now, let's view the example of deleting entities. If we have a list of entities that need to be deleted, here is how we can do it.
foreach (var entity in entities)
{
batch.Add(new TableTransactionAction(TableTransactionActionType.Delete, entity));
}
await client.SubmitTransactionAsync(batch);
Conclusion
In conclusion, Entity Group Transactions (EGTs) in Azure Table Storage offer a powerful means to execute operations on multiple entities within the same partition as a single atomic transaction. Whether it's inserting, updating, or deleting entities, EGTs ensure that these operations succeed as a whole or fail together, maintaining data integrity. However, it's crucial to be aware of EGT limitations, including partition constraints, size restrictions, lack of rollback support, and the inability to perform cross-table transactions.
In addition, we've explored practical code examples in .NET that demonstrate how to utilize EGTs for various operations, including adding multiple entities and performing mixed operations. By understanding and harnessing the capabilities of EGTs, you can effectively manage data transactions in Azure Table Storage, building robust and reliable solutions for your applications.