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.

Taming the resource tiger

353 views

Published on

php streams sockets networking physics

Published in: Internet
  • Be the first to comment

Taming the resource tiger

  1. 1. Taming the resource tiger You cannot hide from Physics!
  2. 2. Mass How much matter is in an object
  3. 3. Data Storage • Hard Disk Drive - HDD • Magnetizes a thin film of ferromagnetic material on a disk • Reads it with a magnetic head on an actuator arm • Solid State Drive – SSD • Uses integrated circuit assemblies as memory to store data persistently • No moving parts
  4. 4. Areal Storage Density • SSD • 2.8 Tbit/in2 • HDD • 1.5 Tbit/in2 Terabits per square inch – numbers as of 2016 (see Wikipedia, our materials are improving)
  5. 5. When hard drives go bad
  6. 6. Streams: Computing Concept Definitions • Idea originating in 1950’s • Standard way to get Input and Output • A source or sink of data Who uses them • C – stdin, stderr, stdout • C++ iostream • Perl IO • Python io • Java • C#
  7. 7. What is a Stream? • Access input and output generically • Can write and read linearly • May or may not be seekable • Comes in chunks of data
  8. 8. Why do I care about streams? • They are created to handle massive amounts of data • Assume all files are too large to load into memory • If this means checking size before load, do it • If this means always treating a file as very large, do it • PHP streams were meant for this!
  9. 9. What does a this have to do with PHP?
  10. 10. The chat that worked for 3 days…
  11. 11. What uses streams in PHP? • EVERYTHING • include/require _once • stream functions • file system functions • many other extensions
  12. 12. ALL IO Attach Context Stream Transport Stream Filter Stream Wrapper How PHP Streams Work
  13. 13. Using Streams
  14. 14. You can also do logic on the fly!
  15. 15. What are Filters? • Performs operations on stream data • Can be prepended or appended (even on the fly) • Can be attached to read or write • When a filter is added for read and write, two instances of the filter are created.
  16. 16. Using Filters
  17. 17. Things to watch for! • Data has an input and output state • When reading in chunks, you may need to cache in between reads to make filters useful • Use the right tool for the job
  18. 18. Throw away your assumptions except for: There will be Terabytes of Cat Gifs!!
  19. 19. Dimension Both an object’s size and mathematical space
  20. 20. Random Access Memory (RAM) • The CPU uses RAM to work • It randomly shoves data inside and pulls data back out • RAM is faster then SSD and HDD • It’s also more expensive
  21. 21. Out of Memory
  22. 22. There are two reasons you’ll see that error • Recursion recursion recursion recursion • Solution: install xdebug and get your stacktrace • Loading too much data into memory • Solution: manage your memory
  23. 23. Inherently PHP hides this problem • Share nothing architecture • Extensions with C libraries that hide memory consumption • FastCGI/CGI blows away processes, restoring memory • Max child and other Apache settings blow away children, restoring memory
  24. 24. How do I fix it!
  25. 25. Halp, I can’t upload!!
  26. 26. Arrays are evil • There are other ways to store data that are more efficient • They should be used for small amounts of data • No matter how hard you try, there is C overhead
  27. 27. Process with the appropriate tools • Load data into the appropriate place for processing • Hint – arrays are IN MEMORY – that is generally not an appropriate place for processing • Datastores are meant for storing and retrieving data, use them
  28. 28. Select * from table
  29. 29. Use the iteration, Luke • Lazy fetching exists for database fetching – use it! • Always page (window) your result sets from the database – ALWAYS • Use filters or generators to format or alter results on the fly
  30. 30. The N+1 problem • In simple terms, nested loops • Don’t distance yourself too much from your datastore • Collapse into one or two queries instead
  31. 31. Throw away all your assumptions except:
  32. 32. Speed The rate at which an object covers distance
  33. 33. How does a CPU work?
  34. 34. CPU limitations • Transmission delays • Heat • Both are materials limitations • http://www.mooreslaw.org/
  35. 35. Why I no longer overclock
  36. 36. What does this have to do with PHP? • You are limited by the CPU your site is deployed upon. • Yes even in a cloud – there are still physical systems running your stuff • Yes even in a VM – there are still physical systems running your stuff • Follow good programming habits • PROFILE
  37. 37. Good programming habits • Turn on opcache in production! • Keep your code error AND WARNING free • Watch complex logic in loops • Short circuit the loop • Rewrite to do the logic on the entire set in one step • Calculate values only once • On small arrays use array_walk • On large arrays use generators/iterators • Use isset instead of in_array if possible • Profile to find the place to rewrite for slow code issues
  38. 38. Profiling Options Free • Xdebug • Xhprof • Uprofiler Paid • NewRelic • AppDynamics • Blackfire • Tideways • Tracelytics
  39. 39. Distribute the load • Perfect for heavy processing for some type of data • Queue code that requires heavy processing but not immediate viewing • Design your UX so you can inform users of completed jobs • Cache complex work items
  40. 40. Pick your system • php-resque • Gearman • Beanstalkd • IronMQ • RabbitMQ • ZeroMQ • AmazonSQS • Just visit http://queues.io
  41. 41. Job queuing and 10K page pdfs
  42. 42. Keep your CPU happy • Offload processing • Use a queue
  43. 43. Velocity Speed + Direction
  44. 44. Networking 101 • IP – forwards packets of data based on a destination address • TCP – verifies the correct delivery of data from client to server with error and lost data correction • Network Sockets – subroutines that provide TCP/IP (and UDP and some other support) on most systems
  45. 45. Packet of Data
  46. 46. Speed in the series of tubes • Bandwidth – size of your pipe • Latency – length of your pipe including size changes • Jitter – air bubbles in your pipe
  47. 47. Network Socket Types • Stream • Connection oriented (tcp) • Datagram • Connectionless (udp) • Raw • Low level protocols
  48. 48. Definitions • Socket • Bidirectional network stream that speaks a protocol • Transport • Tells a network stream how to communicate • Wrapper • Tells a stream how to handle specific protocols and encodings
  49. 49. Using Sockets
  50. 50. What does this have to do with PHP? • APIs fail • APIs go byby • AWS goes down • Or loses network connection to a specific area • Or otherwise fails
  51. 51. What do you mean we can’t write files?
  52. 52. Prepare for failure • Handle timeouts • Handle failures • Abstract enough to replace systems if necessary, but only as much as necessary • If you’re not paying for it, don’t base your business model on it
  53. 53. Checklist • Cultivate good coding habits • Try not to loop logic or processing • Don’t be afraid to offload work to other systems or services • Assume every file is huge • Assume there are 1 million rows in your DB table • Assume that every network request is slow or going to fail • Profile to find code bottlenecks, DON’T assume you know the bottleneck • Wrap 3rd party tools enough to deal with downtime or retirement of apis
  54. 54. SHHHHHH • Plotting • https://github.com/phplang/streams2/wiki • PHP is always improving!
  55. 55. About Me  http://emsmith.net  auroraeosrose@gmail.com  twitter - @auroraeosrose  IRC – freenode – auroraeosrose  #phpmentoring  https://joind.in/talk/18dd4

×