Our Redis for Local Magento 2 development tutorial is a direct add-on to our previous blog post: Docker for Magento 2 Development. If you are not already using Docker for local Magento 2 development, it will be necessary to follow that tutorial first.
Why Use Redis for Local Magento 2 Development?
In this developer tutorial we show you how to use Redis for local Magento 2 devevlopment on Docker. In short, Redis offers a more efficient and better-performing mechanism for caching and session storage. To learn more about Redis in general as well as the available commands and documentation, click here.
Redis is a popular caching and session storage solution for Magento 2. Your production environment is most likely using it. As any experienced developer knows, it is important that your local development environment is an exact replica (or close to it) of your production environment. Fortunately, Docker is awesome. Adding a Redis container to our Docker development environment is going to be easy. We will confirm it is working as expected at the end of this tut.
Magento recommends Redis for default cache, page cache, and session storage. Metadata and cache records are stored in far fewer files which makes for faster and more efficient performance. Additionally, tags are indexed in files which prevents the need for scanning all cache files when conducting tag operations.
Add Redis to the Docker + M2 Mix
Adding and connecting a Redis container to your existing Docker + Magento 2 setup is a fairly straightforward process. Simply follow the steps below and you will be on your way in no time at all!
- Add Redis service/container to docker-compose.yml
- Tell Magento to use Redis for caching and session storage
- Rebuild Docker
- Verify that Redis is working
To follow this tutorial on YouTube, click here.
Add Redis Service
The docker-compose.yml file below is the same file that you created in the initial tutorial. The highlighted lines in the file below show what we have added and how we link the web service with the Redis service. It also shows how we can persist the cached data through the use of data volumes. Even if we tear down and remove all of the Docker services/containers, we will not lose any data when we spin the VM back up.
version: '3' services: web: image: webdevops/php-apache-dev:ubuntu-16.04 container_name: web restart: always user: application environment: - WEB_ALIAS_DOMAIN=local.domain.com - WEB_DOCUMENT_ROOT=/app/pub - PHP_DATE_TIMEZONE=EST - PHP_DISPLAY_ERRORS=1 - PHP_MEMORY_LIMIT=2048M - PHP_MAX_EXECUTION_TIME=300 - PHP_POST_MAX_SIZE=500M - PHP_UPLOAD_MAX_FILESIZE=1024M volumes: - /path/to/magento:/app:cached ports: - "80:80" - "443:443" - "32823:22" links: - mysql # link web service to Redis service - redis mysql: image: mariadb:10 container_name: mysql restart: always ports: - "3306:3306" environment: - MYSQL_ROOT_PASSWORD=root - MYSQL_DATABASE=magento volumes: - db-data:/var/lib/mysql phpmyadmin: container_name: phpmyadmin restart: always image: phpmyadmin/phpmyadmin:latest environment: - MYSQL_ROOT_PASSWORD=root - PMA_USER=root - PMA_PASSWORD=root ports: - "8080:80" links: - mysql:db depends_on: - mysql #add Redis service and connect to internal data volume redis: image: redis:latest container_name: redis restart: always volumes: - redis-data:/data volumes: db-data: external: false #add internal data volume so that data persists redis-data: external: false
Update Magento’s env.php File
In the original tutorial, the one in which you installed Magento, a file was automatically created for you at app/etc/env.php. The highlighted changes below should be added to that file. In this file, there should already be a “session” element in the array. Remove that session element completely and replace it with the highlighted lines from below, exactly as you see them. Once this is done, Magento will know to use Redis for caching and session storage. It will also know where the Redis server is located and how to connect to it.
<?php return [ 'backend' => [ 'frontName' => 'admin' ], 'crypt' => [ 'key' => '12f933de0c87811c434fb205c8bb8e9e' ], 'db' => [ 'table_prefix' => '', 'connection' => [ 'default' => [ 'host' => 'mysql', 'dbname' => 'magento', 'username' => 'root', 'password' => 'root', 'model' => 'mysql4', 'engine' => 'innodb', 'initStatements' => 'SET NAMES utf8;', 'active' => '1' ] ] ], 'resource' => [ 'default_setup' => [ 'connection' => 'default' ] ], 'x-frame-options' => 'SAMEORIGIN', 'MAGE_MODE' => 'default', 'cache' => [ 'frontend' => [ 'default' => [ 'backend' => 'Cm_Cache_Backend_Redis', 'backend_options' => [ 'server' => 'redis', 'database' => '0', 'port' => '' ] ], 'page_cache' => [ 'backend' => 'Cm_Cache_Backend_Redis', 'backend_options' => [ 'server' => 'redis', 'port' => '', 'database' => '1', 'compress_data' => '0' ] ] ] ], 'session' => [ 'save' => 'redis', 'redis' => [ 'host' => 'redis', 'port' => '', 'password' => '', 'timeout' => '2.5', 'persistent_identifier' => '', 'database' => '2', 'compression_threshold' => '2048', 'compression_library' => 'gzip', 'log_level' => '1', 'max_concurrency' => '6', 'break_after_frontend' => '5', 'break_after_adminhtml' => '30', 'first_lifetime' => '600', 'bot_first_lifetime' => '60', 'bot_lifetime' => '7200', 'disable_locking' => '0', 'min_lifetime' => '60', 'max_lifetime' => '2592000' ] ], 'cache_types' => [ 'config' => 1, 'layout' => 1, 'block_html' => 1, 'collections' => 1, 'reflection' => 1, 'db_ddl' => 1, 'eav' => 1, 'customer_notification' => 1, 'config_integration' => 1, 'config_integration_api' => 1, 'full_page' => 1, 'translate' => 1, 'config_webservice' => 1 ], 'install' => [ 'date' => 'Sat, 23 Jun 2018 06:17:17 +0000' ] ];
Rebuild the Docker Environment
Our next step is to rebuild Docker. In the previous steps, we added the Redis service to the docker-compose configuration. We also added Redis to the Magento configuration. However, if you
docker ps you will notice that the container named redis still does not exist. If you were to open the Magento site in a browser, you would probably receive a fatal error. This error will occur because Magento is looking for a Redis host but does not find it. This is actually a good sign. Let’s go ahead and take care of the error.
docker-compose up -d --build
Verify That Redis is Working
Upon completion of step number 3, Redis should be up and running and Magento should be able to find the Redis server. Run a quick
docker ps to make sure that there is now a container named redis. If there is, our installation and configuration of Redis is complete. Let’s go ahead and confirm that it is being used.
docker exec -it redis bash
- Now, in a browser, open or refresh a page on your Magento site. After you do that, return to you terminal and you should see that it filled up with caching and session data. If you see this, your addition of Redis is complete and confirmed to be working properly.
Also in the Magento 2 Development Environment Series