Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add short-lived cache handlers #106

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

meyer9
Copy link
Contributor

@meyer9 meyer9 commented Dec 10, 2024

Description

We currently send a ton of traffic to backend nodes on barely uncacheable endpoints (endpoints based on block number). This PR adds a cache with a very short TTL (block_ttl - defaults to 2 seconds) for these endpoints.

Since Redis 2.6 the expire error is from 0 to 1 milliseconds.

Short TTLs seem fine for Redis. Other caching backends will currently ignore short lived cache keys.

Open question: should we implement this for in-memory cache?

Tests

TODO

Additional context

Add any other context about the problem you're solving.

proxyd/cache.go Outdated Show resolved Hide resolved
proxyd/config.go Outdated
TTL TOMLDuration `toml:"ttl"`
Enabled bool `toml:"enabled"`
TTL TOMLDuration `toml:"ttl"`
BlockTTL TOMLDuration `toml:"block_ttl"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

block_ttl could probably be changed to something live ShortLivedTTL to better reflect it can be used for multiple RPCs

Comment on lines +205 to +215
"eth_getBlockByNumber": shortLivedHandler,
"eth_blockNumber": shortLivedHandler,
"eth_getBalance": shortLivedHandler,
"eth_getStorageAt": shortLivedHandler,
"eth_getTransactionCount": shortLivedHandler,
"eth_getBlockTransactionCountByNumber": shortLivedHandler,
"eth_getUncleCountByBlockNumber": shortLivedHandler,
"eth_getCode": shortLivedHandler,
"eth_getTransactionByBlockNumberAndIndex": shortLivedHandler,
"eth_getUncleByBlockNumberAndIndex": shortLivedHandler,
Copy link
Contributor

@jelias2 jelias2 Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should all of these be low TTL? ex. eth_getCode, eth_getBlockByNumber or eth_getBlockTransactionCountByNumber seem like they would return consistent results ?

Just curious how these RPCs were chosen, any related data? I get that tip relevant RPCs and rapidly changing data fit the low TTL behavior

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow up could be making this configurable but I think that maybe out of scope for the inital PR

proxyd/proxyd.go Outdated
Comment on lines 313 to 315

blockTtl := defaultBlockTtl
if config.Cache.BlockTTL != 0 {
blockTtl = time.Duration(config.Cache.BlockTTL)
}
cache = newRedisCache(redisClient, redisReadClient, config.Redis.Namespace, ttl, blockTtl)

Copy link
Contributor

@jelias2 jelias2 Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to write the feature so the shortLivedTTL can be toggled on and off? With the default being off? It looks like in its current state shortLivedTTL will always be enabled

Copy link
Contributor

@jelias2 jelias2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good, definitely can get this merged! Left a couple comments. PTAL, and in the final form please remove the prints, and add a couple more UTs if the feature can be toggled on and off.

Thanks for the contribution @meyer9!

@jelias2
Copy link
Contributor

jelias2 commented Dec 12, 2024

Currently the PR is still in draft, please update once its ready.
Also could the readme be updated under cacheable method with a section about the shortlived TTL

proxyd/cache.go Outdated Show resolved Hide resolved
@meyer9 meyer9 force-pushed the meyer9/short-lived-cache branch from 9202ab3 to bd91511 Compare December 13, 2024 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants