Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add optimize recommendation for transactions (#DA-2035) #6515

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions content/700-optimize/300-recordings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ When a recording session ends, Optimize generates recommendations such as:
- [Queries on unindexed columns](/optimize/recommendations/queries-on-unindexed-columns)
- [Repeated query](/optimize/recommendations/repeated-query)
- [Overfetching](/optimize/recommendations/select-returning)
- [Long-running transactions](/optimize/recommendations/long-running-transactions)
- [Using `@db.Money`](/optimize/recommendations/avoid-db-money)

:::info
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
title: 'Long-running transactions'
metaTitle: 'Optimize Recommendations: Avoid long-running transactions'
metaDescription: "Learn about the recommendation provided by Optimize for long-running transaction."
tocDepth: 3
toc: true
---

Optimize provides actionable recommendations to help you identify and resolve performance issues caused by **long-running transactions**.

**Long-running transactions** can negatively impact scalability and resilience by locking resources and holding database connections for extended periods. Below is a common example of a problematic long-running transaction:

```ts
// Example: A single massive transaction performing multiple steps
await prisma.$transaction(async (prisma) => {
const order = await prisma.order.create({
data: {
/* ... */
},
});
await prisma.user.update({
where: { id: userId },
data: { balance: { decrement: order.total } },
});
await prisma.shipping.create({ data: { orderId: order.id /* ... */ } });
// Additional dependent operations
});
```

### What is the problem?

Long-running transactions can cause several critical issues that harm the performance and reliability of your application:

- **Database locks**: Long transactions hold locks on rows, tables, or other resources, preventing access by other queries. This leads to contention and blocking, which can significantly disrupt concurrent operations.

- **Connection tie-ups**: Transactions occupy database connections for their entire duration. With a limited connection pool, this can quickly exhaust available connections, resulting in application-wide slowdowns or failures.

- **Increased contention**: As locks accumulate and connections are tied up, other transactions queue up, creating bottlenecks, higher latency, and reduced throughput.

- **Scalability challenges**: Inefficiencies caused by long transactions are magnified in high-traffic systems, limiting the system’s ability to scale effectively.

- **Fragility**: When a long transaction fails or times out, all intermediate progress is lost. This is especially problematic in workflows with multiple dependent steps, as recovering from partial failures becomes complex and error-prone.

- **Debugging difficulties**: Troubleshooting long-running transactions is challenging due to their multiple steps and potential failures caused by timeouts, deadlocks, or unexpected dependencies.
Loading