Git Product home page Git Product logo

poc-redis-cache-write-through's Introduction

POC: Redis Cache Write Through Strategy

It demonstrates how to implement caching based on write through 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 data before sending it to the relational database. That can help us to reduce the number of cache miss we have by guaranteeing the new data persisted is on cache.

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 Cache Aside and Write Back.

How to run

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

Preview

Write Through Strategy write execution flow:

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

Write Through 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]

Logging output after creating a person:

2022-07-09T10:41:29.484-03:00  INFO 6063 --- [o-auto-1-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-07-09T10:41:29.484-03:00  INFO 6063 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-07-09T10:41:29.485-03:00  INFO 6063 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
2022-07-09T10:41:29.682-03:00  INFO 6063 --- [o-auto-1-exec-1] com.example.person.PersonServiceDefault  : Person cached (key=edaf2eaf-b3d7-4b43-af47-c0e83f461448, value=Person(id=edaf2eaf-b3d7-4b43-af47-c0e83f461448, name=John Smith, age=45))
2022-07-09T10:41:29.702-03:00  INFO 6063 --- [o-auto-1-exec-1] com.example.person.PersonServiceDefault  : Person saved (person=Person(id=edaf2eaf-b3d7-4b43-af47-c0e83f461448, name=John Smith, age=45))

Logging output after finding the person by ID multiple times:

2022-07-09T10:41:29.749-03:00  INFO 6063 --- [o-auto-1-exec-2] com.example.person.PersonServiceDefault  : Person retrieved from cache (personId=edaf2eaf-b3d7-4b43-af47-c0e83f461448)
2022-07-09T10:41:29.770-03:00  INFO 6063 --- [o-auto-1-exec-3] com.example.person.PersonServiceDefault  : Person retrieved from cache (personId=edaf2eaf-b3d7-4b43-af47-c0e83f461448)
2022-07-09T10:41:29.778-03:00  INFO 6063 --- [o-auto-1-exec-4] com.example.person.PersonServiceDefault  : Person retrieved from cache (personId=edaf2eaf-b3d7-4b43-af47-c0e83f461448)
2022-07-09T10:41:29.786-03:00  INFO 6063 --- [o-auto-1-exec-5] com.example.person.PersonServiceDefault  : Person retrieved from cache (personId=edaf2eaf-b3d7-4b43-af47-c0e83f461448)
2022-07-09T10:41:29.795-03:00  INFO 6063 --- [o-auto-1-exec-6] com.example.person.PersonServiceDefault  : Person retrieved from cache (personId=edaf2eaf-b3d7-4b43-af47-c0e83f461448)

poc-redis-cache-write-through's People

Contributors

lucasvsme avatar

Watchers

 avatar  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.