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(sync-service): Reduce memory use by hibernating idle shape processes #2240

Merged
merged 4 commits into from
Jan 22, 2025

Conversation

robacourt
Copy link
Contributor

@robacourt robacourt commented Jan 22, 2025

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).
Screenshot 2025-01-22 at 12 06 41
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 to 50ms. 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.
Screenshot 2025-01-22 at 12 09 38
As you can see, memory use flatlines at 120MB with hibernation rather than blowing up to 2000MB without hibernation.

@robacourt robacourt marked this pull request as ready for review January 22, 2025 13:11
Copy link
Contributor

@magnetised magnetised left a comment

Choose a reason for hiding this comment

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

Beautifully simple change after a ton of research. Thanks!

Just FYI we'll be unable to publish electric to hex while we keep a github-based dependency, so here's hoping they accept that patch.

@robacourt
Copy link
Contributor Author

Just FYI we'll be unable to publish electric to hex while we keep a github-based dependency, so here's hoping they accept that patch.

Good point. I don't think they're going to be quick so I guess I'll have to publish the fork to hex.

Copy link

netlify bot commented Jan 22, 2025

Deploy Preview for electric-next ready!

Name Link
🔨 Latest commit 212c566
🔍 Latest deploy log https://app.netlify.com/sites/electric-next/deploys/6791241b9f336400070c2a20
😎 Deploy Preview https://deploy-preview-2240--electric-next.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@robacourt robacourt merged commit 7c72c1e into main Jan 22, 2025
33 checks passed
@robacourt robacourt deleted the rob/hibernate branch January 22, 2025 17:09
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