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

[Driver] Allow stale liquidity for quote requests #1924

Merged
merged 4 commits into from
Oct 9, 2023

Conversation

fleupold
Copy link
Contributor

@fleupold fleupold commented Oct 7, 2023

Description

When giving production traffic to the colocated setup, we noticed a lot of price estimators (even fast ones like baseline) getting rate limited frequently. This is due to the driver requesting liquidity for the most recent block and blocking on fetching it if it's not available. For the legacy setup, there was a code path specifically for quotes, allowing to use a "recent" instead of the latest block for fetching liquidity.

This PR recreates this path for the co-located setup

Changes

  • Adds a flag to the liquidity fetcher indicating whether stale liquidity is allowed
  • If this flag is set, quote for recent instead of latest block (which should already be cached)

How to test

Run this benchmark script against before and after:

SECONDS=0 
while (( SECONDS < 60 )); do
  time curl -H 'content-type: application/json' --data '{"from": "0xc3792470cee7e0d42c2be8e9552bd651766c5178","buyToken": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","sellToken": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2","kind": "sell","sellAmountAfterFee": "100000000000000000", "quality":"optimal"}' http://localhost:8080/api/v1/quote
done

observe http://localhost:11088/metrics in both cases and look for liquidity cache hits:

Before:

driver_recent_block_cache_hits{cache_type="uniswapv2"} 34300
driver_recent_block_cache_misses{cache_type="uniswapv2"} 840

After

driver_recent_block_cache_hits{cache_type="uniswapv2"} 39200
driver_recent_block_cache_misses{cache_type="uniswapv2"} 140

Note that we now only have 1 cache miss (140 pools) on cold start vs 1 cache miss on each new block and higher overall throughput

Related issues

#1672

@fleupold fleupold requested a review from a team as a code owner October 7, 2023 06:39
@nlordell nlordell mentioned this pull request Oct 8, 2023
2 tasks
@nlordell
Copy link
Contributor

nlordell commented Oct 8, 2023

Added some changes in #1926 - sorry didn't see this PR when I started working on it.

Nicholas Rodrigues Lordello and others added 2 commits October 9, 2023 11:48
# Description

This PR was my take on recent liquidity fetching for the driver.
Unfortunately, I did not see #1924 when I started working on it, but I
believe some of my proposed changes are worthwhile including, so I
rebased it onto the aforementioned PR.

In particular, this PR (just like the one its based on) uses recent
instead of latest liquidity for quoting. This improves latency of
quotes.

# Changes

- [x] Introduce a `liquidity::When` enum to specify when to fetch
liquidity for (latest vs recent) instead of using a `bool` flag.
- [x] Add an E2E test to verify this. Note that this kind of E2E test is
imperfect (since it involves timings, which is never great), but it is
capturing the expected behavior of quotes (and because the cache update
loop is long enough, it doesn't fail the many times I ran it).

## How to test

E2E test that verifies liquidity cache is not being updated for the
`/quote` request.
@fleupold fleupold enabled auto-merge (squash) October 9, 2023 09:49
@fleupold fleupold merged commit 5be8e8f into main Oct 9, 2023
@fleupold fleupold deleted the allow_stale_liquidity_for_quotes branch October 9, 2023 10:07
@github-actions github-actions bot locked and limited conversation to collaborators Oct 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants