Cap-go / capgo

Instant update for capacitor apps
https://capgo.app
GNU Affero General Public License v3.0
115 stars 57 forks source link

Proposal to add redis caching #308

Closed WcaleNieWolny closed 1 year ago

WcaleNieWolny commented 1 year ago

Currently the update url does not use any caching. I propose we add redis to reduce the load on the postgress db My proposal is to do the following:

Given that we have 7M request per day (data from martin, not sure how accurate) and would probably fit in some of these fixed plans (redis enterprise)

image

I strongly believe that with a strong caching we can reduce the load on supabase by at least 50-80% making the extra cost worth it

WcaleNieWolny commented 1 year ago

Further price calculation would require data from @riderx (Number of apps, price for supabase, number of devices that are overwritten in any way, number of devices, perhaps even active uses of all apps, request to the updates endpoint per day)

Also AB testing / progressive deploy is an interesting issue. This is not easy to implement however it would allow capgo to scale more efficiently

WcaleNieWolny commented 1 year ago

I have implemented further PoC for this. I have modeled the redis data in the following way:

image

This allows for a very efficient caching of data. When someone uploads a new version we invalidate (remove) all of the ver_* keys from this hash (hash is just an object in redis AFAIK)

When someone overwrites a device we remove said device from the cache thus allowing the cache keys to stay between version uploads. The way I see this is that we can have the ver_* keys with an infinite TTL and the device_* keys with a TTL of 3 days (for example 3 days)

WcaleNieWolny commented 1 year ago

Further information on this:

I will not be using lua. Lua is pointless, sure it might reduce the overall reads but it adds complexity and a potential bottleneck. In the PoC I managed to avoid lua.

Another thing is that I think that in the final impl the old endpoint will get removed and replaced with an internal JS call (Thus avoiding the entire network stack allowing for faster caching)

Redis also add a lot of options for improving the stats endpoint as it adds the pub/sub (allowing for batch SQL updates) or potentially moving stats from supabase to redis

WcaleNieWolny commented 1 year ago

I am strongly in favor of using zod for the new edge functions to avoid manual parsing.

Please let me know if the usage of zod is acceptable

riderx commented 1 year ago

For provider, I was more thinking of this: https://upstash.com/docs/redis/overall/pricing it's more made for serverless. What do you think ? For zod, i'm not sure why you need it, can you elaborate more ?

WcaleNieWolny commented 1 year ago

I used zod as it allowed for quick and easy prototyping. It reduced the complexity of the code and decreased the potential for undefined errors.

I believe zod is not necessary for this, however migrating the manual parsing in all of the edge fns to zod might increase the overall error quality in our product