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: Move Redis dependency management to background tasks #915

Merged

Conversation

jimwashbrook
Copy link
Contributor

@jimwashbrook jimwashbrook commented Dec 13, 2024

Overview

  1. Split the Contentful dependency management logic to separate class
  2. Have a BackgroundService that can receive + process tasks on another thread async
  3. Move the execution of creating + saving dependency Redis set keys to a background task processed by fix: Namespace naming + code structure #2

Changes

Minor

  • Split the Contentful dependency management logic to separate class
  • Add a class for adding/removing Tasks to be ran in the background
  • Add a HostedService that processes queues from the above

How to review the PR

Run the web app against a cleared Redis cache, compare load times against existing functionality.

Use a Stopwatch for most accurate timings. E.g. in GetOrCreateAsync you could do something like this:

            var stopwatch = new Stopwatch();
            stopwatch.Start();
            var db = await _connectionManager.GetDatabaseAsync(databaseId);
            var redisResult = await GetAsync<T>(db, key);

            if (redisResult.ExistedInCache == true)
            {
                _logger.LogTrace("Cache item with key: {Key} found", key);
                return redisResult.CacheValue;
            }
            else if (redisResult.Errored)
            {
                return await action();
            }

            var result = await CreateAndCacheItemAsync(db, key, action, expiry, onCacheItemCreation);
            stopwatch.Stop();
            _logger.LogInformation("Get and create took {MS}ms", stopwatch.ElapsedMilliseconds);

Additional notes

Some initial testing of the original code, a few minor tweaks I made, and this PR:

Initial requests: 456.3ms

Get and create took 4697ms
Get and create took 70ms
Get and create took 59ms
Get and create took 59ms
Get and create took 66ms
Get and create took 65ms
Get and create took 57ms
Get and create took 80ms
Get and create took 61ms
Get and create took 93ms
Get and create took 3256ms
Get and create took 109ms
Get and create took 57ms
Get and create took 56ms
Get and create took 60ms
Get and create took 84ms
Get and create took 108ms
Get and create took 332ms
Get and create took 76ms
Get and create took 55ms
Get and create took 3297ms
Get and create took 68ms
Get and create took 57ms
Get and create took 54ms
Get and create took 95ms
Get and create took 65ms
Get and create took 56ms
Get and create took 369ms
Get and create took 69ms
Get and create took 59ms

After removing double query - 467.366ms

  Get and create took 3858ms
  Get and create took 69ms
  Get and create took 58ms
  Get and create took 60ms
  Get and create took 61ms
  Get and create took 72ms
  Get and create took 61ms
  Get and create took 636ms
  Get and create took 74ms
  Get and create took 218ms
  Get and create took 4049ms
  Get and create took 56ms
  Get and create took 59ms
  Get and create took 56ms
  Get and create took 60ms
  Get and create took 59ms
  Get and create took 62ms
  Get and create took 73ms
  Get and create took 55ms
  Get and create took 56ms
  Get and create took 3318ms
  Get and create took 139ms
  Get and create took 57ms
  Get and create took 78ms
  Get and create took 57ms
  Get and create took 56ms
  Get and create took 53ms
  Get and create took 397ms
  Get and create took 59ms
  Get and create took 55ms

Removing improer async usage, running both at same time:
422.3ms

  Get and create took 4042ms
  Get and create took 70ms
  Get and create took 54ms
  Get and create took 59ms
  Get and create took 60ms
  Get and create took 124ms
  Get and create took 50ms
  Get and create took 455ms
  Get and create took 68ms
  Get and create took 83ms
  Get and create took 2640ms
  Get and create took 57ms
  Get and create took 56ms
  Get and create took 53ms
  Get and create took 55ms
  Get and create took 61ms
  Get and create took 50ms
  Get and create took 563ms
  Get and create took 103ms
  Get and create took 52ms
  Get and create took 2685ms
  Get and create took 54ms
  Get and create took 267ms
  Get and create took 50ms
  Get and create took 55ms
  Get and create took 57ms
  Get and create took 49ms
  Get and create took 554ms
  Get and create took 77ms
  Get and create took 64ms

Dependency management as background task: 230ms

Get and create took 868ms
Get and create took 65ms
Get and create took 62ms
Get and create took 65ms
Get and create took 197ms
Get and create took 61ms
Get and create took 73ms
Get and create took 697ms
Get and create took 2655ms
Get and create took 89ms
Get and create took 317ms
Get and create took 57ms
Get and create took 80ms
Get and create took 53ms
Get and create took 117ms
Get and create took 60ms
Get and create took 78ms
Get and create took 494ms
Get and create took 73ms
Get and create took 61ms
Get and create took 260ms
Get and create took 59ms
Get and create took 56ms
Get and create took 132ms
Get and create took 104ms
Get and create took 74ms
Get and create took 54ms
Get and create took 303ms
Get and create took 106ms
Get and create took 54ms
Get and create took 450ms
Get and create took 104ms
Get and create took 190ms
Get and create took 72ms
Get and create took 67ms
Get and create took 126ms
Get and create took 55ms
Get and create took 454ms
Get and create took 68ms
Get and create took 198ms

Checklist

Delete any rows that do not apply to the PR.

Copy link
Contributor

@katie-gardner katie-gardner left a comment

Choose a reason for hiding this comment

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

Great job with this, seeing similar results to you with generally double speed on page loads, and implementation looks sound, still 1-2s sometimes on self assessment page but better than before

@jimwashbrook jimwashbrook force-pushed the feat/add-background-processing-for-redis-jobs branch 2 times, most recently from 6aa4754 to 19c7884 Compare December 13, 2024 15:49
@jimwashbrook jimwashbrook changed the title 🚧 feat: Move Redis dependency management to background tasks feat: Move Redis dependency management to background tasks Dec 13, 2024
@jimwashbrook jimwashbrook force-pushed the feat/add-background-processing-for-redis-jobs branch 2 times, most recently from 8ae0d09 to 1dfc3a0 Compare December 13, 2024 15:53
tests and refactor

chore: Linted code for plan-technology-for-your-school.sln solution

chore: remove stopwatch

chore: reawait task

chore: add options to program extensions

chore: amend how default options work
@jimwashbrook jimwashbrook force-pushed the feat/add-background-processing-for-redis-jobs branch from 1dfc3a0 to 8531c70 Compare December 13, 2024 15:56
@jimwashbrook jimwashbrook merged commit ab1db97 into development Dec 16, 2024
11 checks passed
@jimwashbrook jimwashbrook deleted the feat/add-background-processing-for-redis-jobs branch December 16, 2024 15:04
@jimwashbrook jimwashbrook mentioned this pull request Jan 10, 2025
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.

3 participants