Contenido del Curso
Introduction to Redis
Introduction to Redis
Practical Use of Hashes
We'll fully assemble our application and test it using Redis and Spring Boot. Caching will significantly speed up request processing and reduce the load on the database.
Brief Summary from the Video
In our program, we used the following logic: when a user is added to the main database, their data is not cached because it's not necessary yet.
The createUser
method simply saves the user to the database.
When a request comes in to fetch a user's data by ID
, we first check if the information is available in the Redis cache. This helps avoid unnecessary database queries if the data is already cached.
Using the @Cacheable
annotation, we cache the data under the key user-cache
, which includes the value of the id (user-cache:20
for a user with ID 20
). If the data is in the cache, it is retrieved and returned. If the data is not in the cache, the method queries the database.
When deleting data from the database, it's important to remove it from the cache as well to ensure the data remains consistent and outdated information is not used.
The deleteUser
method deletes the user from the database and clears their data from the Redis cache to prevent using outdated information in future queries.
Benefits of Caching
Now for the exciting part — why did we implement caching? After adding the Redis cache, requests became much faster — literally up to 20 times faster! This is clearly demonstrated in the screenshots below.
Before Caching
Before implementing caching, each request went directly to the database, resulting in significant delays during processing.
After Caching
With caching in place, most requests are now handled by Redis, which significantly reduces response time.
Summary
Caching with Redis has allowed us to optimize the application's performance, speed up request processing, and reduce the load on the database. This approach is especially beneficial for high-traffic applications where request processing speed is crucial.
¡Gracias por tus comentarios!