Featured image of post Today Completed Refactoring All Internal APIs from PHP to Go

Today Completed Refactoring All Internal APIs from PHP to Go

Refactoring is satisfying; continuous refactoring brings continuous satisfaction.

Previous API Request Flow

  1. User opens APP and requests business API
  2. Business API requests recommendation algorithm (with fault tolerance)
  3. Recommendation algorithm requests internal service
  4. 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

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)