superfly / fly

Deploy app servers close to your users. Package your app as a Docker image, and launch it in 17 cities with one simple CLI.
https://fly.io
985 stars 48 forks source link

Redis cache cluster #294

Open mrkurt opened 4 years ago

mrkurt commented 4 years ago

Redis makes a really nice global cache service. Replicas can be configured to allow writes with replica-read-only false. With a reasonably small dataset, replicas can be added and removed quickly too.

Making this work on Fly (currently) requires two applications.

  1. A "primary" Redis app with a min-count and max count of 1, it should not scale.
  2. A replica Redis app configured to replicate from the primary Redis, replica-read-only false, and no disk persistence.

The primary Redis is useful for propagating commands to all the replicas. A del sent to the primary will remove keys in all the replicas. A set will propagate to all the replicas. Writes to each replica are invisible to other replicas, so the best pattern for using this is to do most caching operations against the replica app, and only write to the primary app for things that need to be the same everywhere.

hhff commented 4 years ago

Thanks @mrkurt - this approach requires read/write splitting in the implementation code though, right?

ie - it's not what you'd called "active-active", and would require that your reads hit a different redis DSN to the writes, correct?

mrkurt commented 4 years ago

Kind of. You will have two addresses, like these:

  1. REDIS_CACHE_URL
  2. REDIS_GLOBAL_CACHE_URL

In this configuration, you'd mostly be reading AND writing against REDIS_CACHE_URL. You'd only need to use the REDIS_GLOBAL_CACHE_URL when you want data to propagate to all the regions.

This works because Redis allows writes against a replica.

mrkurt commented 4 years ago

Migrated to Discourse: https://community.fly.io/t/redis-cache-cluster/62