<?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: Rinki Basu</title>
    <description>The latest articles on DEV Community by Rinki Basu (@rinkibasu).</description>
    <link>https://dev.to/rinkibasu</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%2F4256175%2F94a625a9-6c4f-4cf2-8d4d-91699fce424f.png</url>
      <title>DEV Community: Rinki Basu</title>
      <link>https://dev.to/rinkibasu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rinkibasu"/>
    <language>en</language>
    <item>
      <title>A Refund Changed the Customer’s Eligibility. How Does the API Know?</title>
      <dc:creator>Rinki Basu</dc:creator>
      <pubDate>Fri, 20 Nov 2026 18:11:23 +0000</pubDate>
      <link>https://dev.to/rinkibasu/a-refund-changed-the-customers-eligibility-how-does-the-api-know-146p</link>
      <guid>https://dev.to/rinkibasu/a-refund-changed-the-customers-eligibility-how-does-the-api-know-146p</guid>
      <description>&lt;p&gt;A customer has Gold membership and three completed purchases. That's enough to qualify for a rewards offer.&lt;/p&gt;

&lt;p&gt;Then one of those purchases is refunded.&lt;/p&gt;

&lt;p&gt;The business decision has changed. But what happens to the data that powers the Rewards API—and the AI agent relying on that API?&lt;/p&gt;

&lt;p&gt;This is a useful example of a broader data architecture challenge: &lt;strong&gt;How do we keep operational read models current as source data changes?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The architectural pattern
&lt;/h2&gt;

&lt;p&gt;Consider a simple flow:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PostgreSQL → Change Data Capture → Incremental SQL → Maintained Eligibility View → Rewards API → AI Agent&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's what happens:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. The source changes.&lt;/strong&gt; A refund is recorded in PostgreSQL, changing the business state that determines customer eligibility.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. CDC captures the change.&lt;/strong&gt; Rather than repeatedly reading all purchase and membership records, the system consumes changes from PostgreSQL's logical replication stream.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Incremental SQL maintains the result.&lt;/strong&gt; An engine such as Materialize updates the affected eligibility calculation as source changes are processed.&lt;/p&gt;

&lt;p&gt;For example, consider an illustrative schema with &lt;code&gt;orders&lt;/code&gt;, &lt;code&gt;memberships&lt;/code&gt;, and &lt;code&gt;refunds&lt;/code&gt;. Assume each customer has one membership record and each refunded order has one refund record.&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;CREATE&lt;/span&gt; &lt;span class="k"&gt;MATERIALIZED&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;customer_eligibility&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;membership_status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'COMPLETED'&lt;/span&gt;
          &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;completed_purchases&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'GOLD'&lt;/span&gt;
        &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'COMPLETED'&lt;/span&gt;
              &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;eligible&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;memberships&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;refunds&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a refund record arrives, that order no longer contributes to the completed-purchase count. The count falls from three to two, changing eligibility from &lt;code&gt;true&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The important idea is that &lt;strong&gt;the query declares the desired business result&lt;/strong&gt;, while the incremental computation engine maintains that result as inputs change.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. The API reads the maintained view.&lt;/strong&gt; The Rewards API queries the eligibility view served by Materialize, rather than recalculating the entire result from the operational tables on each request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. The AI agent uses the API.&lt;/strong&gt; When the agent requests eligibility through an authorized API tool, it receives the latest state currently available through that API.&lt;/p&gt;

&lt;p&gt;This is an illustrative design—not a claim that every source change becomes instantly visible. The end-to-end delay must still be measured.&lt;/p&gt;

&lt;h2&gt;
  
  
  What matters in production?
&lt;/h2&gt;

&lt;p&gt;Three questions deserve attention.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Can we trust the change stream?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;CDC must reliably capture updates and deletes and recover from interruptions. PostgreSQL replication lag, WAL retention, and recovery behavior need operational monitoring.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. How fresh is the result?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Define a freshness target and measure the delay between a source change and the API-visible result. If the system cannot establish a trustworthy answer, it should not silently turn “unavailable” into &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Who owns the final business decision?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A continuously maintained eligibility view supports fast reads. But if redeeming a reward commits financial value, the authoritative rewards service should still validate eligibility transactionally.&lt;/p&gt;

&lt;p&gt;These questions matter more than simply getting the first SQL query to work.&lt;/p&gt;

&lt;h2&gt;
  
  
  The transferable pattern
&lt;/h2&gt;

&lt;p&gt;The bigger architectural lesson goes beyond PostgreSQL or Materialize:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Change Data Capture → Incremental Computation → Continuously Maintained Read Model&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This pattern can support many changing business contexts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pricing and promotions&lt;/li&gt;
&lt;li&gt;Inventory availability&lt;/li&gt;
&lt;li&gt;Customer status and entitlements&lt;/li&gt;
&lt;li&gt;Operational information exposed to AI agents&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Batch processing, custom streaming, and continuously maintained read models all have legitimate uses. The right choice depends on freshness requirements, business complexity, cost, recovery behavior, and decision ownership.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The real goal is not to adopt another technology. It's to make trustworthy business information available where decisions are made.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For a more detailed walkthrough, read my &lt;a href="https://rinkibasu.substack.com/p/clinic-01-deep-dive-how-it-works" rel="noopener noreferrer"&gt;Clinic #01 Deep Dive on Beyond the Blueprint&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This is an educational architecture example, not a production benchmark. The SQL uses an illustrative schema and has not been presented as a tested deployment script.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>dataengineering</category>
      <category>postgres</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
