Commit lag on Postgre Auora RDS instance

We are getting occasional issues with our Postgre Auora RDS instance with what seems to be a slight commit lag. For instance if we insert an item and try and read it from the database immediately sometimes no results are found. It doesn’t happen often and I have ensured the code would create this, after I get the error log if I check then the item is in the database. I can amend the code to ‘wait’ for the item but I would rather understand what is going on. It is a single DB that is the same for read and writes, with no replication. Can anyone give me some pointers please.

If you’re reading from a reader node of a cluster then this is to be expected. It’s not instant replication but normally within the ms range. You say it’s not the case for you but just for info.

If it’s a single instance then are you using 2 sessions and the reader is in the middle of a transaction where it’s being given a consistent view of the data as it was when the transaction started? i.e. this session can’t see any newer data until it commits or rolls back?

If it’s not that and you’re still using two sessions - are you sure the commit has returned (as in, signaled complete) in the writer session before you’re trying to read in the reader session?

If you’re writing and then reading from the same instance and the same session this should not happen.

Hi Phil, I am using doctrine to make the calls but I have not configured anything to make me believe it is using multiple sessions. If it were, then I would expect the reader to wait as the row is locked from the write. I am 100% sure that the commit succeeds before we try and read the value. This very rarely happens but is becoming an annoyance to the stage that my colleague wants to build in some waits but I feel this would mask the problem to the point we would never be able to properly fix it.

FWIW, we’ve been using PG Aurora for multiple, multiple-terabyte relatively high volume OLTP databases, averaging ~1500 commits per second each with peaks much higher than that. A common use case is adding or updating data in our app and seeing the results immediately. We haven’t encountered this when using a single read/write host. In a prior incarnation in physical land, we did see it when we tried to use a write master and read replicas so I’m at least familiar with the issue.

I definitely lean to the things that /u/phil-99 calls out.

Wondering if in your workflow you write and read it back straight away, because if you do it certainly screams I am doing something wrong. If not, is it possible that there is a few ms lag?

If it were, then I would expect the reader to wait as the row is locked from the write

that’d only be true for a select for update/share
https://www.postgresql.org/docs/current/transaction-iso.html

Just for your info, postgresql transactions allow non-repeatable reads. The data other transactions commit during your transaction are reflected in your repeated reads within the same transaction. https://amirsoleimani.medium.com/understanding-database-isolation-level-via-examples-mysql-and-postgres-a86b5502d404

Not that it affects OP, but it’s an important difference between pSQL and mySQL