samedi 25 avril 2020

Rails application reads a stale record

Consider the following situation. I have two machines (AWS EC2s), A and B. A and B have access to a common database. I also have a single DB table MyTable which has a single record with fields field1, field2, field3.

On A I have a pure Ruby script which once a minute updates the fields (using pg gem). On B I have a Rails 5 application which reads field1, field2, field3.

Now, the wired thing is that even after the Ruby script updates the field values, the Rails application still reads the old (stale?) values of the fields. I read the record as following

def myRecord
  rec ||= MyTable.first
end

If I restart the Rails application it reads the correct (latest) value. I guess this is because the Rail application reads from cache. Please correct me if I.m wrong. So, how could I force the Rails application to read the most recent value from the database?

Aucun commentaire:

Enregistrer un commentaire