This is a simple note taking application that is built using microservices architecture. The application consists of two services, the notes-service
and the users-service
. The notes-service
is responsible for managing notes and the users-service
is responsible for managing users and authentication. The notes-service
and the users-service
communicate with each other using REST APIs And message broker (RabbitMQ) , every Microservice has its own database and two services are connected to Nginx as a api gateway for routing the requests.
- Clone the repository
- Run
start.sh
script to start the application the flow of script- Check if docker is installed or not
- Check if docker-compose is installed or not
- Start the application using docker-compose
- Check if the application is started successfully or not by check status code of end point of nginx getway
- docker compose will start the following services
users-service-db
: MySQL database for theusers-service
notes-service-db
: MySQL database for thenotes-service
auth-service
: Laravel service for user management and authenticationnotes-service
: Laravel service for notes managementnotes-service-worker
: Laravel worker service for handling delete user eventnginx
: Nginx service as an API gatewayrabbitmq
: RabbitMQ service as a message broker
- path: ApiGetway + Microservice + Route
- Create User : APIGetway + '/auth/api/register' [POST]
- Login User : APIGetway + '/auth/api/login' [POST]
- Check Authenticated User : APIGetway + '/auth/api/check' [GET]
- Delete User : APIGetway + '/auth/api/delete' [DELETE]
- Create Note : APIGetway + '/note/api/note' [POST]
- Get All Notes : APIGetway + '/note/api/note' [GET]
- Get Single Note : APIGetway + '/note/api/note/{id}' [GET]
- Update Note : APIGetway + '/note/api/note/{id}' [PUT]
- Delete Note : APIGetway + '/note/api/note/{id}' [DELETE]
- User registration , login and authentication using JWT
- Create, Read, Update and Delete notes
- Get all notes of a user
- Get a single note of a user
- User registers throw the
auth-service
Service and gets a JWT token - User Creates a note throw the
notes-service
Service and sends the JWT token in the header - The
notes-service
Service validates the JWT token Throw send REST API request to theauth-service
Service - The
auth-service
Service validates the JWT token and sends the response to thenotes-service
Service - The
notes-service
Service creates the note in the database and returns the response to the user - If User deletes his account throw the
auth-service
Service,auth-service
Service sends an event to thenotes-service
Service to delete all notes of the user throwRabbitMQ
message broker
Why we don't use a message broker to communicate between the auth-service
and the notes-service
in authentication process ?
- Because
note-service
needs to validate the JWT token in every request and it's not a good idea to use a message broker in this case because it will make the process slower and more complex becausenote-service
want to response to the user as fast as possible so if we use a message broker it will make the process slower because thenote-service
will send event contains the JWT token and identification number or session id to identify current user and will waint time untill theauth-service
validate the JWT token and send the response to thenote-service
throw the message broker and then thenote-service
must in this case to validate all events that it receives from theauth-service
throw the message broker and check which event is related to the current user and then it will respond to the user so it's not a good idea to use a message broker in this case,let us take example to worst case we have 4 requested in same time so php application server like (fpm-php or FrankenPHP ) open 4 threads to handle the requests and the 4 threads will send 4 events to the message broker and the message broker will send the 4 events to the
auth-service
and theauth-service
will validate the JWT token and send the response to the message broker and the message broker will send the response to thenote-service
and thenote-service
will validate the response and send the response to the user so in this case the process will be slower and more complex and thenote-service
will validate the response 4 times to check which response is related to the current user so it's not a good idea to use a message broker in this case in this case service or instance to service is stateful because thenote-service
must validate the response and check which response is related to the current user so it's not a good idea to use a message broker in this case
Why we use a message broker to communicate between the auth-service
and the notes-service
in delete user process ?
- Because
auth-service
wants to delete all notes of the user when the user deletes his account so it's a good idea to use a message broker in this case because theauth-service
doesn't need to wait for the response of thenotes-service
to delete the user because theauth-service
doesn't need to know if thenotes-service
deleted the user notes or not so it's a good idea to use a message broker in this case because theauth-service
can send the event to the message broker and the message broker will send the event to thenotes-service
and thenotes-service
will delete the user notes and thenotes-service
doesn't need to send a response to theauth-service
so it's a good idea to use a message broker in this case because theauth-service
doesn't need to wait for the response of thenotes-service
to delete the user notes so in this case service or instance to service is stateless because theauth-service
doesn't need to wait for the response of thenotes-service
to delete the user notes so it's a good idea to use a message broker in this case
We make Feature tests for the auth-service
and the notes-service
to test the API endpoints and the authentication process and the notes management process
- Register User
- Login User
- Check Authenticated User
- Delete User
How to run the tests
- Go to the
auth-service
directory - Run
php artisan test
- Create Note
- Get All Notes
- Get Single Note
- Update Note
- Delete Note
How to run the tests
- Go to the
notes-service
directory - Run
php artisan test
i make a Github Action workflow to run the tests of the auth-service
and the notes-service
every time we pull request to the main branch , i make separate testing pipline workflow for the auth-service
and the notes-service
to run the tests of the auth-service
and the notes-service
separately based on the changes that we made in the code of the auth-service
or the notes-service
so we can know which service has the issue in the code and we can fix it easily
- Add more features to the application like sharing notes between users
- Add end to end testing to the application whcih test the application as a whole in every new release
- Add more microservices to the application like
files-service
to manage files andnotifications-service
to manage notifications - Scale the application by adding more instances of the services and use a load balancer to distribute the requests between the instances
- Use Kubernetes to manage the application and scale the application and make it more reliable and secure
- Use Redis as a cache to cache the requests and make the application faster
- Laravel
- MySQL
- RabbitMQ
- Nginx
- Docker
- Github Actions