We're designing the architecture of an embedded device (esp32). One of the tasks is that the device should connect to the internet and use a preprovisioned redeem code to register itself with our web cloud. Within the same message the device will upload a random generated password to the server which is then stored in an SQL database. This password will be used on every further startup of the device to login to the cloud. So it should also be saved into nonvolatile storage. Here's the set of operations in pseudocode:
if (password not found in internal memory)
{
pw = create_new_password() (1)
send_passsword_with_redeem_code_to_server(pw) (2)
store_password_to_flash(pw) (3)
}
The problem with this is that operations (2) and (3) need to be carried out in an all-or-nothing fashion BUT both commands can also fail. If the storage to flash memory fails AFTER the database has registered the device password, the device would not be allowed to register a new password again and it would be floating. At the same time, if I were to store the password first to flash and then do the web request, the web request could fail OR the device could be powered of and the password would never reach the webserver, causing the device to "think" that it has registered on the next boot (as the password is found in flash) but it actually isn't and it would be floating again.
Are there techniques to group these two commands together as a set of atomic transactions which are either accomplished or discarded as a whole?