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.
This PR Addresses point 5 from the conclusions of the image loading performance analysis.
An in-memory cache check and insert is introduced in all user model methods that return a single user (with their media progress) from the database. Caching this saves around 15 ms per authenticated request.
The cache doesn't have a TTL, and is invalidated only if needed (i.e. when changes to the user were not made through the cached object). I have convinced myself that all relevant writes to user and media progress are made through user model methods that are covered by a call to the
maybeInvalidate
method (and specifically increateUpdateMediaProgressFromPayload
)Note that this covers point 5, but I've decided not address point 4 (Remove media progress from req.user), since at this point media progress is quite entangled with req.user and requires quite a few changes in the code, which aren't worth it since the cache already solves the db performance issue. I think it would still be worthwhile disentangle them in the future, but this change makes it less urgent.