Skip to main content

Background

The demos in the powersync-js monorepo provide a minimal working example that illustrate the use of PowerSync with different frameworks. The demos are therefore not necessarily optimized for performance and can therefore be improved. This tutorial demonstrates how to improve the Supabase Connector’s performance by implementing two batching strategies that reduce the number of database operations.

Batching Strategies

The two batching strategies that will be implemented are:
  1. Sequential Merge Strategy, and
  2. Pre-sorted Batch Strategy
Overview:
  • Merge adjacent PUT and DELETE operations for the same table
  • Limit the number of operations that are merged into a single API request to Supabase
Shoutout to @christoffer_configura for the original implementation of this optimization.
Overview:
  • Create three collections to group operations by type:
    • putOps: For PUT operations, organized by table name
    • deleteOps: For DELETE operations, organized by table name
    • patchOps: For PATCH operations (partial updates)
  • Loop through all operations, sort them into the three collections, and then process all operations in batches.

Differences

Sequential merge strategy

  • Pre-sorts all operations by type and table
  • Processes each type in bulk after grouping

Pre-sorted batch strategy

  • Processes operations sequentially
  • Merges consecutive operations of the same type up to a batch limit
  • More dynamic/streaming approach

Sequential merge strategy

  • Uses a sliding window approach with MERGE_BATCH_LIMIT
  • Merges consecutive operations up to the limit
  • More granular control over batch sizes
  • Better for mixed operation types

Pre-sorted batch strategy

  • Groups ALL operations of the same type together
  • Executes one bulk operation per type per table
  • Better for large numbers of similar operations

Key Similarities and Differences

Key Similarities

Handling of CRUD operations (PUT, PATCH, DELETE) to sync local changes to Supabase
Transaction management with getNextCrudTransaction()
Implement similar error handling for fatal and retryable errors
Complete the transaction after successful processing

Key Differences

Operation grouping strategy
Batching methodology

Use cases

Sequential Merge Strategy

You need more granular control over batch sizesYou want more detailed operation loggingYou need to handle mixed operation types more efficiently
Best for: Mixed operation types
Optimizes for: Memory efficiency
Trade-off: Potentially more network requests

Pre-sorted Batch Strategy

You have a large number of similar operations.You want to minimize the number of network requests.

Best for: Large volumes of similar operations
Optimizes for: Minimal network requests
Trade-off: Higher memory usage