wger icon indicating copy to clipboard operation
wger copied to clipboard

Performance of `api/v2/exercisebaseinfo/?limit=900`

Open bbkz opened this issue 1 year ago • 7 comments

Use case

In my setup, when i open /exercise/overview/ it takes 2-3 minutes to load the content of api/v2/exercisebaseinfo/?limit=900. I run it on raspberry's and similar, so it may just be a hardware limitation.

Workaround

So i came up with the following workaround. I set EXERCISE_CACHE_TTL to 25 hours and run a cronjob every 24 hours to warm up the cache:

python3 manage.py warmup-exercise-api-cache --force

This way it will reset the TTL of the exercise cache every 24 hours. This work fine and it is blazing fast now ;-) I think updating a exercise will warmup it's cache induvidually so, i don't have to wait till the next day for the updates (but i am not sure).

Proposal

It would be nice if the warmup-exercise-api-cache could be refactured similar to sync_exercises, so that celery task can be created. This would eliminate the fact that a cronjob is needed to warmup the cache.

bbkz avatar Jun 11 '24 15:06 bbkz

mhh, yes, that does sound like a good idea

rolandgeider avatar Jun 12 '24 13:06 rolandgeider

Can I be assigned to the task?

wbonicki avatar May 22 '25 09:05 wbonicki

Sure @wbonicki !

We already have some periodic tasks, e.g. in wger.exercises.tasks.setup_periodic_tasks, we'd do something like that. Also, you will need to start a celery task as well btw: https://wger.readthedocs.io/en/latest/development/celery.html

rolandgeider avatar May 22 '25 11:05 rolandgeider

Here is PR: https://github.com/wger-project/wger/pull/1977

I'm unsure about one thing: I created a new task for caching API exercises, and if it's activated, it will be executed once a day (at random time). Is that correct? Since the default value of EXERCISE_CACHE_TTL is 3600 seconds (1 hour), would it make more sense to run the task every hour instead? And another thing: I'm not sure if CACHE_API_EXERCISES_CELERY_FORCE_UPDATE variable is actually necessary.

wbonicki avatar May 25 '25 12:05 wbonicki

Hi wbonicki,

thank you for looking into it! Yes it makes no sense to run the cache warmup once a day when it exipres a hour later. But as the cache warmup of all exercices can take quite some time, i think it is overkill to do that every hour.

I the sysadmin can configure the celery task, he can set whatever makes sense to him.

I don't have the complete overview of the caching made, but is it possible that when a edit of a exercice has been made, that it warms up the cache for this exercise?

bbkz avatar May 30 '25 07:05 bbkz

@bbkz do you mean that during the caching process, the exercise cache is overwritten only if it has been edited?

wbonicki avatar May 30 '25 11:05 wbonicki

No it think i once saw in the code, when saving a exercices a method to warm up that cache was called. I was only peeking around so i am really not sure if that is the case.

The idea of my comment was: That i think maybe the whole concept on what and how long something should be cached, should be made clear.

bbkz avatar May 30 '25 15:05 bbkz

at the moment when an exercise is edited, its cache is deleted. That means that when calling the overview the next time, that one needs to be recreated, which should be fine.

Also, in the compose file, we set EXERCISE_CACHE_TTL to 5 hours, so once a day should be fine (this value was probably from before we did reset the cache on edit, so this could be increased, but I'd need to check that).

I would also say we don't need CACHE_API_EXERCISES_CELERY_FORCE_UPDATE, if the instance admin wants to force this, there's the manual warmup-exercise-api-cache.py command

rolandgeider avatar Jun 27 '25 13:06 rolandgeider