Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: allow running Redis standalone #2268

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ MBTA_API_BASE_URL=https://api-dev.mbtace.com
# If you're using Redis for local development, the port is 30001
# REDIS_HOST=
# REDIS_PORT=
REDIS_STANDALONE=true

# These credentials control access to resetting cache entries for the CMS.
# You can set them to be whatever you want.
Expand Down
58 changes: 10 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ Welcome to [Dotcom](https://www.notion.so/mbta-downtown-crossing/Dotcom-6aa7b0f0
Minor note - you may see a prompt to upgrade `npm`. This isn't needed, and `"lockfileVersion": 1` in our `assets/package-lock.json` file means it was generated with an `npm` version prior to 7.


1. Update the MBTA Metro assets. From the root of this repo:
```
mix mbta_metro.update_assets
```


1. Set up required environment variables:
```
cp .env.template .env
Expand All @@ -121,57 +127,13 @@ For details on environment configuration, including optional variables, see

## Running the Server

The easiest way to develop MBTA dotcom is to use Docker Compose.
paulswartz marked this conversation as resolved.
Show resolved Hide resolved

```
docker compose -f deploy/dev.yml up -d --build
```

This will set up Redis in cluster mode and run two versions of Dotcom with nginx load balancing requests between them.
Visit http://localhost:4001 to hit the load balancer.
http://localhost:4002 and http://localhost:4003 will take you directly to either of the two nodes.

You can even connect to individual Elixir nodes in order to run commands.
Let's say you want to connect to dotcom2 (the one running at http://localhost:4003).

```
docker exec -it deploy-dotcom-2-1 iex --sname foobarbaz --cookie foobarbaz

iex(foobarbaz@0b061394460f)1> node = :dotcom2@0b061394460f
:dotcom2@0b061394460f
iex(foobarbaz@0b061394460f)2> Node.connect(node)
true
iex(foobarbaz@0b061394460f)3> :rpc.call(node, Dotcom.Cache.Multilevel, :get, ["cms.repo|important-notices"])
...
```

Note that the address (the @... part) will be different every time.

---

If you choose not to use Docker Compose, you'll still have to run Redis cluster.
The easiest way to get it running is to download and compile it.
To run the server, you'll need to have a Redis instance running. You can either install it manually, or run it via Docker:

```
cd $HOME
wget https://github.com/redis/redis/archive/7.2.4.tar.gz
tar xvf 7.2.4.tar.gz
cd redis-7.2.4
make
./utils/create-cluster/create-cluster start
./utils/create-cluster/create-cluster create -f
```

When you're done with it:

```
./utils/create-cluster/create-cluster stop
cd $HOME
rm 7.2.4.tar.gz
rm -rf redis-7.2.4
paulswartz marked this conversation as resolved.
Show resolved Hide resolved
``` shell
docker run --rm -p 6379:6379 redis:7.2.4
```

Start the server with `iex -S mix phx.server`
Then, start the server with `iex -S mix phx.server`

Then, visit the site at http://localhost:4001.

Expand Down
22 changes: 16 additions & 6 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ end
redis_host_env = System.get_env("REDIS_HOST", "127.0.0.1")
redis_port_env = System.get_env("REDIS_PORT", "6379")

redis_mode =
if System.get_env("REDIS_STANDALONE") == nil do
:redis_cluster
else
:standalone
end

redis_host =
if redis_host_env == "",
do: "127.0.0.1",
Expand All @@ -58,22 +65,25 @@ redis_port =
do: 6379,
else: String.to_integer(redis_port_env)

redis_conn_opts = [
host: redis_host,
port: redis_port
]

redis_config = [
mode: :redis_cluster,
mode: redis_mode,
conn_opts: redis_conn_opts,
redis_cluster: [
configuration_endpoints: [
conn_opts: [
host: redis_host,
port: redis_port
]
conn_opts: redis_conn_opts
]
],
stats: true,
telemetry: true
]

# This is used by PubSub, we only use the first node in the cluster
config :dotcom, :redis_config, redis_config[:redis_cluster][:configuration_endpoints][:conn_opts]
config :dotcom, :redis_config, redis_conn_opts

# Set caches that use the Redis cluster
config :dotcom, Dotcom.Cache.Multilevel,
Expand Down
65 changes: 65 additions & 0 deletions docs/RedisClustering.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Testing Redis Clustering

The default development environment only runs under a single Redis instance.
Some features (cross-cluster delete) do need a full cluster to test, so this
document describes how to run such as cluster locally for testing.

These instructions assume you've already gotten a normal development environment running.

## Docker Compose

The easiest way to run the Redis Cluster is to use Docker Compose.

```
docker compose -f deploy/dev.yml up -d --build
```

This will set up Redis in cluster mode and run two versions of Dotcom with nginx load balancing requests between them.
Visit http://localhost:4001 to hit the load balancer.
http://localhost:4002 and http://localhost:4003 will take you directly to either of the two nodes.

You can even connect to individual Elixir nodes in order to run commands.
Let's say you want to connect to dotcom2 (the one running at http://localhost:4003).

```
docker exec -it deploy-dotcom-2-1 iex --sname foobarbaz --cookie foobarbaz

iex(foobarbaz@0b061394460f)1> node = :dotcom2@0b061394460f
:dotcom2@0b061394460f
iex(foobarbaz@0b061394460f)2> Node.connect(node)
true
iex(foobarbaz@0b061394460f)3> :rpc.call(node, Dotcom.Cache.Multilevel, :get, ["cms.repo|important-notices"])
...
```

Note that the address (the @... part) will be different every time.


## Running Redis Locally

If you choose not to use Docker Compose, you'll still have to run Redis cluster.
The easiest way to get it running is to download and compile it.

```
cd $HOME
wget https://github.com/redis/redis/archive/7.2.4.tar.gz
tar xvf 7.2.4.tar.gz
cd redis-7.2.4
make
./utils/create-cluster/create-cluster start
./utils/create-cluster/create-cluster create -f
```

Start the server with `iex -S mix phx.server`

Then, visit the site at http://localhost:4001.

When you're done with the cluster:

```
./utils/create-cluster/create-cluster stop
cd $HOME
rm 7.2.4.tar.gz
rm -rf redis-7.2.4
```

Loading