Code Monkey home page Code Monkey logo

upgrade-code-challenge's Introduction

Campsite Booking System

Design

This campsite booking is a read heavy system. The data are stored in DB. It has two tables:

  • Booking: Stores the user email, first name, last name, confirmation code (We can normalize the user info in a 'User' table in future enhancement.)
  • BookingDate: Stores the reservation dates for this booking

I use redis as the cache server. It caches all the dates that have been reserved in 30 days. Redis cache is thread safe as Redis is implemented with single thread. It not only serves as cache but also provides the distributed lock to synchronize the data access. It helps to ensure cache data are synchronized across multiple micro service instances if we do want to scale this campsite booking system horizontally. Without that, we will have much more DB calls and DB lock. DB locks are much more expensive than the redis lock.

Maven Modules

  • campsite-spec

    • This module defines OpenApi Spec : CampsiteApi-v1.yaml. It provides two sets of APIs:
      • Booking APIs: It can add, update and delete the booking.
      • AvailableDate APIs: It provides the available dates within a date range (max 30 future days) for user to pick up.
    • The maven build will generate the server/client code based on the spec.
  • campsite-service

    • This module implement the server site logic. The service class - CacheableBookingServiceImpl has the most business logic and handles the cache/DB access.

Test Cases

  • CachealbeBookingServiceIT.java

    • It uses the H2 DB and a embeded Redis server. The test case 'testConcurrent_Add_Edit' will simulate the concurrent reservation requests. It picks up the booking dates randomly and makes the reservation, then it will update those reservations/bookings by picking up or releasing some dates back to the pool. Please note that some of add/update booking calls may have DateNotAvailabeException or NoPickupDateException. You will see some warning log. This is to simulate the real world situation: the user selects the date first, then makes the reservation requests. But between that time, those dates may be booked by others.
      • To run it:
        mvn clean install
        
  • CachealbeBookingServiceITDocker.java

    • It connects mysql and redis to run similar test cases as the above CachealbeBookingServiceIT.
      • The default 'mvn test' won't run this test class, you can manually run it:
        cd <checkout-root-folder>
        mvn clean install
        dock-compose up
        mvn integration-test -Dit.test=CachealbeBookingServiceITDocker
        

Run the App

  • You can use following commands to start the app.
      cd <checkout-root-folder>
      mvn clean install
      dock-compose up
      mvn spring-boot:run
  • API usage:
    • You can check the API spec 'CampsiteApi-v1.yaml' to see how to use APIs. Below is an example to get the available dates.
curl --location --request GET 'http://localhost:8080/campsite-service/v1/availableDate?checkInDate=2021-08-01&checkOutDate=2021-08-15'

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.