Skip to content

Conversation

@waleedlatif1
Copy link
Collaborator

@waleedlatif1 waleedlatif1 commented Oct 2, 2025

Summary

  • remove writes to workflow updated_at on position updates for blocks, edges, & subflows

  • previosuly, every position update or any operation for that matter updated the workflow record int he wrofklow table

    • this caused many different operations to contend for the same lock since they were all trying to update the updated_at timestamp of the same record. to resolve this, we removed the writes to updated_at on position changes and made it only for non position-update ops. this will significantly reduce the 200ms avg update time
  • for the logs, this was the pattern before

  1. Scan ALL workflow_execution_logs (millions of rows)
  2. Join to workflow table
  3. Join to permissions WHERE user_id = X
  4. Filter WHERE workspace_id = Y
  5. Return 100 rows
  • pattern now
  1. Start with workflow WHERE workspace_id = Y (filters to ~50-100 rows)
  2. Join to workflow_execution_logs for only those workflows
  3. Verify permissions for user
  4. Return 100 rows 

Type of Change

  • Other: Performance

Testing

Manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

…ition updates for blocks, edges, & subflows
@vercel
Copy link

vercel bot commented Oct 2, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
sim Ready Ready Preview Comment Oct 2, 2025 6:50pm
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
docs Skipped Skipped Oct 2, 2025 6:50pm

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Overview

Summary

This PR implements a targeted performance optimization to reduce database lock contention in workflow operations. The change modifies the database transaction logic in `apps/sim/socket-server/database/operations.ts` to conditionally update the workflow's `updated_at` timestamp based on the operation type.

What Changed:
Previously, every workflow operation - including high-frequency position updates for blocks, edges, and subflows - would immediately update the workflow record's updated_at timestamp at the start of each database transaction. This caused multiple concurrent operations to contend for locks on the same workflow record, resulting in an average 200ms update time.

The solution introduces a conditional check: if (op !== 'update-position') before updating the workflow timestamp. This means position updates (which are cosmetic changes that don't affect workflow logic) no longer trigger timestamp updates, while all other meaningful operations continue to update the timestamp as expected.

How It Integrates:
This change fits into the workflow management system's broader architecture where the updateLastSaved function (shown in the context) handles client-side state persistence, while this database operation handles server-side persistence. The optimization specifically targets the server-side database layer to reduce contention without affecting the client-side workflow state management or the overall user experience.

The change also removes debug logging for position updates and reorders the transaction flow to handle operations before timestamp updates, creating a cleaner and more performant execution path.

PR Description Notes:

  • Minor typo: "previosuly" should be "previously"
  • Minor typo: "wrofklow" should be "workflow"
  • Minor typo: "int he" should be "in the"

Important Files Changed

Changed Files
Filename Score Overview
apps/sim/socket-server/database/operations.ts 4/5 Adds conditional logic to skip workflow timestamp updates for position-only operations, reducing database lock contention

Confidence score: 4/5

  • This PR is safe to merge with minimal risk as it addresses a clear performance bottleneck without breaking existing functionality
  • Score reflects a well-targeted optimization that maintains data consistency while improving performance through reduced lock contention
  • The single file change is straightforward and the conditional logic is easy to understand and maintain

Sequence Diagram

sequenceDiagram
    participant User
    participant SocketServer as Socket Server
    participant DB as Database
    participant WorkflowTable as workflow table
    participant BlocksTable as workflow_blocks table
    
    User->>SocketServer: "Send position update operation"
    SocketServer->>SocketServer: "Parse operation (target: block, op: update-position)"
    SocketServer->>DB: "Start transaction"
    
    DB->>SocketServer: "Transaction started"
    SocketServer->>SocketServer: "handleBlockOperationTx()"
    SocketServer->>BlocksTable: "UPDATE workflow_blocks SET positionX, positionY, updatedAt"
    BlocksTable->>SocketServer: "Position updated"
    
    SocketServer->>SocketServer: "Check: op !== 'update-position'"
    Note over SocketServer: Skip workflow.updatedAt update for position changes
    
    SocketServer->>DB: "Commit transaction"
    DB->>SocketServer: "Transaction committed"
    SocketServer->>User: "Position update complete"
    
    User->>SocketServer: "Send non-position operation (e.g., add block)"
    SocketServer->>SocketServer: "Parse operation (target: block, op: add)"
    SocketServer->>DB: "Start transaction"
    
    DB->>SocketServer: "Transaction started"
    SocketServer->>SocketServer: "handleBlockOperationTx()"
    SocketServer->>BlocksTable: "INSERT new block"
    BlocksTable->>SocketServer: "Block inserted"
    
    SocketServer->>SocketServer: "Check: op !== 'update-position'"
    Note over SocketServer: Update workflow.updatedAt for non-position changes
    SocketServer->>WorkflowTable: "UPDATE workflow SET updatedAt"
    WorkflowTable->>SocketServer: "Workflow timestamp updated"
    
    SocketServer->>DB: "Commit transaction"
    DB->>SocketServer: "Transaction committed"
    SocketServer->>User: "Operation complete"
Loading

1 file reviewed, no comments

Edit Code Review Agent Settings | Greptile

@vercel vercel bot temporarily deployed to Preview – docs October 2, 2025 18:46 Inactive
@waleedlatif1 waleedlatif1 merged commit 1513862 into staging Oct 2, 2025
10 checks passed
@waleedlatif1 waleedlatif1 deleted the improvemnet/performance branch October 7, 2025 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants