-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8983f86
Showing
6 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.envrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |