Do I have an use case for moving to async here?

I have a working app that I would like to start migrating to async, but I’m having a hard time finding tutorials on the matter.

For example, look at this use case. In it, I have a rating models that, for each new rating, trigger a recalculation of the UserRating model

@receiver(post_save, sender=Rating)
def update_or_create_skillsummmary(sender, instance, **kwargs):
    """
    Updates or creates a UserRating when a Rating is saved
    """
    if instance.level == sender.Rating.UNKNOWN:
        return

    skill_summary, _ = UserRating.objects.get_or_create(
        user=instance.to_user, skill=instance.skill
    )

    if instance.is_positive:
        skill_summary.update_upvotes()
    elif instance.is_negative:
        skill_summary.update_downvotes()

However, signals are synchronous by default I don’t need to wait for this recalculation before returning a response. I was wondering if I could move that transaction to an asynchronous queue somehow.

Unless there’s something really knarly in update_upvotes or update_downvotes, this is going to be such a fast process that it’s not worth trying to process it independently.
In fact, I’d even get rid of the signal here, and add the call to this to the Rating model’s save method.

Thanks for the support, Ken!

Right now update_upvotes / update_downvotes are simple aggregations, but I’m now including a new process for normalizing the user ratings which may take a while.

I expect the number of calculations on the UserRating model to increase.

Again, you want to also weigh that in the context of how frequently this is going to occur. I don’t know what your application is, how many users are actively using it, and how often this Rating model is going to be updated. That’s contextual information that only you can determine.

I always approach this type of situation from the perspective of “keep it simple until it has been proven necessary to do otherwise”. You can waste a lot of time rearchitecting an application for performance reasons to prevent what you think a problem is going to be - only to find out that you cleared the wrong bottleneck and that the real issue is somewhere else.

Even if your most complicated updates take a full second - or even two - by themselves, that’s not too bad if any individual person is only going to encounter it once or twice / day.

So yes, unless you can prove to yourself that you really have a problem - and that addressing this will solve it - I wouldn’t worry about it yet.

2 Likes

That makes sense.
Thanks for the input!