How to Enable Password Authentication with ElastiCache Redis Cluster Mode in a Laravel Project
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
This article provides a summary of how to enable password authentication in cluster mode for ElastiCache Redis in a Laravel project.
Environment Verified
The environment verified in this article is based on Laravel 8 documentation, so as long as the Predis version matches, the implementation should be possible.
Implementation Specifications
- PHP Laravel (EC2)
- ElastiCache Redis (AWS)
- Cluster Mode
- Password Authentication
- Encryption in Transit (TLS)
- Other settings to minimize costs
Creating ElastiCache Redis Cache in AWS Console
In this experiment, cost-effective configurations are used to set up the environment.
- Deployment Option: Custom cache design
- Creation Method: Cluster cache
- Cluster Mode: Enabled
- Cluster Information: Enter name and description
- Location: AWS Cloud, Multi-AZ disabled
- Cluster Settings: Engine version 7.1, Port 6397, Parameter group 'default.redis7.cluster.on', Node type 'cache.t3.micro', Shard count 1, Replica count 0
- Connectivity: Network type IPv4, Subnet select 'Create a new subnet group' in the same VPC as EC2
- AZ Deployment: Slot and key space distributed evenly, no specific AZ selection
- Security: Enable stored and in-transit encryption, set access control to 'Redis authentication default user access', set Redis authentication token to desired password (e.g., 'password')
- Security Group: Inbound rule for EC2 security group and port 6379, Outbound rule 'None'
- Automatic Backup: Disable automatic backups for testing purposes
- Maintenance: No specific maintenance window, enable automatic upgrades for minor versions, disable Amazon SNS notification topics
- Logs: Disable slow logs and engine logs
- Tags: None
Laravel Configuration
Install Packages
Use the Predis package for this implementation.
Update config/database.php
Edit the Redis configuration in this file to use Predis exclusively for the client and specify the necessary options.
Working with EC2
ElastiCache Connection Testing
ElastiCache does not support local server connection testing, and attempting to connect locally may result in a 'No connections available in the pool' error.
Update .env
Edit the .env file within the Laravel project on EC2 to include the necessary Redis configuration details.
Adjust CACHE_DRIVER to redis
Changing this configuration option ensures that Laravel uses the Redis cache.
Update REDIS_HOST without port
Remove the port number from the copied ElastiCache endpoint to avoid errors.
Enter REDIS_PASSWORD
Enter the password set during the ElastiCache security configuration.
Additional Information
- The cost of ElastiCache nodes is calculated based on the number of nodes.
- Network bandwidth limitations can affect performance, especially with larger keys.
- Consider testing node performance in real workloads to understand network throughput limitations.
Conclusion
This article provides a guide on implementing password authentication for ElastiCache Redis cluster mode in a Laravel project on AWS EC2.