Webhook race condition mitigation strategies

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:

3 Likes

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

1 Like

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

2 Likes

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

2 Likes

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.

2 Likes