Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Scaling Yii2 Application

1,806 views

Published on

My share session at YiiConf 26th August 2017 about scaling Yii2 app.

Published in: Software

Scaling Yii2 Application

  1. 1. Scaling Out Yii2 App @petrabarus YiiConf Indonesia 26 Ags 2017
  2. 2. Hi Everyone! I am Petra Novandi Now serves as Chief Technology Officer at UrbanIndo.com - Contact me at Email: petra.barus@gmail.com Twitter: @petrabarus
  3. 3. UrbanIndo & Yii - Use Yii 1.1 at 2011 - Migrate to Yii 2.0 at 2015 - Contribute to Yii core - #7435 #8933 #9249 #9277 - Contribute to Yii2 plugins - yii2-queue, yii2-s3-cache, yii2-dynamodb
  4. 4. WHAT’S THIS ABOUT?
  5. 5. Let me tell you a common story... Dewi is a software engineer. She had a great idea for app! So Dewi build a new app from scratch.
  6. 6. MySQL Yii App PHP Apache Pictures Assets Libraries Logs Scripts Files Like a lot other engineers, Dewi put all the stuffs in one server.
  7. 7. AND THE TRAFFIC GOES UP UP UP! AWESOME!!
  8. 8. BUT ALL HELL BREAK LOOSE! SOUNDS FAMILIAR?
  9. 9. This is story about Scaling
  10. 10. Scale Up (Vertical) Scale Out (Horizontal)
  11. 11. Continuous Availability/Redundancy No single point of failure Cost/Performance Flexibility Not being locked by the hardware WHY SCALE OUT? Continuous Upgrade Without downtime Geographical Distribution Access to nearest node
  12. 12. Multi-servers environment concerns in Yii2 Database Assets Load Balancing File System Session Deployment And many more… Logging, Cache, Queue, Batch Processings. Let’s discuss that on another occasion.. I don’t have enough time.. :(
  13. 13. LOAD BALANCING DATABASE SESSION FILE SYSTEM ASSETS DEPLOYMENT
  14. 14. Yii2 Apache Yii2 Apache Yii2 Apache Yii2 Apache Yii2 Apache Yii2 Apache Yii2 Apache Yii2 Apache Load Balancer Load balancer will forward requests to instances
  15. 15. Sticky Sessions Requests from same client will be forwarded to same instance Load Balancer Session Types Non-Sticky Sessions Requests from same client can be forwarded to different instances.
  16. 16. Open Source Load Balancer Technologies
  17. 17. Example Config in NGINX
  18. 18. » No need a lot of configuration in the Yii side » Although you might want to have concerns in ⋄ hostInfo in the yiiwebUrlManager ⋄ ipHeaders in yiiwebRequest (upcoming Yii 2.0.13), different proxies can have different headers
  19. 19. LOAD BALANCING DATABASE SESSION FILE SYSTEM ASSETS DEPLOYMENT
  20. 20. Yii2 Apache Yii2 Apache Yii2 Apache Yii2 Apache Yii2 Apache Yii2 Apache Yii2 Apache Yii2 Apache Database All instances access the same database instance(s). Make them more stateless
  21. 21. Simple Change in Configuration
  22. 22. DATABASE CAN BE SCALED TOO!
  23. 23. Master-Master Master-Slave or both
  24. 24. Yii2 Natively Support Db Replication Or use other tech like ProxySQL
  25. 25. LOAD BALANCING DATABASE SESSION FILE SYSTEM ASSETS DEPLOYMENT
  26. 26. ❏ Since PHP stores session in file system we need to store it outside the instance ❏ Similar like database ❏ Use classes like yiimongodbSession, yiiredisSession, yiiwebDbSession, yiiwebCacheSession,
  27. 27. Sample Config
  28. 28. Some Considerations » Better use key-value storage type that stores in memory instead of files ⋄ E.g. Redis, memcached ⋄ Or SQL that support memory storage ⋄ MySQL’s MEMORY storage engine » Execute Session::gcSession() using cron.
  29. 29. LOAD BALANCING DATABASE SESSION FILE SYSTEM ASSETS DEPLOYMENT
  30. 30. Avoid writing and storing data to local file systems Every app instance has to identical
  31. 31. file_put_contents(‘/tmp/text1.txt’, ‘blablabla’) Instance A Instance B file_get_contents(‘/tmp/text1.txt’)
  32. 32. Write file to centralized storage » FTP » Amazon S3 » Azure » DropBox Use filesystem abstraction! » GridFs » Rackspace » Copy.com Recommended library: league/flysystem ⇨ creocoder/yii2-flysystem
  33. 33. Sample Config
  34. 34. Yii::$app->fs->write(‘text1.txt’, blablabla’) Instance A Instance B Yii::$app->fs->read(‘text1.txt’) Central Storage
  35. 35. LOAD BALANCING DATABASE SESSION FILE SYSTEM ASSETS DEPLOYMENT
  36. 36. Asset Management in Single Server » Hash is not deterministic due to filemtime() (yiiwebAssetManager::hash) » Different server can have different filemtime()
  37. 37. Multiple Server /var/www/web/assets/1234567890/script.js /var/www/web/assets/ABCDEFGHIJ/script.js
  38. 38. Configuring Asset Hash Generation
  39. 39. Multiple Server With Consistent Hash /var/www/web/assets/1234567890/script.js /var/www/web/assets/1234567890/script.js
  40. 40. A Much Better Way 1. Build and compress the assets in build server 2. Upload assets to central storage (e.g. S3) 3. Put a CDN in front of the storage
  41. 41. BUILD COMPR ESS UPLOA D DEPLO Y SOURCE REPO BUILD SERVER CENTRAL STORAGE (S3) APP SERVERS HTML Requests JS/CSS Requests CDN
  42. 42. Pros 1. No need additional local process to generate assets (Reduced request time) 2. Optimized assets 3. Served through CDN Cons 1. Need build server 2. Need central storage 3. Need CDN 4. Build can be slow
  43. 43. LOAD BALANCING DATABASE SESSION FILE SYSTEM ASSETS DEPLOYMENT
  44. 44. Deployment Can Be As Simple As BUT a lot of things can go wrong with this
  45. 45. Deploy Command Things can go wrong, e.g. failure on deploy or bug after deploying, need rollback
  46. 46. Deployment & Automation Technologies
  47. 47. Some Considerations » Composer installation after code pull » Asset installation, build, compress after code pull » Load balancer request draining before code symlink » Local cache clean up » JIT warming up (if using HHVM) » Health check for load balancer » Blue-green deployment
  48. 48. NOW FOR CONCLUSION!
  49. 49. ❏ Scaling Out has more better merits in common use cases ❏ App has to be Stateless for scaling out ❏ Data storages has to be Centralized
  50. 50. THANKS! You can find me at » @petrabarus » petra.barus@gmail.com

×