Skip to content

Cache read queries#48

Merged
Brayden merged 4 commits intomainfrom
bwilmoth/cache
Dec 12, 2024
Merged

Cache read queries#48
Brayden merged 4 commits intomainfrom
bwilmoth/cache

Conversation

@Brayden
Copy link
Member

@Brayden Brayden commented Dec 12, 2024

Purpose

When the X-Starbase-Cache header is present, if the query in consideration is a READ operation (e.g. a SELECT statement) then by default we will cache the results for 60 seconds in our Durable Object storage. You can see the tmp_cache table will be a generated table in the DO and each row will contain the results of a single query and include the timestamp, ttl expiry, query and the results.

From internal testing I have experienced successful optimizations for external data sources cutting down from 1s or greater down to ~50ms. Regardless of the time the non-cached query takes, the cached result usually has a response time of that 50ms ballpark.

Internal data source queries in the Durable Object itself are never cached as those already have incredibly fast read times. However, any external data source connected to the StarbaseDB instance is eligible for becoming cached when the header is present. The following data sources are supported:

  • Postgres
  • MySQL
  • Mongo
  • Cloudflare D1
  • StarbaseDB
  • Turso

NOTE: Considerations were made here on how the caching mechanism could work. For the time being we landed on storing the query results in a Durable Object table. We can continue evaluating using the Cloudflare Cache API to do this but it seems that requires us to cache a Response object and need to verify that we do this at the right point in time and are able to pull out the request to fetch the cache object as well. To move in this direction would also want to do some more testing in the maximum cache response size (per response) as well as any additional potential latency that might be added to our request. The hope would be a higher cache size per response + no incurred latency penalty.

Tasks

  • Add a beforeQueryCache function to check if the queries response is cached
  • Add a afterQueryCache function to check eligibility to, and then cache the result
  • Ensure the database connection is preserved as long as possible in a globalConnection variable

Verify

  • Test REST API
  • Test internal data source queries (should not be cached)
  • Test external data source queries (without X-Starbase-Cache header)
  • Test external data source queries (with X-Starbase-Cache header)

Before

After

@Brayden Brayden self-assigned this Dec 12, 2024
@Brayden Brayden added the enhancement New feature or request label Dec 12, 2024
@Brayden Brayden merged commit fa0209e into main Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant