auto scalar application for a rest API.
At started off with following approach from the base document shared.
- Declare types for the AppStatus and Replicas
- call /app/status to get CPU and replica count.
- calculate the replica new count in a way that CPU <.80
- inc replica will dec CPU and dec replica will inc CPU.
- call /app/replicas to update the replica count.
- repeat step 2 to 4 until CPU as much as close to .80 for every given time interval.
For point 1, based on the given sample response and constraint data, I declared the constants and defined types for AppStatus and Replicas.
For point 2, Started with http package but moved to use resty package as it was easier to use and code is more readable.
For point 3, replica is inversely proportional to CPU. calculated replicas count as factor of current replicas with current/target CPU.
For point 4, used resty package to update the replica count with Replicas.
Improvements:
- Don't update the same replica count - Done
- Refactor the code to make it more readable - Done
- Exit gracefully on exiting the program - Done
- Add more configuration options like env based config. - Done
- Add more logging and error handling. - Done
- Add unit tests - done
- update code docs. - done
- containerize the application. - done
To build the application, run the following command to generate the binary in the name of auto-scalar.
go build -o auto-scalar .
To run this application, run the following command.
go run .
To run the unit tests, run the following command.
go test .
Container
To build the container, run the following command.
docker build -t auto-scalar .
To Run the container, run the following command.
docker run -e STATUS_API_URL=http://host.docker.internal:8123/app/status -e REPLICAS_API_URL=http://host.docker.internal:8123/app/replicas -e ENVIRONMENT=prod auto-scalar
Note: update the STATUS_API_URL and REPLICAS_API_URL with respective endpoints based on the platfrom. Given example is for the MacOS
Potentially this application can be further iterated to make it more organized and also depending on the deployment platform and service discovery.