Using Redis to Store Sessions
Depending on the website traffic and/or its infrastructure, you might want to use Redis to manage user sessions instead of PostgreSQL.
When we talked about branching the project's code to move sessions from the filesystem to the database, we listed all the needed step to add a new service.
Here is how you can add Redis to your project in one patch:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
--- a/.platform.app.yaml
+++ b/.platform.app.yaml
@@ -14,6 +14,7 @@ runtime:
- iconv
- mbstring
- pdo_pgsql
+ - redis
- sodium
- xsl
@@ -40,6 +41,7 @@ mounts:
relationships:
database: "database:postgresql"
+ redis: "rediscache:redis"
hooks:
build: |
--- a/.platform/services.yaml
+++ b/.platform/services.yaml
@@ -16,3 +16,6 @@ varnish:
files:
type: network-storage:2.0
disk: 256
+
+rediscache:
+ type: redis:5.0
--- a/compose.yaml
+++ b/compose.yaml
@@ -20,6 +20,10 @@ services:
# - ./docker/db/data:/var/lib/postgresql/data:rw
###< doctrine/doctrine-bundle ###
+ redis:
+ image: redis:5-alpine
+ ports: [6379]
+
volumes:
###> doctrine/doctrine-bundle ###
database_data:
--- a/config/packages/framework.yaml
+++ b/config/packages/framework.yaml
@@ -9,7 +9,7 @@ framework:
# Enables session support. Note that the session will ONLY be started if you read or write from it.
# Remove or comment this section to explicitly disable session support.
session:
- handler_id: '%env(resolve:DATABASE_URL)%'
+ handler_id: '%env(REDIS_URL)%'
cookie_secure: auto
cookie_samesite: lax
Isn't it beautiful?
"Reboot" Docker to start the Redis service:
1 2
$ docker compose stop
$ docker compose up -d
Test locally by browsing the website; everything should still work as before.
Commit and deploy as usual:
1
$ symfony cloud:deploy
Going Further
This work, including the code samples, is licensed under a
Creative Commons BY-NC-SA 4.0
license.