{"id":18709207,"url":"https://github.com/jojoee/parking-lot-api","last_synced_at":"2025-08-09T03:21:53.918Z","repository":{"id":39138950,"uuid":"273704612","full_name":"jojoee/parking-lot-api","owner":"jojoee","description":"Parking lot API design","archived":false,"fork":false,"pushed_at":"2022-06-01T16:49:56.000Z","size":653,"stargazers_count":12,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-26T05:51:07.611Z","etag":null,"topics":["parking-lot","system-design"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jojoee.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-20T12:33:38.000Z","updated_at":"2024-12-03T21:27:35.000Z","dependencies_parsed_at":"2022-08-24T14:29:00.947Z","dependency_job_id":null,"html_url":"https://github.com/jojoee/parking-lot-api","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jojoee%2Fparking-lot-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jojoee%2Fparking-lot-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jojoee%2Fparking-lot-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jojoee%2Fparking-lot-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jojoee","download_url":"https://codeload.github.com/jojoee/parking-lot-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248553892,"owners_count":21123543,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["parking-lot","system-design"],"created_at":"2024-11-07T12:26:43.263Z","updated_at":"2025-04-12T10:35:11.640Z","avatar_url":"https://github.com/jojoee.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# parking-lot-api\n\n[![Travis](https://img.shields.io/travis/jojoee/parking-lot-api.svg)](https://travis-ci.org/jojoee/parking-lot-api)\n[![GitHub issues](https://img.shields.io/github/issues/jojoee/parking-lot-api)](https://github.com/jojoee/parking-lot-api/issues)\n[![GitHub license](https://img.shields.io/github/license/jojoee/parking-lot-api)](https://github.com/jojoee/parking-lot-api/blob/master/LICENSE)\n\nParking lot API problem design, an API document example is available [here](https://documenter.getpostman.com/view/424070/TzeWHomJ)\n\n## Getting started\nInstall [Docker](https://github.com/docker) + [Docker Compose](https://github.com/docker/compose) then run `docker-compose up`\n\nIf you want to run test, so open the new terminal then\n```\ndocker exec -it ctn_parking_lot_api_alpha sh\n\u003e NODE_ENV=test npx sequelize db:create\n\u003e NODE_ENV=test npx sequelize db:migrate\n\u003e npm run validate\n```\n\nTo access production ready container, then\n```\ndocker exec -it ctn_parking_lot_api sh\n```\n\n## Assumption\n\n![Scenario](./asset/parking-lot-scenario.png)\n\n```\n- Scenario: free public parking\n- Database: MySQL (DBMS)\n- Parking lot: support multiple parking lots\n  (must be ordered with `rank` property to find *nearest slot*)\n- Parking lot: not support multi-level\n- Parking lot: only 1 entry point for each parking lot\n- Concurrent processes: support by using database transaction feature to handle processes\n  (park the vehicle in the same time with different entry point)\n- Size:\n  - 3 vehicle sizes =\u003e small, medium, large\n  - 3 slot sizes =\u003e small, medium, large\n- Ticket and provided slot:\n  - Customer nice enough to park in the provided/assigned slot\n  - Vehicle must park at the same slot size only\n    (e.g. small vehicle park at small slot, medium vehicle park at the medium slot)\n  - The ticket system will find the *nearest slot*\n    (slot \"rank\" of each parking lot must be ordered by the distance between\n    slot itself and parking lot entry point)\n- Pricing strategy: free\n- Priority slot: none\n- Goal: *fast parking* so searching nearest slot must fast\n```\n\n## Algorithm\n\n```\nWe create *n* stacks for caching available slot ids\n(n = number of parking lot * number of slot size)\n\nWhen we park a car then\n1. get stacks\n2. check if each stack for specific slot size has available slot ids \n3. if there is available slot then remove the first slot from the stack\n\nWhen we exit a parking lot\n1. get the stack that related to our slot\n2. concat *our slot* with *available stack slots* then sort it\n3. update the stack with available-sorted-slots from 2)\n```\n\n## Model\n\n![ERD](./asset/parking-lot-ERD-orthogonal-layout.png)\n\n## API\n- [x] POST `/parking_lot` to create parking lot entity\n- [x] POST `/vehicle/park` to park a vehicle\n- [x] POST `/vehicle/exit` to release a vehicle slot (vehicle leave the parking slot)\n- [x] GET `/parking_lot/status` to get available slots of all parking slots\n- [x] GET `/ticket?slot_size_id=1` to get ticket by slot size (which contains registration plate numbers / slot numbers)\n\n## Future improvement\n- [x] Test: unit test\n- [x] Test: E2E test\n- [ ] Docker: `Dockerfile` for development\n- [ ] Security concerns e.g. not expose `id` field directly from database\n- [ ] Hide 5xx error\n- [ ] Encoding e.g. encode ticket id\n- [ ] Archive strategy on ticket table that continually increase\n- [ ] Load Balancer\n- [ ] [Kong API Gateway](https://github.com/Kong/kong#why-kong) for authentication, rate-limit, etc.\n- [ ] API: pagination support\n- [ ] CI: E2E test\n- [ ] CI: deploy coverage report to the service e.g. https://codecov.io/\n- [ ] Create example of Kubernetes deployment\n- [ ] Create example of Kubernetes service \n- [ ] Distributed storage, incase the data is too big\n- [ ] Limitation: where the limit of caching table\n- [ ] Database: add index to optimize fast search\n- [x] Example code of client side with [multi-stage builds](https://docs.docker.com/develop/develop-images/multistage-build/)\n\n## CMD\n\n```\nnpx sequelize db:create\nnpx sequelize migration:generate --name create-slot_size-table\nnpx sequelize migration:generate --name add-index-into-slot_size-table\nnpx sequelize migration:generate --name create-slot-table\nnpx sequelize db:seed:all\nnpx sequelize db:seed:undo:all\nnpx sequelize db:migrate\nnpx sequelize db:migrate:undo:all\n\nNODE_ENV=test npx sequelize db:create\nNODE_ENV=test npx sequelize db:migrate\n\ndocker build --tag parking-lot-api:1.0.0 .\ndocker build -f ./Dockerfile --tag parking-lot-api:1.0.0 .\ndocker run --name ctn_parking_lot_api parking-lot-api:1.0.0\ndocker exec -it ctn_parking_lot_api sh\ndocker exec -it ctn_mysql sh\n  mysql -u root -p\n\ndocker rm ctn_parking_lot_api\ndocker rm ctn_mysql\ndocker rm -f $(docker ps -qa)\ndocker rmi parking-lot-api:1.0.0\ndocker images \"parking-lot-api*\" --no-trunc --format \"{{.ID}}\"\ndocker rmi $(docker images \"parking-lot-api*\" --no-trunc --format \"{{.ID}}\")\ndocker image prune\n\ndocker-compose up\ndocker-compose up --build -d\n\ncd ./example\ndocker build -f ./Dockerfile --tag parking-lot-api-client:1.0.0 .\ndocker run -p 5008:5007 --name ctn_parking_lot_api_client parking-lot-api-client:1.0.0\n\ncurl localhost:5005\ncurl localhost:5006\ncurl localhost:5007\n```\n\n## Reference\n- [x] [System Design Interview Question: DESIGN A PARKING LOT - asked at Google, Facebook](https://www.youtube.com/watch?v=DSGsa0pu8-k)\n- [x] [Parking Lot - System Design Interview Question](https://www.youtube.com/watch?v=eV5Xh6jNfmU)\n- [ ] [Object Oriented Design Interview Question: Design a Car Parking Lot.](https://www.youtube.com/watch?v=2vtT6TBnOAM)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjojoee%2Fparking-lot-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjojoee%2Fparking-lot-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjojoee%2Fparking-lot-api/lists"}