How to Handle API Notifications and State Management without Adding Complexity?

0
8
Asked By Techie42 On

I'm working on a webshop app where I manage an order class with states for Received and Finished. The business rules state that I should only update the order to Finished in the database if the notification to an external API is successful. Here's the catch: what if the API notification succeeds but the database update fails? In that case, on a retry from a background job, I'd end up re-notifying the API, which is not ideal. I was thinking about introducing a new state called Notifying to handle this, but I'm wondering if there's a simpler way to achieve this without adding complexity. Any suggestions?

4 Answers

Answered By DevMaster101 On

You really don't need to complicate it by adding a substate. I think you can simply use a job queue that retries to update the database state until it succeeds. If the API notification was already successful, there's no reason to re-notify the service again.

Answered By CleverDev On

I totally agree! Using a simple boolean flag can act like a substate without the need for an entire state machine overhaul. It just keeps everything cleaner.

Answered By SimpleCoder On

Honestly, instead of overcomplicating your state machine, just use a boolean flag like notification_sent. It's a straightforward solution that keeps things manageable without the overhead of a substate.

Answered By CodeWhisperer On

What if the API call succeeds but you have a temporary issue updating the database? You'd still need to have a way to record that the notification was requested, and maybe have a background job that checks those records and verifies whether the API call went through.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.