{"slug": "a-refund-changed-the-customers-eligibility-how-does-the-api-know", "title": "A Refund Changed the Customer’s Eligibility. How Does the API Know?", "summary": "A developer outlined an architecture for keeping an AI agent's eligibility data current when source records change, using PostgreSQL change data capture, incremental SQL in Materialize, and a continuously maintained read model behind a Rewards API. In the example, a refund drops a Gold member's completed purchases from three to two, flipping eligibility from true to false without recomputing the full result per request. The writeup stresses measuring end-to-end freshness, monitoring replication lag and WAL retention, and having the authoritative rewards service revalidate eligibility transactionally before committing financial value.", "body_md": "A customer has Gold membership and three completed purchases. That's enough to qualify for a rewards offer.\n\nThen one of those purchases is refunded.\n\nThe business decision has changed. But what happens to the data that powers the Rewards API—and the AI agent relying on that API?\n\nThis is a useful example of a broader data architecture challenge: **How do we keep operational read models current as source data changes?**\n\nConsider a simple flow:\n\n**PostgreSQL → Change Data Capture → Incremental SQL → Maintained Eligibility View → Rewards API → AI Agent**\n\nHere's what happens:\n\n**1. The source changes.** A refund is recorded in PostgreSQL, changing the business state that determines customer eligibility.\n\n**2. CDC captures the change.** Rather than repeatedly reading all purchase and membership records, the system consumes changes from PostgreSQL's logical replication stream.\n\n**3. Incremental SQL maintains the result.** An engine such as Materialize updates the affected eligibility calculation as source changes are processed.\n\nFor example, consider an illustrative schema with `orders`, `memberships`, and `refunds`. Assume each customer has one membership record and each refunded order has one refund record.\n\n```\nCREATE MATERIALIZED VIEW customer_eligibility AS\nSELECT\n    m.customer_id,\n    m.status AS membership_status,\n    COUNT(o.order_id) FILTER (\n        WHERE o.status = 'COMPLETED'\n          AND r.order_id IS NULL\n    ) AS completed_purchases,\n    (\n        m.status = 'GOLD'\n        AND COUNT(o.order_id) FILTER (\n            WHERE o.status = 'COMPLETED'\n              AND r.order_id IS NULL\n        ) >= 3\n    ) AS eligible\nFROM memberships m\nLEFT JOIN orders o\n    ON o.customer_id = m.customer_id\nLEFT JOIN refunds r\n    ON r.order_id = o.order_id\nGROUP BY m.customer_id, m.status;\n```\n\nWhen a refund record arrives, that order no longer contributes to the completed-purchase count. The count falls from three to two, changing eligibility from `true` to `false`.\n\nThe important idea is that **the query declares the desired business result**, while the incremental computation engine maintains that result as inputs change.\n\n**4. The API reads the maintained view.** The Rewards API queries the eligibility view served by Materialize, rather than recalculating the entire result from the operational tables on each request.\n\n**5. The AI agent uses the API.** When the agent requests eligibility through an authorized API tool, it receives the latest state currently available through that API.\n\nThis is an illustrative design—not a claim that every source change becomes instantly visible. The end-to-end delay must still be measured.\n\nThree questions deserve attention.\n\n**1. Can we trust the change stream?**\n\nCDC must reliably capture updates and deletes and recover from interruptions. PostgreSQL replication lag, WAL retention, and recovery behavior need operational monitoring.\n\n**2. How fresh is the result?**\n\nDefine 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 `false`.\n\n**3. Who owns the final business decision?**\n\nA continuously maintained eligibility view supports fast reads. But if redeeming a reward commits financial value, the authoritative rewards service should still validate eligibility transactionally.\n\nThese questions matter more than simply getting the first SQL query to work.\n\nThe bigger architectural lesson goes beyond PostgreSQL or Materialize:\n\n**Change Data Capture → Incremental Computation → Continuously Maintained Read Model**\n\nThis pattern can support many changing business contexts:\n\nBatch 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.\n\n**The real goal is not to adopt another technology. It's to make trustworthy business information available where decisions are made.**\n\nFor a more detailed walkthrough, read my [Clinic #01 Deep Dive on Beyond the Blueprint](https://rinkibasu.substack.com/p/clinic-01-deep-dive-how-it-works).\n\n*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.*", "url": "https://wpnews.pro/news/a-refund-changed-the-customers-eligibility-how-does-the-api-know", "canonical_source": "https://dev.to/rinkibasu/a-refund-changed-the-customers-eligibility-how-does-the-api-know-146p", "published_at": "2026-11-20 18:11:23+00:00", "updated_at": "2026-11-20 18:14:02.655859+00:00", "lang": "en", "topics": ["ai-agents", "ai-infrastructure", "mlops"], "entities": ["PostgreSQL", "Materialize"], "also_reported_by": [], "takeaway": "Maintain eligibility as an incrementally updated materialized view fed by CDC rather than recomputing per request, but measure source-to-API freshness and revalidate transactionally before committing financial value.", "recommendation": {"advice": "Feed agent-facing APIs from incrementally maintained read models via CDC, and measure source-to-API lag so stale or unavailable data is never silently returned as false.", "playbook": "agent-architecture", "playbook_url": "https://wpnews.pro/playbooks/agent-architecture"}, "alternates": {"html": "https://wpnews.pro/news/a-refund-changed-the-customers-eligibility-how-does-the-api-know", "markdown": "https://wpnews.pro/news/a-refund-changed-the-customers-eligibility-how-does-the-api-know.md", "text": "https://wpnews.pro/news/a-refund-changed-the-customers-eligibility-how-does-the-api-know.txt", "jsonld": "https://wpnews.pro/news/a-refund-changed-the-customers-eligibility-how-does-the-api-know.jsonld"}}