feat(sync-service): Reduce memory use by hibernating idle shape processes #2240
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Potential fix for #2229
Electric's memory footprint can get very large due to shape processes holding on to memory even when idle. This PR hibernates idle shape processes after 30 seconds of inactivity which garbage collects unused memory.
This first graph shows 10MB changes coming in for 100 shapes (2 shapes per second). You can see this taking up 2000MB of memory which is not released even though the memory is no longer needed (it's been written to disk).
Not only is the memory not released, but the memory is amplified. In this example it's amplified by 2x since 2000MB is twice the 1000MB needed (10MB * 100 shapes). This is due to a binary copy that happens when CubDB persists data to file. Amplification can increase further do to having a binary version of the change as well as an Elixir term version of the change which is used for where clause filtering.
This second graph is with
ELECTRIC_SHAPE_HIBERNATE_AFTER
set to50ms
. A value this short is unlikely to be used in production but allows the effect to be seen quickly for the purposes of this benchmark.As you can see, memory use flatlines at 120MB with hibernation rather than blowing up to 2000MB without hibernation.