Webhook race condition mitigation strategies

AI-generated summary

devaoc is developing a two-way sync between Discourse and Discord, using webhooks for topic and post events. They encountered a race condition where post webhooks are received before topic webhooks, causing issues in their middleware. After discussing with Falco, they decided to switch to using only post webhooks and rely on the topic_id to link threads, making the system more reliable. Falco agreed that this change would improve reliability and suggested using API requests to enrich data if needed.

Hey everyone! :waving_hand:

I’m working on building a two-way sync between Discourse and Discord. On the Discourse side, I’m using webhooks for topic and post events (created, edited, destroyed, recovered) to relay updates to the corresponding Discord channels. The other direction is handled using a bot built with discord.js.

I’ve been running into some issues getting my middleware to handle things cleanly — mainly due to a race condition between when topic and post webhooks are received.

I’d love some advice on how to best handle this. Right now, I’m considering dropping topic webhooks entirely and basing everything around post events (using topic_id as the unifier). That said, I’d prefer to find a way to make my current setup work rather than rewriting everything from scratch.

Any insights or suggestions would be greatly appreciated! :folded_hands:

I just decided to fetch the values and fill in some data before having the topic in my database

Is this that you are getting the post webhook before the topic webhook? Given the async nature of that it may indeed happen.

So it was that I was doing both topic and post webhooks but I’ve switched to only post webhooks and using whats available in those to link threads <> topics

That’s a good change, it will make your system way more reliable, and as you said you can always reach for API requests to enrich the data.