Ethereum noob over here. I'm working with ethereum locally using HardHat. I've created a contract for NFTs and I have a database that stays up to date with the NFTs that have been created by listening for "created" events.
I have a web client that provides an interface for viewing and creating NFTs and listens for transactions to complete (for the NFT to be mined), at which point it calls the db for the latest state. However, the issue I'm having is that often times the client captures the "transaction complete" event from the contract and queries the db before the db has had a chance to capture the similar event - e.g. the db has stale data.
These are options I've been thinking about in order to solve this but none of them seem great.
1 - I could check if the etag on the request has changed and long poll to wait for the "create" event to be triggered if it has. However, this still feels error prone in that if multiple transactions are being written to the contract I could accidentally capture the wrong one.
2 - I could do a "hard refresh" from the blockchain in which the db fetches the most recent data from the contract. However, is this even possible?
3 - Upon capturing that the transaction completed on the client, I could add a slight delay before querying the db, thus increasing the likelihood that the recent transaction has been captured in the db. However, of these 3 solutions this is my least favorite in that it feels quite brittle.
4 - Another option that is proposed in the comments is using Web Sockets on the server to notify the client of the change (instead of the client listening for changes directly from the blockchain). I'm hoping to avoid this, however, since it would put additional demand on my server (to keep the sockets open) and my server is not very powerful.
How is this problem usually solved?