Fail-safe remote API operations for managing customers and billing I'm working with two remote APIs: one which manages customers and the other (a Paypal API interface) which manages billing. To delete an account, I must delete the records from both.

function deleteAccount($account_id) {
    try {
        $api1-&gt;deleteAccount( $account_id );
    } catch (Exception $e) {
        Logger::log(...);
        return false;
    }

    try {
        $api2-&gt;cancelBilling( $account_id );
    } catch (Exception $e) {
        Logger::log(...);
        return false;
    }
}


Suppose that the first operation (with api1) succeeds but the second operation (with api2) fails. This could happen, for example, if the api2 server was down - a factor which is out of my control.

I would then have deleted the account in api1, but would still be billing the user, which would lead to a corruption in my data (and some unhappy users!).

I have one idea on how to maximise the possibility of success, by doing various checks for errors:

function deleteAccount($account_id) {
    //Check that servers are up
    if(!$api1-&gt;canConnectToServer() || !$api2-&gt;canConnectToServer()) {
        return false;
    }
    if (!$api1-&gt;isAccountIDValid($account_id ) || !$api2-&gt;isAccountIDValid($account_id ) ) {
        return false;
    }

    try {
        $api1-&gt;deleteAccount( $account_id );
    } catch (Exception $e) {
        Logger::log(...);
        return false;
    }

    try {
        $api2-&gt;cancelBilling( $account_id );
    } catch (Exception $e) {
        Logger::log(...);
        return false;
    }
}


However, this seems a little flimsy and doesn't guarantee data integrity. Is there a better way to handle this? I'm also not sure whether I should return false or throw an exception.
Do you have access to the customer API? If so, I think the deletion of the Paypal account should be handled by it.

If you do not have access to it, it seems the most important call is the Paypal one, so you should first make sure that one succeeds. Once it does, you can delete the customer account. But no matter how you delete it, there will be some time, even if only a few seconds, when the customer account will be without a Paypal account. The customer API needs to handle this situation gracefully.

To make sure the customer account gets eventually deleted, I would add this account to some table and have a cron job process it every X seconds. The cron job should delete all the account in that table. It means that if deletion fails, it will try again a few seconds later, so it's safe to assume the account will eventually get deleted.