Skip to content

Caching based on cache aside strategy (write on miss) using Redis

Notifications You must be signed in to change notification settings

lucasvsme/poc-redis-cache-aside

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

POC: Redis Cache Aside Strategy

It demonstrates how to implement caching based on cache aside (write on miss) strategy using Redis.

We want to implement a Web Service that allows us to create a person and find it by the random ID generated to the person after persisting it on a Postgres relational database. The goal is to cache the person after we try to find them by ID for the first time, so next requests to find that person takes less time to respond.

To avoid writing abstractions and boilerplate code, we are going to depend on Spring MVC for the Web layer, Spring Data JPA for persistence on relational database and Spring Data Redis for caching. The source code should be evaluated using automated tests with database containers provisioned by TestContainers and tests managed by JUnit.

Other approaches to cache are Write Through and Write Back.

How to run

Description Command
Run tests ./gradlew test
Run application ./gradlew bootRun

Preview

Cache Aside Strategy write execution flow:

flowchart TB
    A[POST /people\n]
    B[Persist person\nwith ID 1]
    C[Location /people/1]

    A --> B --> C
Loading

Cache Aside Strategy read execution flow:

flowchart TB
    A[GET /people/1] --> B{is person\nwith ID 1\ncached?}
    B --> |Yes| C[Return the\nPerson]
    B --> |No| D{is person\nwith ID 1\npersisted? }
    D --> |Yes| E[Cache person\nwith ID 1] --> F[Return the\nPerson]
    D --> |No| G[Return Error]
Loading

Logging output after creating a person:

2022-07-08T22:32:38.980-03:00  INFO 50734 --- [o-auto-1-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-07-08T22:32:38.980-03:00  INFO 50734 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-07-08T22:32:38.981-03:00  INFO 50734 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms
2022-07-08T22:32:39.072-03:00  INFO 50734 --- [o-auto-1-exec-1] com.example.person.PersonServiceDefault  : Person saved (person=Person(id=432d0066-8be3-4576-8737-b3c42f02ebad, name=John Smith, age=45))

Logging output after finding the person by ID multiple times:

2022-07-08T22:32:39.251-03:00  INFO 50734 --- [o-auto-1-exec-2] com.example.person.PersonServiceDefault  : Person retrieved from database (personId=432d0066-8be3-4576-8737-b3c42f02ebad)
2022-07-08T22:32:39.255-03:00  INFO 50734 --- [o-auto-1-exec-2] com.example.person.PersonServiceDefault  : Person cached (key=432d0066-8be3-4576-8737-b3c42f02ebad, value=Person(id=432d0066-8be3-4576-8737-b3c42f02ebad, name=John Smith, age=45))
2022-07-08T22:32:39.273-03:00  INFO 50734 --- [o-auto-1-exec-3] com.example.person.PersonServiceDefault  : Person retrieved from cache (personId=432d0066-8be3-4576-8737-b3c42f02ebad)
2022-07-08T22:32:39.281-03:00  INFO 50734 --- [o-auto-1-exec-4] com.example.person.PersonServiceDefault  : Person retrieved from cache (personId=432d0066-8be3-4576-8737-b3c42f02ebad)
2022-07-08T22:32:39.289-03:00  INFO 50734 --- [o-auto-1-exec-5] com.example.person.PersonServiceDefault  : Person retrieved from cache (personId=432d0066-8be3-4576-8737-b3c42f02ebad)