This solution relies on the expiration time, Time-To-Live (TTL), of the key in Redis. Redis will not be updated proactively when MySQL is updated. This solution is simple to implement, but the duration of inconsistency may be very long, especially for scenarios with frequent read requests.
Use this main.tf
file in Terraform to provision ECS, Redis and RDS MySQL instances from this solution.
Log on ECS
and set up environment Note: This tutorial uses Alibaba Cloud Linux. When you check with the terraform script, you will see the image id: aliyun_2_1903_x64_20G_alibase_20200904.vhd 1.1
Get the EIP of the ECS and then log on to ECS via SSH. Please use the account root/N1cetest; the password has been predefined in Terraform script for this tutorial. If you changed the password, please use the correct password accordingly.
ssh root@ 1.2
Run the following command to install the required utilities on the instance, including JDK, Maven, and MySQL with MySQL Client:
yum install java-latest-openjdk.x86_64 yum install maven yum install mysql
Modify and run application 2.1
Execute the following commands one by oneDownload the demo application code, and then build the demo Java application.
wget https://github.com/alibabacloud-howto/solution-mysql-redis-cache-simple/raw/master/source.tar.gz tar xvf source.tar.gz && cd source mvn clean package assembly:single -DskipTests 2.2
Edit the env.sh file to set the Redis and RDS MySQL endpoints (you can get from the Redis and RDS MySQL web console) accordingly. 2.3
Use the mysql_client.sh to create a demo table and insert some records into RDS MySQL.
sh mysql_client.sh < rosebowl.sql 2.4
Run the Redis demo application to verify the cache and TTL logic. You can pick up a query from the query.sql file to run. The default TTL is set to 10 seconds in the demo application and it will tell you whether the result is fetched from the MySQL or Redis cache. The life time of query result in Redis cache is only 10 seconds. If you want to make it longer, you can modify the code
and build it to run again.
Database Migration Solution
Migrate to fully managed cloud databases.
Learn More >
Reach Alibaba Cloud experts for support