<?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: Dmitry</title>
    <description>The latest articles on DEV Community by Dmitry (@mrdotflat).</description>
    <link>https://dev.to/mrdotflat</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3924537%2Fe1a18f35-185d-4717-91c4-8326df8496c4.jpg</url>
      <title>DEV Community: Dmitry</title>
      <link>https://dev.to/mrdotflat</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mrdotflat"/>
    <language>en</language>
    <item>
      <title>Stop using TTL for Ethereum contract cache — use event-driven invalidation instead</title>
      <dc:creator>Dmitry</dc:creator>
      <pubDate>Mon, 11 May 2026 08:55:37 +0000</pubDate>
      <link>https://dev.to/mrdotflat/stop-using-ttl-for-ethereum-contract-cache-use-event-driven-invalidation-instead-3pk5</link>
      <guid>https://dev.to/mrdotflat/stop-using-ttl-for-ethereum-contract-cache-use-event-driven-invalidation-instead-3pk5</guid>
      <description>&lt;p&gt;If you're building a dApp, you've probably written something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`cdp:position:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;position&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;cdpContract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getPosition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setEx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`cdp:position:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;position&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 60s TTL&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;position&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works, but there's a hidden problem: your data is stale for up to 60 seconds by design. And you're making RPC calls every 60 seconds regardless of whether anything actually changed on-chain.&lt;/p&gt;

&lt;p&gt;For most protocol contracts — a CDP vault, a staking contract, a price oracle, a liquidity pool — transactions are relatively rare. Most users are just reading. The contract state sits unchanged for minutes, hours, sometimes days.&lt;/p&gt;

&lt;h2&gt;
  
  
  The insight
&lt;/h2&gt;

&lt;p&gt;Contract state only changes when a transaction is mined. A user's CDP position doesn't change on its own between blocks — it changes because that user sent a transaction.&lt;/p&gt;

&lt;p&gt;So instead of a TTL, you can do this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cache forever (TTL = 0)&lt;/li&gt;
&lt;li&gt;Watch the blockchain for transactions to your contracts&lt;/li&gt;
&lt;li&gt;Delete the affected Redis keys the moment a tx is detected
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tx mined → watcher detects it → redis.del("cdp:position:*") → next request hits RPC
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scenario&lt;/th&gt;
&lt;th&gt;60s TTL&lt;/th&gt;
&lt;th&gt;Event-driven (TTL=0)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;No activity, 1 hour&lt;/td&gt;
&lt;td&gt;60 RPC calls&lt;/td&gt;
&lt;td&gt;0 RPC calls&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1 tx per minute&lt;/td&gt;
&lt;td&gt;60 RPC calls&lt;/td&gt;
&lt;td&gt;1 RPC call&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10 tx per minute&lt;/td&gt;
&lt;td&gt;60 RPC calls&lt;/td&gt;
&lt;td&gt;10 RPC calls&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The less active your contract, the bigger the win. For a lending protocol or an oracle, this can mean &lt;strong&gt;zero RPC calls&lt;/strong&gt; during quiet periods while data stays perfectly fresh.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This pattern is designed for your own protocol contracts with moderate activity — not for watching global tokens like USDC or WETH, which receive thousands of transactions per block and would invalidate your cache constantly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Introducing Blockpulse
&lt;/h2&gt;

&lt;p&gt;I extracted this pattern from a production DeFi app into a standalone service: &lt;strong&gt;Blockpulse&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You configure which contracts to watch and which Redis patterns to delete:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// config/config.js&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;contracts&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="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0xYourCDPContract&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cdp&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;events&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PositionOpened&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PositionClosed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Liquidated&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="na"&gt;cacheKeys&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;myapp:cdp:position:*&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;myapp:cdp:stats:*&lt;/span&gt;&lt;span class="dl"&gt;"&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="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0xYourOracle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;oracle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;events&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PriceUpdated&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="na"&gt;cacheKeys&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;myapp:price:*&lt;/span&gt;&lt;span class="dl"&gt;"&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="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. Start it alongside your app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every time a transaction touches your CDP contract, Blockpulse deletes &lt;code&gt;myapp:cdp:position:*&lt;/code&gt; from Redis. Your backend gets fresh data on the next request — and not a moment sooner.&lt;/p&gt;

&lt;h2&gt;
  
  
  What else it does
&lt;/h2&gt;

&lt;p&gt;Beyond cache invalidation, Blockpulse also:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Indexes events&lt;/strong&gt; — decoded logs stored in Redis, queryable via &lt;code&gt;/api/events/:address&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;REST API for contract calls&lt;/strong&gt; — &lt;code&gt;/api/call/:contract/:method&lt;/code&gt; with Redis caching&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Batch calls&lt;/strong&gt; — &lt;code&gt;/api/batch&lt;/code&gt; for multiple contract reads in one round-trip&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Historical sync&lt;/strong&gt; — backfills past events via Etherscan API on startup&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache dependencies&lt;/strong&gt; — invalidate contract B's keys when contract A changes&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multi-chain&lt;/strong&gt; — set &lt;code&gt;CHAIN_ID&lt;/code&gt; for Polygon, Arbitrum, Base, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Architecture
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  Ethereum Node (WebSocket / HTTP)
        │
        ▼
  ┌─────────────┐     tx detected
  │  Blockpulse │ ──────────────────► Redis DEL (your key patterns)
  └──────┬──────┘
         │
         ▼ REST API :3002
  /api/call · /api/events · /api/batch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/nagor2/blockpulse
&lt;span class="nb"&gt;cd &lt;/span&gt;blockpulse
&lt;span class="nb"&gt;cp&lt;/span&gt; .env.example .env               &lt;span class="c"&gt;# add your RPC URL and REDIS_URL&lt;/span&gt;
&lt;span class="nb"&gt;cp &lt;/span&gt;config/config.example.js config/config.js   &lt;span class="c"&gt;# add your contracts&lt;/span&gt;
npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The service has been running in production on Ethereum mainnet as part of a DeFi frontend. It's MIT licensed and ~1700 lines of Node.js.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GitHub: &lt;a href="https://github.com/nagor2/blockpulse" rel="noopener noreferrer"&gt;github.com/nagor2/blockpulse&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Curious if anyone's solved this differently — happy to discuss tradeoffs in the comments.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>redis</category>
      <category>webdev</category>
      <category>blockchain</category>
      <category>ethereum</category>
    </item>
  </channel>
</rss>
