Performance Optimization for Followers/Following Page Load Times #37
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.
Context
The followers page has been experiencing significant slowdowns in production, leading to a poor user experience. Initial analysis pointed to multiple potential issues, including inefficient database queries, the N+1 query problem, and suboptimal data handling during rendering.
Improvements Made
Database Queries Optimization: Implemented indexing on the users and relationships tables to speed up query execution. This change was guided by the observation of slow database access patterns for follower data retrieval.
Eager Loading to Combat N+1 Queries: Modified the data-fetching logic to use .includes for eager loading associations. This change directly addresses the N+1 query problem observed in our AppMaps, where each follower's details were fetched in separate queries.
Rendering Efficiency: Improved the rendering process by introducing partials and collection rendering for followers. Additionally, implemented caching strategies for parts of the page that are less dynamic, significantly reducing the server's work during page rendering.
Pagination Implementation: Implemented pagination for the followers list to limit the number of records fetched and rendered on each page request. This approach not only speeds up the page load but also improves the user experience by not overwhelming them with too much data at once.
Benchmarking and Monitoring
Utilized Rails' built-in benchmarking tools to measure the performance improvements, with a noticeable decrease in page load times and query execution times. Monitoring will continue to ensure that the changes remain effective under varying loads.
Next Steps
Future considerations include further optimizing the cache strategy and exploring additional areas in the app where similar performance improvement techniques can be applied.