Testing shared caching for Supabase with Valkey: SupaFuse benchmarks and invalidation behavior #53304
Shuen14
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hi everyone!
We tested a shared response cache in front of Supabase's PostgREST API using SupaFuse and Valkey. SupaFuse wraps the
supabase-jsfetch path, so application queries keep the usual.from(...).select(...)syntax while multiple backend processes can share cached responses.With our local workload, SupaFuse hot reads averaged 0.208 ms, compared with 0.531 ms for uncached SDK calls, and made no PostgREST requests. A cold read plus cache fill took 2.415 ms. We also tested invalidation, expiry, request deduplication, and shared rate limiting against real services.
1. Test setup
9.1.3, one primary and one asynchronous replica; RDB/AOF disabled17.6, PostgREST16.1, Kong2.8.1, Auth2.195.0, Realtime2.124.426.13.0,supabase-js@2.112.3,ioredis@6.0.0, unmodifiedsupafuse@1.0.1@supabase-cache-helpers/postgrest-server@0.4.0, using only its Redis storeApplication cache reads and writes went to the primary. The replica was used to check that data, TTLs, and deletions replicated. This was a local primary/replica test; it did not exercise Cluster, Sentinel, or failover.
2. A small runnable cache lifecycle test
This example checks cold read → cache hit → write invalidation → refetch → TTL expiry → refetch with one client and a fixed anonymous identity. It uses a 3-second TTL to keep the run short. The cross-process, Realtime, and rate-limit results in the next section come from the broader test suite.
Start your own local Supabase instance first. You need a working Data API, a valid legacy
role=anonJWT, and a PostgreSQL connection that can create/drop test tables and grant permissions. The script creates a randomly named table and cache prefix, givesanonaccess only to its test row, and removes its fixtures when it finishes. All three endpoints must use127.0.0.1orlocalhost.Create an empty working directory and save the following three files there.
compose.yml — Valkey primary and replica
Save
.secrets.envwith your local credentials:reproduce-cache.mjs — complete core reproduction
Install the pinned top-level packages and run the script:
npm init -y npm install --save-exact supafuse@1.0.1 @supabase/supabase-js@2.112.3 ioredis@6.0.0 pg@8.16.3 chmod 600 .secrets.env docker compose -p supafuse-valkey-repro up -d node --env-file=.secrets.env reproduce-cache.mjs # Remove this example's Valkey containers and temporary volumes after the run. docker compose -p supafuse-valkey-repro down -vThe script counts GETs made by the guarded client after the schema-readiness probe. Our run passed all assertions:
v1v1v2v2The initial fill created one response entry and six tag sets, each with a 3-second TTL. The write removed all seven old keys before the next read. The script also checked that all keys from the next fill expired before the final read. These checks run against the primary; replica behavior was tested separately in the full suite.
3. What the broader tests verified
The full suite completed 17 passing cases and one confirmed management limitation across 18 cases. It used two independent Node.js worker processes where shared state was being tested.
v1; worker B repeated the query. Together they made one PostgREST GET. The entry and six tag sets had a 30-second TTL on the primary and replica.v2. A separate 3-second TTL case verified natural expiry and refetch.200 / 200 / 429. Request and byte budgets were also shared through Valkey. The concurrent-request cap remained per client instance.postgrest-server@0.4.0made 8 or 10. With two processes missing together, both libraries made two GETs.RedisCacheAdapter,guard.inspect().entriesreturned[]andguard.clearCache()left remote keys untouched.ioredis,strict: falsefell back to PostgREST.strict: truereturned SDKstatus: 0after roughly 7 seconds of read retries. This did not test network blackholes or reconnect delays.anonquery for a known hidden row returned[]. This checks that case only; it does not establish isolation between arbitrary user sessions.SupaFuse can invalidate after a write made through the guarded client. A separate raw SDK writer needs explicit invalidation, such as
reader.guard.invalidateTable(table), or an independently configured and ready Realtime subscription. Enabling the SupaFuse subscription does not add a table to the database publication or create its access policies.4. Latency and cache costs
The benchmark query returned 20 rows: 4,109 bytes of HTTP response body, with a 300-second cache TTL and the same anonymous identity in all modes. Each mode ran three rounds of 300 serial calls. SupaFuse dedupe, SWR, Realtime, and rate limiting were disabled during timing. MONITOR was disconnected first; cold-read iterations removed only the response value outside the timer and kept existing tag/index keys.
SupaFuse hot reads reduced mean call time by 60.7% in this setup. Its cold path averaged 1.884 ms more than the native SDK and includes fingerprinting, response serialization, and 14 sequential cache writes.
A command probe recorded 15 commands for a SupaFuse cold read: one
GET, oneSET, sevenEXPIREs, and sixSADDs.postgrest-serverissued four commands:GET, pipelinedSET ... PXATandZADD, plusZREMRANGEBYSCORE. Both hot paths issued oneGET.Adding delay only to the cache connection showed why network placement matters. Each delayed profile used three rounds of 60 calls per mode:
This was a TCP delay proxy on a shared development machine, not a measurement of a cloud network. The uncached baseline also varied between profiles. In both delayed profiles, even cache hits took longer than the local uncached query.
Two other measurements help put the latency figures in context:
MEMORY USAGE ... SAMPLES 0totaled 9,200 bytes across seven keys for SupaFuse and 6,288 bytes across two keys for postgrest-server. This counts primary cache keys only, excluding the replica and process overhead.5. Realtime readiness and test boundaries
When we dynamically added a new table to
supabase_realtime, the WebSocket channel could report that it had joined before our SQL updates produced cache invalidation. Two preflight runs needed about 14 and 42 seconds before we observed delivery. We therefore confirmed a real change event before timing the three 410–518 ms invalidation observations. Those values include polling error and exclude initialization time.The cached SELECTs used a fixed
anonidentity and a single table. Multi-user JWT/claim isolation, embedded relations, and Cluster cross-slot tag deletion need separate tests. The successful SWR case also does not rule out a refresh race under other timing conditions.For dynamically added publication tables, is there a supported readiness signal before relying on
postgres_changesfor cache invalidation? We'd also welcome feedback on the benchmark methodology and additional cases worth testing.All reactions