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:- Sequential Merge Strategy, and
- Pre-sorted Batch Strategy
Sequential Merge Strategy
Sequential Merge Strategy
Overview:
- Merge adjacent
PUT
andDELETE
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.
Pre-sorted Batch Strategy
Pre-sorted Batch Strategy
Overview:
- Create three collections to group operations by type:
putOps
: ForPUT
operations, organized by table namedeleteOps
: ForDELETE
operations, organized by table namepatchOps
: ForPATCH
operations (partial updates)
- Loop through all operations, sort them into the three collections, and then process all operations in batches.
Differences
Operation grouping strategy
Operation grouping strategy
Batching methodology
Batching methodology
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
Implement similar error handling for fatal and retryable errors
Complete the transaction after successful processing
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
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
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
Best for: Large volumes of similar operations
Optimizes for: Minimal network requests
Trade-off: Higher memory usage