Kyon147 / laravel-shopify

A full-featured Laravel package for aiding in Shopify App development
MIT License
336 stars 99 forks source link

Shop installation fails with reader/writer DB #227

Closed gianlup closed 8 months ago

gianlup commented 8 months ago

Expected Behavior

Shop installs correctly

Current Behavior

Shop installs correctly only with a second attempt. At the first attempt it returns 500

Failure Information

I have a DB cluster configured with a reader and a writer. At the first install attempt a new user is created in the DB and instantly re-read, but in this way the writer doesn't have time to replicate itself to reader and the query fails. At the second attempt the new user is already available on the reader so everything works fine.

The action is performed here: https://github.com/Kyon147/laravel-shopify/blob/62849824554f9c9eef3137f60c3880568b7f2117/src/Actions/InstallShop.php#L75

Possible solution: use the function onWriteConnection of Laravel Model class. In this way the DB is forced to read from the writer instance.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. Step 1
  2. Step 2
  3. ...

Failure Logs

Please include any relevant log snippets or files here.

Kyon147 commented 8 months ago

I use a read/writer set up on GCP with a cluster and have not run into this issue before but defo makes sense why it could be happening.

It sounds like a race condition with your two databases but you should still be allowed to read from the DB that writes too, that way you should not run into the replication issue.

Do you have some load balancing, that might be forcing you onto the read only DB as a user but the application runtime is sent to the master i.e. writer?

Feel free to create a PR though, as it is worth covering this on the modal.