Previous API Request Flow
- User opens
APP
and requests businessAPI
- Business
API
requests recommendation algorithm (with fault tolerance) - Recommendation algorithm requests internal service
- Internal service provides business interface data to recommendation algorithm (e.g., manually configured recommendations)
user -> API -> recommend service -> inner service
user <- API <- recommend service <- inner service
Originally built with Laravel
, both API
and inner service
faced a single-point bottleneck. While API
servers could scale horizontally, the inner service
remained single-node. This prompted the Go refactoring initiative.
Key Libraries Used
- Web Framework: https://github.com/gin-gonic/gin
- ORM: https://github.com/go-gorm/gorm
- Cache Middleware: https://github.com/gin-contrib/cache
- HTTP Testing: https://github.com/gavv/httpexpect
- Environment Loading: https://github.com/joho/godotenv
- Environment Parsing: https://github.com/joeshaw/envdecode
- Hot Reload: https://github.com/cosmtrek/air
Performance Comparison: PHP/Laravel vs Go/gin in Inner Service
Server Specs: 2-core CPU, 8GB RAM
top
%Cpu0
%Cpu1
KiB Mem : 7645252 total
Without API Caching
# PHP (port 8888)
ab -n 10000 -c 100 127.0.0.1:8888/api/v0/albums
Requests per second: 107.49 [#/sec] (mean)
# Go (port 9998)
ab -n 10000 -c 100 127.0.0.1:9998/api/v1/albums
Requests per second: 2719.86 [#/sec] (mean)
With Redis Caching
# PHP (OPcache enabled)
ab -n 10000 -c 100 127.0.0.1:8888/api/v0/albums
Requests per second: 124.89 [#/sec] (mean)
# Go
ab -n 10000 -c 100 127.0.0.1:9998/api/v0/albums
Requests per second: 4191.87 [#/sec] (mean)