Finally - redis to the rescue
Since last October, I have been working on porting the previous MyUniBus shuttle bus tracking service to a newer version using Cordova based hybrid environment (details on that will come in another entry, hopefully soon). The backend resides on a digitalocean droplet (the cheapest!) with only 512MB RAM, which is sufficient in most test cases. When I say in most test cases, it actually means - in real world applications, it may reach its limit.
During the last weeks, I noticed this when I run htop - the load averages exceeded 1, and memory will go as near as 450MB (usable is stated as 490MB). From previous MyUniBus experience, this is no good. MySQL can be picky on the available RAM, and worst - server slows down as soon as it runs low on RAM. The culprit can be easily spotted - it has to be MySQL since the loggers will update the server, which immediately writes into the database. The backend is written (ahem, not that elegantly) in a way that each request from the mobile app will cause the backend to query the MySQL server once. Imagine now - I will not have the test case (means 3-5 connections simultanously) scenario, and this is where the server runs low.
I have read abotu Redis way back to the MyUniBus time. Therefore, I think I no longer have an excuse to wait. Quickly, I shot up a few webpages on how Redis can be installed and used along with PHP, and there I go. The simplified sequences are as follows:
- install redis-server and php5-redis via apt-get
- configure redis by modifying the configuration file
- restart apache
- create a script that updates the redis cache from the MySQL database
- create a cronjob to automate that
- swap the backend API to the redis-enabled API
and immediately I noticed the drop of load average from 3-8 (!) to 0.3-0.4. I almost going to blame myself for being lazy, but seriously - just a simple diversion from MySQL to Redis helped improved the responsiveness of the tracker app.
Note-to-self: Quick and dirty may be good, but proper planning and tools will ultimately still be the best way out.