Skip to content

Commit

Permalink
Basic Fly global redis cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkurt committed Jul 15, 2021
0 parents commit 8983f86
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.envrc
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM redis:alpine

RUN apk update && apk add bind-tools

ADD redis.conf /usr/local/etc/redis/redis.conf
ADD start-redis-server.sh /usr/bin/
RUN chmod +x /usr/bin/start-redis-server.sh

CMD ["start-redis-server.sh"]
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Redis Geo Cache

This is an example Redis configuration that runs a primary Redis in one region and replicas in other regions. Writes to the primary region propagate, writes to other regions are region local.

Redis includes a `replica-read-only no` setting that makes it especially effective for geo caching. When you run replicas that allow writes, you can write to the closest replica for most ephemeral cache data, and write to the master for commands that should propagate.

### Usage

This is a Fly app that requires volumes. Assuming you want a primary redis in the `scl` region and a replica in the `ord` region, this is what you need to do.

1. Clone this repository
2. `fly launch`, choose to import the existing config
3. Choose `n` when it asks if you want to deploy
4. Set a redis password: `fly secrets set REDIS_PASSWORD=<password>`
5. Create a volume in your primary region:
```
fly volumes create redis_server --size 10 --region scl
```
6. `fly deploy`
7. Add volumes in other regions:
```
fly volumes create redis_server --size 10 --region ord
```
8. Add instances: `fly scale count 2`

### Connecting from your application

You should run your application in the same regions as Redis, and use the region specific addresses to connect. It's helpful to set a matching `PRIMARY_REGION` environment variable on any application you connect to your Redis cluster so you can make choices about where to send writes.

The primary Redis URL is:

```bash
# format
redis://x:<password>@<primary-region>.<appname>.internal:5432

# example in scl
redis://x:[email protected]:5432
```

Read replicas are similar, but using a different region prefix (the `$FLY_REGION` environment variable is handy here).

To generate a local read replica with Node.js, you might do something like:

```javascript
const primary = new URL("redis://x:[email protected]:5432")
const replica = new URL(primary)
replica.hostname = `${process.env['FLY_REGION']}.my-redis-app.internal`
replica.toString()
// 'redis://x:[email protected]:5432'
```
9 changes: 9 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[experimental]
auto_rollback = false

[env]
PRIMARY_REGION = "scl"

[mount]
destination = "/data"
source = "redis_server"
15 changes: 15 additions & 0 deletions redis.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# write to /data/
dir /data/

# snapshot settings
save 900 1
save 300 10
save 60 10000

# allow writes to replicas
replica-read-only no

# never become master
replica-priority 0

# replica should come after this
25 changes: 25 additions & 0 deletions start-redis-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh
sysctl vm.overcommit_memory=1
sysctl net.core.somaxconn=1024

# write region to local redis
nohup /bin/sh -c "sleep 10 && redis-cli -a $REDIS_PASSWORD set fly_region $FLY_REGION" >> /dev/null 2>&1 &

if [ "$PRIMARY_REGION" = "$FLY_REGION" ]; then
# start master
echo "Starting primary in $PFLY_REGION"
redis-server /usr/local/etc/redis/redis.conf \
--requirepass $REDIS_PASSWORD
else
# start replica
echo "Starting replica: $FLY_REGION <- $PRIMARY_REGION"

# redis is dumb and can't replicate from an ipv6 only hostname
ip=$(dig aaaa $PRIMARY_REGION.$FLY_APP_NAME.internal +short | head -n 1)

echo "" >> /usr/local/etc/redis/redis.conf
echo "replicaof $ip 6379" >> /usr/local/etc/redis/redis.conf
redis-server /usr/local/etc/redis/redis.conf \
--requirepass $REDIS_PASSWORD \
--masterauth $REDIS_PASSWORD
fi

0 comments on commit 8983f86

Please sign in to comment.