<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: san</title>
    <description>The latest articles on DEV Community by san (@sanme).</description>
    <link>https://dev.to/sanme</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4268594%2F9d1bfcf2-26af-46bf-b4fe-f1d9e7ddde1d.png</url>
      <title>DEV Community: san</title>
      <link>https://dev.to/sanme</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sanme"/>
    <language>en</language>
    <item>
      <title>Read Your Own Writes on Async Replicas: PostgreSQL 19's WAIT FOR LSN</title>
      <dc:creator>san</dc:creator>
      <pubDate>Fri, 27 Nov 2026 10:11:12 +0000</pubDate>
      <link>https://dev.to/sanme/read-your-own-writes-on-async-replicas-postgresql-19s-wait-for-lsn-4gjp</link>
      <guid>https://dev.to/sanme/read-your-own-writes-on-async-replicas-postgresql-19s-wait-for-lsn-4gjp</guid>
      <description>&lt;p&gt;PostgreSQL 19's &lt;code&gt;WAIT FOR LSN&lt;/code&gt; lets a read on an asynchronous replica wait until that replica has replayed your write. You get read-your-writes consistency without sending every read to the primary.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem: the write succeeded, the read missed it
&lt;/h2&gt;

&lt;p&gt;Read-write splitting is a common way to scale: writes go to the primary, reads spread across asynchronous replicas. It breaks a simple expectation:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A user renames their profile from Alex to Sam.&lt;/li&gt;
&lt;li&gt;The primary commits. Success.&lt;/li&gt;
&lt;li&gt;The user refreshes, and the read lands on a replica.&lt;/li&gt;
&lt;li&gt;The replica hasn't replayed the change yet, so it returns Alex.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Nothing failed. Replication lag let the user miss their own write. The guarantee that this never happens is called &lt;strong&gt;read-your-writes (RYW) consistency&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why not just read from the primary?
&lt;/h2&gt;

&lt;p&gt;You can, and it's a valid fix. Route reads that must see the latest write to the primary. The cost is load: the more traffic does this, the more you lose the reason you added replicas.&lt;/p&gt;

&lt;p&gt;A common variation is &lt;strong&gt;sticky primary routing&lt;/strong&gt;: after a write, read from the primary for a fixed window, say 5 seconds. But replication lag is unpredictable:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The replica catches up in 20 ms. The primary served almost 5 seconds of reads it didn't need to.&lt;/li&gt;
&lt;li&gt;The replica lags 8 seconds. The window closes at 5 seconds, and reads hit a stale replica anyway.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;We're using &lt;strong&gt;time&lt;/strong&gt; to guess replication &lt;strong&gt;progress&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What if we waited for actual progress instead?&lt;/p&gt;

&lt;h2&gt;
  
  
  PostgreSQL 19: WAIT FOR LSN
&lt;/h2&gt;

&lt;p&gt;PostgreSQL replicates by streaming its Write-Ahead Log (WAL). A &lt;strong&gt;Log Sequence Number (LSN)&lt;/strong&gt; is a position in that stream. If you know the LSN of your commit, you know exactly how far a replica must get before it can show your write.&lt;/p&gt;

&lt;p&gt;The pattern takes five steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Commit the write on the primary.&lt;/li&gt;
&lt;li&gt;Capture the WAL position right after the commit.&lt;/li&gt;
&lt;li&gt;Send that LSN along with the next read, to a replica.&lt;/li&gt;
&lt;li&gt;On the replica, wait until it has replayed up to that LSN.&lt;/li&gt;
&lt;li&gt;Run the read on the same replica. Its fresh snapshot includes your write.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;On the primary, after committing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nf"&gt;pg_current_wal_insert_lsn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- 0/0306EE20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the replica, before the read:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="no"&gt;WAIT&lt;/span&gt; &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="no"&gt;LSN&lt;/span&gt; &lt;span class="s1"&gt;'0/0306EE20'&lt;/span&gt;
  &lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;TIMEOUT&lt;/span&gt; &lt;span class="s1"&gt;'200ms'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;NO_THROW&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the replica has already caught up, the command returns immediately. Otherwise it waits until the LSN is replayed or the timeout expires. With &lt;code&gt;NO_THROW&lt;/code&gt;, a timeout returns a status (&lt;code&gt;success&lt;/code&gt; or &lt;code&gt;timeout&lt;/code&gt;) instead of raising an error, so check the status before running the read.&lt;/p&gt;

&lt;p&gt;Run the wait before your transaction takes a snapshot: outside a transaction block, or as its first statement. Earlier versions could do this with application-side polling. PostgreSQL 19 makes it a native command.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't make every write wait. Make only the reads that need consistency wait.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Is this a new idea?
&lt;/h2&gt;

&lt;p&gt;Not really. Other databases have tackled the same problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MySQL&lt;/strong&gt; can wait on transactions instead of log positions, using Global Transaction Identifiers (GTIDs):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="no"&gt;WAIT_FOR_EXECUTED_GTID_SET&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'required-gtid-set'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The read waits until the replica has executed those transactions, with a 0.2-second timeout.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SQL Server&lt;/strong&gt; (Always On Availability Groups) has LSNs too, and its &lt;code&gt;sys.dm_hadr_database_replica_states&lt;/code&gt; view reports &lt;code&gt;last_hardened_lsn&lt;/code&gt; and &lt;code&gt;last_redone_lsn&lt;/code&gt; per replica. Synchronous commit guarantees the log is &lt;em&gt;hardened&lt;/em&gt; on the secondary. Queries only see a change after &lt;em&gt;redo&lt;/em&gt; replays it into the data pages, and redo can lag. There's no directly equivalent built-in wait command, so you would poll that view yourself.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Replication durability and read consistency are different guarantees.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Data can be safe on a replica before a query there can see it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where it fits in production
&lt;/h2&gt;

&lt;p&gt;Every approach pays somewhere:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Approach&lt;/th&gt;
&lt;th&gt;Trade-off&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Primary routing&lt;/td&gt;
&lt;td&gt;More load on the primary&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sticky primary routing&lt;/td&gt;
&lt;td&gt;Assumes replication progress&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Synchronous replay&lt;/td&gt;
&lt;td&gt;Higher write latency&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Replication-aware reads (&lt;code&gt;WAIT FOR LSN&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Read latency and token management&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Replica waits don't replace primary routing. Waiting requests hold database connections, raise tail latency, and need careful timeout and failover handling.&lt;/p&gt;

&lt;p&gt;A practical setup combines all three:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Regular reads&lt;/strong&gt; go straight to replicas, with no waiting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reads right after a write&lt;/strong&gt; run &lt;code&gt;WAIT FOR LSN&lt;/code&gt; on a replica first.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If the replica can't catch up in time&lt;/strong&gt; (&lt;code&gt;timeout&lt;/code&gt;), fall back to the primary.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;WAIT FOR LSN&lt;/code&gt; doesn't remove replication lag or add a new consistency model. It lets you keep asynchronous replication and make only the reads that need consistency wait, instead of making every write wait.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Read-your-writes is a consistency requirement, not a routing strategy.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sometimes reading from the primary is the right answer. Sometimes waiting on a replica is the better trade-off. Let the architecture decide.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.postgresql.org/docs/19/sql-wait.html" rel="noopener noreferrer"&gt;PostgreSQL 19: WAIT FOR LSN&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.mysql.com/doc/refman/8.4/en/gtid-functions.html" rel="noopener noreferrer"&gt;MySQL: GTID functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/sql/database-engine/availability-groups/windows/active-secondaries-readable-secondary-replicas-always-on-availability-groups" rel="noopener noreferrer"&gt;SQL Server: readable secondary replicas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/sql/relational-databases/system-dynamic-management-views/sys-dm-hadr-database-replica-states-transact-sql" rel="noopener noreferrer"&gt;SQL Server: sys.dm_hadr_database_replica_states&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>sql</category>
      <category>systemdesign</category>
    </item>
  </channel>
</rss>
