Menu

1 / 2

Stop using TTL for Ethereum contract cache — use event-driven invalidation instead

DEV Community·Dmitry·6 months ago
#HudlB2GN
#redis#webdev#blockchain#ethereum#contract
Reading 0:00
15s threshold

If you're building a dApp, you've probably written something like this:

const cached = await redis . get ( `cdp:position: ${ user } ` ); if ( cached ) return JSON . parse ( cached ); const position = await cdpContract . methods . getPosition ( user ). call (); await redis . setEx ( `cdp:position: ${ user } ` , 60 , JSON . stringify ( position )); // 60s TTL return position ;

Enter fullscreen mode

Exit fullscreen mode

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.

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.

The insight

Contract state only changes when a transaction is mined.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.