Coder Social home page Coder Social logo

lucasvalenteds / poc-redis-cache-aside Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 151 KB

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

Java 100.00%
java redis spring-data-jpa caching caching-strategies spring-data-redis cache-aside

poc-redis-cache-aside's Introduction

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)

poc-redis-cache-aside's People

Contributors

lucasvsme avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.