Setup Multiserver Rest API - aalfiann/reSlim GitHub Wiki
How to Setup Multiserver Rest API
One of benefit using Rest API is able to run multiserver to scale horizontally purpose.
Limitation
- Upload File is use dynamic url, so in frontend UI you should set which server is going to become image hosting.
- FlexibleConfig is using SQLite, which is can not be shared using PDO. The reason we use this because there is probability to set different config in each server.
Use case
Assume that we have two server and all server only use single same database.
- Server A : IP 111.111.111.001 >> server1.yourdomain.com/api
- Server B : IP 111.111.111.002 >> server2.yourdomain.com/api
A. If You Don't have Redis server
Cache transfer is available since reSlim version 1.17.0
1. Edit the config.php of each servers:
Example Config in Server A
$config['reslim']['authcache'] = true;
$config['reslim']['simplecache'] = true;
$config['reslim']['universalcache'] = true;
$config['cache']['transfer'] = true;
$config['cache']['secretkey'] = 'abc123def';
$config['cache']['listenfrom'] = ['http://server2.yourdomain.com/api'];
Example Config in Server B
$config['reslim']['authcache'] = true;
$config['reslim']['simplecache'] = true;
$config['reslim']['universalcache'] = true;
$config['cache']['transfer'] = true;
$config['cache']['secretkey'] = 'abc123def';
$config['cache']['listenfrom'] = ['http://server1.yourdomain.com/api'];
Explanation
- $config['cache']['secretkey'] on server A and server B have to same value, because this will work like a password to protect your data transfer.
- Don't ever input same domain in $config['cache']['listenfrom'], You must input your another server to listen the incoming data transfer.
Note:
- Don't worry about the blocking php, because transfer is using parallel request (non blocking).
- If you have multiserver more than 4 servers. Then you should go with Redis because filebased cache transfer will take much bandwidth and will increase the CPU usage as this will do transfer to many servers.
- SimpleCache will not work if your server is on subfolder,
Example:- http://yourdomain.com/server1/api >> This won't work
- http://yourdomain.com/server2/api >> This won't work
- http://server1.yourdomain.com/api >> WORK
- http://server2.yourdomain.com/api >> WORK
2. Done
Now your rest api is able to run multiserver. You can set through loadbalancer proxy or just create random connection to rest api url from frontend UI.
B. If You have Redis server
1. We support Redis since reSlim version 1.15.0
2. Assume that you have online Redis server on IP 111.111.111.001
Example Config in Server A
$config['reslim']['authcache'] = true;
$config['reslim']['simplecache'] = true;
$config['reslim']['universalcache'] = true;
$config['cache']['transfer'] = false;
$config['redis']['enable'] = true;
$config['redis']['parameter'] = ['tcp://111.111.111.001:6379'];
$config['redis']['option'] = [];
Example Config in Server B
$config['reslim']['authcache'] = true;
$config['reslim']['simplecache'] = true;
$config['reslim']['universalcache'] = true;
$config['cache']['transfer'] = false;
$config['redis']['enable'] = true;
$config['redis']['parameter'] = ['tcp://111.111.111.001:6379'];
$config['redis']['option'] = [];
Explanation
- Set Redis enable to true to activate cache using Redis.
- Make sure the Redis parameter using IP address which is already online.
- We use predis library, for more detail about redis parameter and option, you can read at here >> https://github.com/nrk/predis/wiki
3. Done
Now your rest api is able to run multiserver. You can set through loadbalancer proxy or just create random connection to rest api url from frontend UI.
Tips
- Multiserver will stronger if you setup master to master or using clustered database.
- Cache for multiserver is recommended to use Redis because it is very fast and save bandwidth.