SlideShare a Scribd company logo
1 of 55
Taming the
resource tiger
You cannot hide from Physics!
Mass
How much matter is in an object
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
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)
When hard drives go bad
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#
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
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!
What does a this have to do with PHP?
The chat that worked for 3 days…
What uses streams in PHP?
• EVERYTHING
• include/require _once
• stream functions
• file system functions
• many other extensions
ALL IO
Attach
Context
Stream
Transport
Stream
Filter
Stream
Wrapper
How PHP Streams Work
Using Streams
You can also do logic on the fly!
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.
Using Filters
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
Throw away your assumptions except for:
There will be Terabytes of Cat Gifs!!
Dimension
Both an object’s size and mathematical space
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
Out of Memory
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
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
How do I fix it!
Halp, I can’t upload!!
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
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
Select * from table
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
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
Throw away all your assumptions except:
Speed
The rate at which an object covers distance
How does a CPU work?
CPU limitations
• Transmission delays
• Heat
• Both are materials limitations
• http://www.mooreslaw.org/
Why I no longer overclock
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
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
Profiling Options
Free
• Xdebug
• Xhprof
• Uprofiler
Paid
• NewRelic
• AppDynamics
• Blackfire
• Tideways
• Tracelytics
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
Pick your system
• php-resque
• Gearman
• Beanstalkd
• IronMQ
• RabbitMQ
• ZeroMQ
• AmazonSQS
• Just visit http://queues.io
Job queuing and 10K page pdfs
Keep your CPU happy
• Offload processing
• Use a queue
Velocity
Speed + Direction
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
Packet of Data
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
Network Socket Types
• Stream
• Connection oriented (tcp)
• Datagram
• Connectionless (udp)
• Raw
• Low level protocols
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
Using
Sockets
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
What do you mean we can’t write files?
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
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
SHHHHHH
• Plotting
• https://github.com/phplang/streams2/wiki
• PHP is always improving!
About Me
 http://emsmith.net
 auroraeosrose@gmail.com
 twitter - @auroraeosrose
 IRC – freenode – auroraeosrose
 #phpmentoring
 https://joind.in/talk/18dd4

More Related Content

What's hot

Avro - More Than Just a Serialization Framework - CHUG - 20120416
Avro - More Than Just a Serialization Framework - CHUG - 20120416Avro - More Than Just a Serialization Framework - CHUG - 20120416
Avro - More Than Just a Serialization Framework - CHUG - 20120416Chicago Hadoop Users Group
 
Thrift vs Protocol Buffers vs Avro - Biased Comparison
Thrift vs Protocol Buffers vs Avro - Biased ComparisonThrift vs Protocol Buffers vs Avro - Biased Comparison
Thrift vs Protocol Buffers vs Avro - Biased ComparisonIgor Anishchenko
 
Modern Black Mages Fighting in the Real World
Modern Black Mages Fighting in the Real WorldModern Black Mages Fighting in the Real World
Modern Black Mages Fighting in the Real WorldSATOSHI TAGOMORI
 
Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.Junichi Ishida
 
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)Tim Bunce
 
Serialization (Avro, Message Pack, Kryo)
Serialization (Avro, Message Pack, Kryo)Serialization (Avro, Message Pack, Kryo)
Serialization (Avro, Message Pack, Kryo)오석 한
 
Serialization and performance by Sergey Morenets
Serialization and performance by Sergey MorenetsSerialization and performance by Sergey Morenets
Serialization and performance by Sergey MorenetsAlex Tumanoff
 
Clojure in real life 17.10.2014
Clojure in real life 17.10.2014Clojure in real life 17.10.2014
Clojure in real life 17.10.2014Metosin Oy
 
Asynchronous I/O in Python 3
Asynchronous I/O in Python 3Asynchronous I/O in Python 3
Asynchronous I/O in Python 3Feihong Hsu
 
Concurrency & Parallel Programming
Concurrency & Parallel ProgrammingConcurrency & Parallel Programming
Concurrency & Parallel ProgrammingRamazan AYYILDIZ
 
The Parenscript Common Lisp to JavaScript compiler
The Parenscript Common Lisp to JavaScript compilerThe Parenscript Common Lisp to JavaScript compiler
The Parenscript Common Lisp to JavaScript compilerVladimir Sedach
 
TypeProf for IDE: Enrich Development Experience without Annotations
TypeProf for IDE: Enrich Development Experience without AnnotationsTypeProf for IDE: Enrich Development Experience without Annotations
TypeProf for IDE: Enrich Development Experience without Annotationsmametter
 
Experience protocol buffer on android
Experience protocol buffer on androidExperience protocol buffer on android
Experience protocol buffer on androidRichard Chang
 

What's hot (19)

System Programming and Administration
System Programming and AdministrationSystem Programming and Administration
System Programming and Administration
 
Avro - More Than Just a Serialization Framework - CHUG - 20120416
Avro - More Than Just a Serialization Framework - CHUG - 20120416Avro - More Than Just a Serialization Framework - CHUG - 20120416
Avro - More Than Just a Serialization Framework - CHUG - 20120416
 
Thrift vs Protocol Buffers vs Avro - Biased Comparison
Thrift vs Protocol Buffers vs Avro - Biased ComparisonThrift vs Protocol Buffers vs Avro - Biased Comparison
Thrift vs Protocol Buffers vs Avro - Biased Comparison
 
Avro
AvroAvro
Avro
 
Modern Black Mages Fighting in the Real World
Modern Black Mages Fighting in the Real WorldModern Black Mages Fighting in the Real World
Modern Black Mages Fighting in the Real World
 
Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.
 
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
 
Serialization (Avro, Message Pack, Kryo)
Serialization (Avro, Message Pack, Kryo)Serialization (Avro, Message Pack, Kryo)
Serialization (Avro, Message Pack, Kryo)
 
Serialization and performance by Sergey Morenets
Serialization and performance by Sergey MorenetsSerialization and performance by Sergey Morenets
Serialization and performance by Sergey Morenets
 
Clojure in real life 17.10.2014
Clojure in real life 17.10.2014Clojure in real life 17.10.2014
Clojure in real life 17.10.2014
 
Avro intro
Avro introAvro intro
Avro intro
 
3 avro hug-2010-07-21
3 avro hug-2010-07-213 avro hug-2010-07-21
3 avro hug-2010-07-21
 
CBOR - The Better JSON
CBOR - The Better JSONCBOR - The Better JSON
CBOR - The Better JSON
 
Asynchronous I/O in Python 3
Asynchronous I/O in Python 3Asynchronous I/O in Python 3
Asynchronous I/O in Python 3
 
Php
PhpPhp
Php
 
Concurrency & Parallel Programming
Concurrency & Parallel ProgrammingConcurrency & Parallel Programming
Concurrency & Parallel Programming
 
The Parenscript Common Lisp to JavaScript compiler
The Parenscript Common Lisp to JavaScript compilerThe Parenscript Common Lisp to JavaScript compiler
The Parenscript Common Lisp to JavaScript compiler
 
TypeProf for IDE: Enrich Development Experience without Annotations
TypeProf for IDE: Enrich Development Experience without AnnotationsTypeProf for IDE: Enrich Development Experience without Annotations
TypeProf for IDE: Enrich Development Experience without Annotations
 
Experience protocol buffer on android
Experience protocol buffer on androidExperience protocol buffer on android
Experience protocol buffer on android
 

Viewers also liked

Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)James Titcumb
 
Dip Your Toes in the Sea of Security
Dip Your Toes in the Sea of SecurityDip Your Toes in the Sea of Security
Dip Your Toes in the Sea of SecurityJames Titcumb
 
SunshinePHP 2017 - Making the most out of MySQL
SunshinePHP 2017 - Making the most out of MySQLSunshinePHP 2017 - Making the most out of MySQL
SunshinePHP 2017 - Making the most out of MySQLGabriela Ferrara
 
A World Without PHP
A World Without PHPA World Without PHP
A World Without PHPBen Marks
 
My app is secure... I think
My app is secure... I thinkMy app is secure... I think
My app is secure... I thinkWim Godden
 
JWT - To authentication and beyond!
JWT - To authentication and beyond!JWT - To authentication and beyond!
JWT - To authentication and beyond!Luís Cobucci
 
Amp your site: An intro to accelerated mobile pages
Amp your site: An intro to accelerated mobile pagesAmp your site: An intro to accelerated mobile pages
Amp your site: An intro to accelerated mobile pagesRobert McFrazier
 
From Docker to Production - SunshinePHP 2017
From Docker to Production - SunshinePHP 2017From Docker to Production - SunshinePHP 2017
From Docker to Production - SunshinePHP 2017Chris Tankersley
 
PHP UK 2017 - Don't Lose Sleep - Secure Your REST
PHP UK 2017 - Don't Lose Sleep - Secure Your RESTPHP UK 2017 - Don't Lose Sleep - Secure Your REST
PHP UK 2017 - Don't Lose Sleep - Secure Your RESTAdam Englander
 
Docker for Developers - Sunshine PHP
Docker for Developers - Sunshine PHPDocker for Developers - Sunshine PHP
Docker for Developers - Sunshine PHPChris Tankersley
 
WordPress for the modern PHP developer
WordPress for the modern PHP developerWordPress for the modern PHP developer
WordPress for the modern PHP developerChris Sherry
 
Building Big on the Web
Building Big on the WebBuilding Big on the Web
Building Big on the WebCal Henderson
 
Scalable Web Architectures: Common Patterns and Approaches
Scalable Web Architectures: Common Patterns and ApproachesScalable Web Architectures: Common Patterns and Approaches
Scalable Web Architectures: Common Patterns and Approachesadunne
 
PHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return TypesPHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return TypesEric Poe
 
flickr's architecture & php
flickr's architecture & php flickr's architecture & php
flickr's architecture & php coolpics
 
PHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodePHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodeSWIFTotter Solutions
 
Practical Performance Tips and Tricks to Make Your HTML/JavaScript Apps Faster
Practical Performance Tips and Tricks to Make Your HTML/JavaScript Apps FasterPractical Performance Tips and Tricks to Make Your HTML/JavaScript Apps Faster
Practical Performance Tips and Tricks to Make Your HTML/JavaScript Apps FasterDoris Chen
 
Giving birth to an ElePHPant
Giving birth to an ElePHPantGiving birth to an ElePHPant
Giving birth to an ElePHPantMark Baker
 
Services Oriented Architecture with PHP and MySQL
Services Oriented Architecture with PHP and MySQLServices Oriented Architecture with PHP and MySQL
Services Oriented Architecture with PHP and MySQLJoe Stump
 

Viewers also liked (20)

Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
 
Dip Your Toes in the Sea of Security
Dip Your Toes in the Sea of SecurityDip Your Toes in the Sea of Security
Dip Your Toes in the Sea of Security
 
SunshinePHP 2017 - Making the most out of MySQL
SunshinePHP 2017 - Making the most out of MySQLSunshinePHP 2017 - Making the most out of MySQL
SunshinePHP 2017 - Making the most out of MySQL
 
A World Without PHP
A World Without PHPA World Without PHP
A World Without PHP
 
My app is secure... I think
My app is secure... I thinkMy app is secure... I think
My app is secure... I think
 
JWT - To authentication and beyond!
JWT - To authentication and beyond!JWT - To authentication and beyond!
JWT - To authentication and beyond!
 
Amp your site: An intro to accelerated mobile pages
Amp your site: An intro to accelerated mobile pagesAmp your site: An intro to accelerated mobile pages
Amp your site: An intro to accelerated mobile pages
 
From Docker to Production - SunshinePHP 2017
From Docker to Production - SunshinePHP 2017From Docker to Production - SunshinePHP 2017
From Docker to Production - SunshinePHP 2017
 
PHP UK 2017 - Don't Lose Sleep - Secure Your REST
PHP UK 2017 - Don't Lose Sleep - Secure Your RESTPHP UK 2017 - Don't Lose Sleep - Secure Your REST
PHP UK 2017 - Don't Lose Sleep - Secure Your REST
 
Docker for Developers - Sunshine PHP
Docker for Developers - Sunshine PHPDocker for Developers - Sunshine PHP
Docker for Developers - Sunshine PHP
 
WordPress for the modern PHP developer
WordPress for the modern PHP developerWordPress for the modern PHP developer
WordPress for the modern PHP developer
 
Building Big on the Web
Building Big on the WebBuilding Big on the Web
Building Big on the Web
 
Scalable Web Architectures: Common Patterns and Approaches
Scalable Web Architectures: Common Patterns and ApproachesScalable Web Architectures: Common Patterns and Approaches
Scalable Web Architectures: Common Patterns and Approaches
 
Php extensions
Php extensionsPhp extensions
Php extensions
 
PHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return TypesPHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return Types
 
flickr's architecture & php
flickr's architecture & php flickr's architecture & php
flickr's architecture & php
 
PHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodePHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better Code
 
Practical Performance Tips and Tricks to Make Your HTML/JavaScript Apps Faster
Practical Performance Tips and Tricks to Make Your HTML/JavaScript Apps FasterPractical Performance Tips and Tricks to Make Your HTML/JavaScript Apps Faster
Practical Performance Tips and Tricks to Make Your HTML/JavaScript Apps Faster
 
Giving birth to an ElePHPant
Giving birth to an ElePHPantGiving birth to an ElePHPant
Giving birth to an ElePHPant
 
Services Oriented Architecture with PHP and MySQL
Services Oriented Architecture with PHP and MySQLServices Oriented Architecture with PHP and MySQL
Services Oriented Architecture with PHP and MySQL
 

Similar to Taming the resource tiger

Taming the tiger - pnwphp
Taming the tiger - pnwphpTaming the tiger - pnwphp
Taming the tiger - pnwphpElizabeth Smith
 
DataIntensiveComputing.pdf
DataIntensiveComputing.pdfDataIntensiveComputing.pdf
DataIntensiveComputing.pdfBrahmam8
 
Technologies for Data Analytics Platform
Technologies for Data Analytics PlatformTechnologies for Data Analytics Platform
Technologies for Data Analytics PlatformN Masahiro
 
Scalability, Availability & Stability Patterns
Scalability, Availability & Stability PatternsScalability, Availability & Stability Patterns
Scalability, Availability & Stability PatternsJonas Bonér
 
Scaling etl with hadoop shapira 3
Scaling etl with hadoop   shapira 3Scaling etl with hadoop   shapira 3
Scaling etl with hadoop shapira 3Gwen (Chen) Shapira
 
Writing Scalable Software in Java
Writing Scalable Software in JavaWriting Scalable Software in Java
Writing Scalable Software in JavaRuben Badaró
 
Meta scale kognitio hadoop webinar
Meta scale kognitio hadoop webinarMeta scale kognitio hadoop webinar
Meta scale kognitio hadoop webinarKognitio
 
Building Big Data Streaming Architectures
Building Big Data Streaming ArchitecturesBuilding Big Data Streaming Architectures
Building Big Data Streaming ArchitecturesDavid Martínez Rego
 
Meta scale kognitio hadoop webinar
Meta scale kognitio hadoop webinarMeta scale kognitio hadoop webinar
Meta scale kognitio hadoop webinarMichael Hiskey
 
Dev nexus 2017
Dev nexus 2017Dev nexus 2017
Dev nexus 2017Roy Russo
 
Parallel and Asynchronous Programming - ITProDevConnections 2012 (Greek)
Parallel and Asynchronous Programming -  ITProDevConnections 2012 (Greek)Parallel and Asynchronous Programming -  ITProDevConnections 2012 (Greek)
Parallel and Asynchronous Programming - ITProDevConnections 2012 (Greek)Panagiotis Kanavos
 
Flashy prefetching for high performance flash drives
Flashy prefetching for high performance flash drivesFlashy prefetching for high performance flash drives
Flashy prefetching for high performance flash drivesPratik Bhat
 
Best practices for highly available and large scale SolrCloud
Best practices for highly available and large scale SolrCloudBest practices for highly available and large scale SolrCloud
Best practices for highly available and large scale SolrCloudAnshum Gupta
 
Backup and Disaster Recovery in Hadoop
Backup and Disaster Recovery in HadoopBackup and Disaster Recovery in Hadoop
Backup and Disaster Recovery in Hadooplarsgeorge
 
Devnexus 2018
Devnexus 2018Devnexus 2018
Devnexus 2018Roy Russo
 

Similar to Taming the resource tiger (20)

Taming the tiger - pnwphp
Taming the tiger - pnwphpTaming the tiger - pnwphp
Taming the tiger - pnwphp
 
DataIntensiveComputing.pdf
DataIntensiveComputing.pdfDataIntensiveComputing.pdf
DataIntensiveComputing.pdf
 
Technologies for Data Analytics Platform
Technologies for Data Analytics PlatformTechnologies for Data Analytics Platform
Technologies for Data Analytics Platform
 
Scalability, Availability & Stability Patterns
Scalability, Availability & Stability PatternsScalability, Availability & Stability Patterns
Scalability, Availability & Stability Patterns
 
Scaling etl with hadoop shapira 3
Scaling etl with hadoop   shapira 3Scaling etl with hadoop   shapira 3
Scaling etl with hadoop shapira 3
 
Writing Scalable Software in Java
Writing Scalable Software in JavaWriting Scalable Software in Java
Writing Scalable Software in Java
 
Meta scale kognitio hadoop webinar
Meta scale kognitio hadoop webinarMeta scale kognitio hadoop webinar
Meta scale kognitio hadoop webinar
 
Building Big Data Streaming Architectures
Building Big Data Streaming ArchitecturesBuilding Big Data Streaming Architectures
Building Big Data Streaming Architectures
 
Drupal performance
Drupal performanceDrupal performance
Drupal performance
 
Meta scale kognitio hadoop webinar
Meta scale kognitio hadoop webinarMeta scale kognitio hadoop webinar
Meta scale kognitio hadoop webinar
 
Dev nexus 2017
Dev nexus 2017Dev nexus 2017
Dev nexus 2017
 
Intro to Big Data
Intro to Big DataIntro to Big Data
Intro to Big Data
 
Server Tips
Server TipsServer Tips
Server Tips
 
Parallel and Asynchronous Programming - ITProDevConnections 2012 (Greek)
Parallel and Asynchronous Programming -  ITProDevConnections 2012 (Greek)Parallel and Asynchronous Programming -  ITProDevConnections 2012 (Greek)
Parallel and Asynchronous Programming - ITProDevConnections 2012 (Greek)
 
Flashy prefetching for high performance flash drives
Flashy prefetching for high performance flash drivesFlashy prefetching for high performance flash drives
Flashy prefetching for high performance flash drives
 
Best practices for highly available and large scale SolrCloud
Best practices for highly available and large scale SolrCloudBest practices for highly available and large scale SolrCloud
Best practices for highly available and large scale SolrCloud
 
Backup and Disaster Recovery in Hadoop
Backup and Disaster Recovery in Hadoop Backup and Disaster Recovery in Hadoop
Backup and Disaster Recovery in Hadoop
 
Backup and Disaster Recovery in Hadoop
Backup and Disaster Recovery in HadoopBackup and Disaster Recovery in Hadoop
Backup and Disaster Recovery in Hadoop
 
Big Data for QAs
Big Data for QAsBig Data for QAs
Big Data for QAs
 
Devnexus 2018
Devnexus 2018Devnexus 2018
Devnexus 2018
 

More from Elizabeth Smith

Database theory and modeling
Database theory and modelingDatabase theory and modeling
Database theory and modelingElizabeth Smith
 
Security is not a feature
Security is not a featureSecurity is not a feature
Security is not a featureElizabeth Smith
 
Mentoring developers-php benelux-2014
Mentoring developers-php benelux-2014Mentoring developers-php benelux-2014
Mentoring developers-php benelux-2014Elizabeth Smith
 
Socket programming with php
Socket programming with phpSocket programming with php
Socket programming with phpElizabeth Smith
 
Spl in the wild - zendcon2012
Spl in the wild - zendcon2012Spl in the wild - zendcon2012
Spl in the wild - zendcon2012Elizabeth Smith
 
Mentoring developers - Zendcon 2012
Mentoring developers - Zendcon 2012Mentoring developers - Zendcon 2012
Mentoring developers - Zendcon 2012Elizabeth Smith
 
Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012Elizabeth Smith
 
Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for DummiesElizabeth Smith
 
Event and signal driven programming
Event and signal driven programmingEvent and signal driven programming
Event and signal driven programmingElizabeth Smith
 

More from Elizabeth Smith (20)

Welcome to the internet
Welcome to the internetWelcome to the internet
Welcome to the internet
 
Database theory and modeling
Database theory and modelingDatabase theory and modeling
Database theory and modeling
 
Modern sql
Modern sqlModern sql
Modern sql
 
Php extensions
Php extensionsPhp extensions
Php extensions
 
Lexing and parsing
Lexing and parsingLexing and parsing
Lexing and parsing
 
Hacking with hhvm
Hacking with hhvmHacking with hhvm
Hacking with hhvm
 
Security is not a feature
Security is not a featureSecurity is not a feature
Security is not a feature
 
Using unicode with php
Using unicode with phpUsing unicode with php
Using unicode with php
 
Mentoring developers-php benelux-2014
Mentoring developers-php benelux-2014Mentoring developers-php benelux-2014
Mentoring developers-php benelux-2014
 
Using unicode with php
Using unicode with phpUsing unicode with php
Using unicode with php
 
Socket programming with php
Socket programming with phpSocket programming with php
Socket programming with php
 
Mentoring developers
Mentoring developersMentoring developers
Mentoring developers
 
Do the mentor thing
Do the mentor thingDo the mentor thing
Do the mentor thing
 
Spl in the wild - zendcon2012
Spl in the wild - zendcon2012Spl in the wild - zendcon2012
Spl in the wild - zendcon2012
 
Mentoring developers - Zendcon 2012
Mentoring developers - Zendcon 2012Mentoring developers - Zendcon 2012
Mentoring developers - Zendcon 2012
 
Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012
 
Mentoring developers
Mentoring developersMentoring developers
Mentoring developers
 
Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for Dummies
 
Event and signal driven programming
Event and signal driven programmingEvent and signal driven programming
Event and signal driven programming
 
Spl in the wild
Spl in the wildSpl in the wild
Spl in the wild
 

Recently uploaded

定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一Fs
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxDyna Gilbert
 
Blepharitis inflammation of eyelid symptoms cause everything included along w...
Blepharitis inflammation of eyelid symptoms cause everything included along w...Blepharitis inflammation of eyelid symptoms cause everything included along w...
Blepharitis inflammation of eyelid symptoms cause everything included along w...Excelmac1
 
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja VipCall Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja VipCall Girls Lucknow
 
Contact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New DelhiContact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New Delhimiss dipika
 
A Good Girl's Guide to Murder (A Good Girl's Guide to Murder, #1)
A Good Girl's Guide to Murder (A Good Girl's Guide to Murder, #1)A Good Girl's Guide to Murder (A Good Girl's Guide to Murder, #1)
A Good Girl's Guide to Murder (A Good Girl's Guide to Murder, #1)Christopher H Felton
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书zdzoqco
 
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一Fs
 
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一Fs
 
PHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationPHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationLinaWolf1
 
Magic exist by Marta Loveguard - presentation.pptx
Magic exist by Marta Loveguard - presentation.pptxMagic exist by Marta Loveguard - presentation.pptx
Magic exist by Marta Loveguard - presentation.pptxMartaLoveguard
 
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012rehmti665
 
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一Fs
 
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一z xss
 
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作ys8omjxb
 
Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24Paul Calvano
 

Recently uploaded (20)

定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptx
 
Model Call Girl in Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in  Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in  Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
 
Blepharitis inflammation of eyelid symptoms cause everything included along w...
Blepharitis inflammation of eyelid symptoms cause everything included along w...Blepharitis inflammation of eyelid symptoms cause everything included along w...
Blepharitis inflammation of eyelid symptoms cause everything included along w...
 
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja VipCall Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
 
Contact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New DelhiContact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New Delhi
 
A Good Girl's Guide to Murder (A Good Girl's Guide to Murder, #1)
A Good Girl's Guide to Murder (A Good Girl's Guide to Murder, #1)A Good Girl's Guide to Murder (A Good Girl's Guide to Murder, #1)
A Good Girl's Guide to Murder (A Good Girl's Guide to Murder, #1)
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
 
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
 
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
 
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
 
PHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationPHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 Documentation
 
Magic exist by Marta Loveguard - presentation.pptx
Magic exist by Marta Loveguard - presentation.pptxMagic exist by Marta Loveguard - presentation.pptx
Magic exist by Marta Loveguard - presentation.pptx
 
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
 
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
 
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
 
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
 
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
 
Hot Sexy call girls in Rk Puram 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in  Rk Puram 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in  Rk Puram 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Rk Puram 🔝 9953056974 🔝 Delhi escort Service
 
Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24
 

Taming the resource tiger

  • 1. Taming the resource tiger You cannot hide from Physics!
  • 2. Mass How much matter is in an object
  • 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. 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)
  • 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. 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. 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. What does a this have to do with PHP?
  • 10. The chat that worked for 3 days…
  • 11. What uses streams in PHP? • EVERYTHING • include/require _once • stream functions • file system functions • many other extensions
  • 14. You can also do logic on the fly!
  • 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.
  • 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. Throw away your assumptions except for: There will be Terabytes of Cat Gifs!!
  • 19. Dimension Both an object’s size and mathematical space
  • 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
  • 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. 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. How do I fix it!
  • 25. Halp, I can’t upload!!
  • 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. 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. Select * from table
  • 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. 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. Throw away all your assumptions except:
  • 32. Speed The rate at which an object covers distance
  • 33. How does a CPU work?
  • 34. CPU limitations • Transmission delays • Heat • Both are materials limitations • http://www.mooreslaw.org/
  • 35. Why I no longer overclock
  • 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. 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. Profiling Options Free • Xdebug • Xhprof • Uprofiler Paid • NewRelic • AppDynamics • Blackfire • Tideways • Tracelytics
  • 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. Pick your system • php-resque • Gearman • Beanstalkd • IronMQ • RabbitMQ • ZeroMQ • AmazonSQS • Just visit http://queues.io
  • 41. Job queuing and 10K page pdfs
  • 42. Keep your CPU happy • Offload processing • Use a queue
  • 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
  • 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. Network Socket Types • Stream • Connection oriented (tcp) • Datagram • Connectionless (udp) • Raw • Low level protocols
  • 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
  • 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. What do you mean we can’t write files?
  • 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. 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
  • 55. About Me  http://emsmith.net  auroraeosrose@gmail.com  twitter - @auroraeosrose  IRC – freenode – auroraeosrose  #phpmentoring  https://joind.in/talk/18dd4

Editor's Notes

  1. No matter how many virtual machines you throw at a problem you always have the physical limitations of hardware. Memory, CPU, and even your NIC's throughput have finite limits. Are you trying to load that 5 GB csv into memory to process it? No really, you shouldn't! PHP has many built in features to deal with data in more efficient ways that pumping everything into an array or object. Using PHP stream and stream filtering mechanisms you can work with chunked data in an efficient matter, with sockets and processes you can farm out work efficiently and still keep track of what your application is doing. These features can help with memory, CPU, and other physical system limitations to help you scale without the giant AWS bill.
  2. Our first physical law we’ll talk about is mass, how much matter is in a thing In physics, mass is a property of a physical body. It is the measure of an object's resistance to acceleration (a change in its state of motion) when a force is applied.[1] It also determines the strength of its mutual gravitational attraction to other bodies. Mass is not the same as weight, even though we often calculate an object's mass by measuring its weight with a spring scale, rather than comparing it directly with known masses. An object on the Moon would weigh less than it does on Earth because of the lower gravity, but it would still have the same mass. This is because weight is a force, while mass is the property that (along with gravity) determines the strength of this force.
  3. NAND sacrifices the random-access and execute-in-place advantages of NOR. NAND is best suited to systems requiring high capacity data storage. It offers higher densities, larger capacities, and lower cost. It has faster erases, sequential writes, and sequential reads. HDDs • Enthusiast multimedia users and heavy downloaders: Video collectors need space, and you can only get to 4TB of space cheaply with hard drives. • Budget buyers: Ditto. Plenty of cheap space. SSDs are too expensive for $500 PC buyers. • Graphic arts and engineering professionals: Video and photo editors wear out storage by overuse. Replacing a 1TB hard drive will be cheaper than replacing a 500GB SSD.
  4. The maximum areal storage density for flash memory used in SSDs is 2.8 Tbit/in2 in laboratory demonstrations as of 2016, and the maximum for HDDs is 1.5 Tbit/in2. The areal density of flash memory is doubling every two years, similar to Moore's law (40% per year) and faster than the 10–20% per year for HDDs. As of 2016, maximum capacity was 10 terabytes for an HDD,[10] and 15 terabytes for an SSD.[15] HDDs were used in 70% of the desktop and notebook computers produced in 2016, and SSDs were used in 30%. The usage share of HDDs is declining and could drop below 50% in 2018–2019 according to one forecast, because SSDs are replacing smaller-capacity (less than one-terabyte) HDDs in desktop and notebook computers and MP3 players.[154] Areal density is a measure of the quantity of information bits that can be stored on a given length of track, area of surface, or in a given volume of a computer storage medium. Generally, higher density is more desirable, for it allows greater volumes of data to be stored in the same physical space. Density therefore has a direct relationship to storage capacity of a given medium. Density also generally has a fairly direct effect on the performance within a particular medium, as well as price.
  5. Story about me and my vm drive and the bad blocks go bad
  6. Quick computer science lesson Originally done with magic numbers in fortran, C and unix standardized the way it worked On Unix and related systems based on the C programming language, a stream is a source or sink of data, usually individual bytes or characters. Streams are an abstraction used when reading or writing files, or communicating over network sockets. The standard streams are three streams made available to all programs. Who else uses them? Most languages descended from C have the “files as streams concept” and ways to extend the IO functionality beyond merely files, this allows them to be merged all together Great way to standardize the way data is grabbed and used Questions on who has used streams in other languages
  7. Streams are a huge underlying component of PHP Streams were introduced with PHP 4.3.0 – they are old, but underuse means they can have rough edges… so TEST TEST TEST But they are more powerful then almost anything else you can use Why is this better ? Lots and lots of data in small chunks lets you do large volumes without maxing out memory and cpu
  8. So this is a very common problem in PHP scripts, PHP bombing out because a file_get_contents call loaded something too big into memory Although file_get_contents is pretty great, doing it without a size check is deadly (well, unless you total control the file) Consider files user data just like that POST data you just got from a user
  9. Let’s talk about how space on disk is important
  10. Talk about a very early experiment writing a chat room for my personal website… using a text file that I concated and read Hey, this was 1998 and it was running php-nuke ;) But that rapidly changed to a gigabyte file when my friends tested it all night long 
  11. Any good extension will use the underlying streams API to let you use any kind of stream for example, cairo does this stuff to work with PHP streams is spread across at least two portions of the manual, plus appendixes for the build in transports/filters/context options. It’s very poorly arranged so be sure to take the time to learn where to look in the manual – there should be three main places What doesn’t use streams? Chmod, touch and some other very file specific funtionality, lazy/bad extensions, extensions with issues in the libraries they wrap around
  12. All input and output comes into PHP It gets pushed through a streams filter Then through the streams wrapper During this point the stream context is available for the filter and wrapper to use Streams themselves are the “objects” coming in Wrappers are the “classes” defining how to deal with the stream
  13. Some notes – file_get_contents and it’s cousin stream_get_contents are your fastest most efficient way if you need the whole file File(blah) is going to be the best way to get the whole file split by lines Both are going to stick the whole file into memory at some point. For very large files and to help with memory consumption, the use of fgets and fread will help
  14. You don’t even have to load all the data in to work on it with PHP! You can do everything on the fly in chunks That’s the magic of filtering
  15. A filter is a final piece of code which may perform operations on data as it is being read from or written to a stream. Any number of filters may be stacked onto a stream. Custom filters can be defined in a PHP script using stream_filter_register() or in an extension using the API Reference in Working with streams. To access the list of currently registered filters, use stream_get_filters(). Stream data is read from resources (both local and remote) in chunks, with any unconsumed data kept in internal buffers. When a new filter is prepended to a stream, data in the internal buffers, which has already been processed through other filters will not be reprocessed through the new filter at that time. This differs from the behavior of stream_filter_append(). Filters are nice for manipulating data on the fly – but remember you’ll be getting data in chunks, so your filter needs to be smart enough to handle that
  16. Filters can be appended or prepended – and attached to READ or WRITE Notice that stream_filter_prepend and append are smart – if you opened with the r flag, by default it’ll attach to read, if you opened with the w flag, it will attach to write Note: Stream data is read from resources (both local and remote) in chunks, with any unconsumed data kept in internal buffers. When a new filter is prepended to a stream, data in the internal buffers, which has already been processed through other filters will not be reprocessed through the new filter at that time. This differs from the behavior of stream_filter_append(). Note: When a filter is added for read and write, two instances of the filter are created. stream_filter_prepend() must be called twice with STREAM_FILTER_READ and STREAM_FILTER_WRITE to get both filter resources.
  17. Well it may look like manipulating data in a variable is preferable to the above. But the above is just a simple example. Once you add a filter to a stream it basically hides all the implementation details from the user. You will be unaware of the data being manipulated in a stream. And also the same filter can be used with any stream (files, urls, various protocols etc.) without any changes to the underlying code. Also multiple filters can be chained together, so that the output of one can be the input of another. The filters need an input state and an output state. And they need to respect the the fact that number of requested bytes does not necessarily mean reading the same amount of data on the other end. In fact the output side does generally not know whether less, the same amount or more input is to be read. But this can be dealt with inside the filter. However the filters should return the number input vs the number of output filters always independently. Regarding states we would be interested if reaching EOD on the input state meant reaching EOD on the output side prior to the requested amount, at the requested amount or not at all yet (more data available).
  18. Throw away all your old assumptions and make a new one Trust no one with your file/stream manipulations Assume that file is a terabyte zip file of cat gifs Nope, not kidding
  19. Dimension is a neat word because we overload it, like filter In physics and mathematics, the dimension of a mathematical space (or object) is informally defined as the minimum number of coordinates needed to specify any point within it.
  20. The "memory wall" is the growing disparity of speed between CPU and memory outside the CPU chip. An important reason for this disparity is the limited communication bandwidth beyond chip boundaries, which is also referred to as bandwidth wall. From 1986 to 2000, CPU speed improved at an annual rate of 55% while memory speed only improved at 10%. Given these trends, it was expected that memory latency would become an overwhelming bottleneck in computer performance
  21. Uploading items kept failing Realized the issue was the sheer amount of data being synced, because the system had waited all evening when wifi went out
  22. A lot of people don’t realize that you as a developer are responsible for managing the amount of memory consumed by PHP No one wants to hear that but it’s true
  23. PHP’s inherent characteristics are hiding this issue with memory
  24. It’s very hard to duplicate very large scale issues in testing, which is often why this stuff isn’t caught until it’s time to deploy
  25. SO, I had a system that worked locally on a client and then had a nightly upload The upload itself was working properly – saving the data and processing it appropriately But the RETURN was failing and screwing up the clients, because the return package was simply to large to send down Two changes were made to the system to allow syncing already saved entries, and we no longer passed back the changed data Frankly because it wasn’t important But also because we added windowing to the paging system
  26. In PHP 5.x a whopping 144 bytes per element were required. In PHP 7 the value is down to 36 bytes, or 32 bytes for the packed case but it’s STILL not the best So think about 30K items in an array
  27. Story about a cron job adding new items to our database, and no windowing functionality in sight
  28. Nested loops are of the devil
  29. Assume the data is always big Because if it’s not now, some day it will be Tests usually miss this, so just make a good habit
  30. So the first thing you always want to think about in your PHP application is speed right? After all PHP is soooo slow, computers are so slow… in everyday use and in kinematics, the speed of an object is the magnitude of its velocity (the rate of change of its position); it is thus a scalar quantity. The fastest possible speed at which energy or information can travel, according to special relativity, is the speed of light in a vacuum c = 299,792,458 metres per second (approximately 1079000000 km/h or 671,000,000 mph). Matter cannot quite reach the speed of light, as this would require an infinite amount of energy. In relativity physics, the concept of rapidity replaces the classical idea of speed.
  31. A microprocessor -- also known as a CPU or central processing unit -- is a complete computation engine that is fabricated on a single chip. Using its ALU (Arithmetic/Logic Unit), a microprocessor can perform mathematical operations like addition, subtraction, multiplication and division. Modern microprocessors contain complete floating point processors that can perform extremely sophisticated operations on large floating point numbers. A microprocessor can move data from one memory location to another. A microprocessor can make decisions and jump to a new set of instructions based on those decisions.
  32. Transmission delays occur in the wires that connect things together on a chip. The "wires" on a chip are incredibly small aluminum or copper strips etched onto the silicon. A chip is nothing more than a collection of transistors and wires that hook them together, and a transistor is nothing but an on/off switch. When a switch changes its state from on to off or off to on, it has to either charge up or drain the wire that connects the transistor to the next transistor down the line. Imagine that a transistor is currently "on." The wire it is driving is filled with electrons. When the switch changes to "off," it has to drain off those electrons, and that takes time. The bigger the wire, the longer it takes. As the size of the wires has gotten smaller over the years, the time required to change states has gotten smaller, too. But there is some limit -- charging and draining the wires takes time. That limit imposes a speed limit on the chip. There is also a minimum amount of time that a transistor takes to flip states. Transistors are chained together in strings, so the transistor delays add up. On a complex chip like the G5, there are likely to be longer chains, and the length of the longest chain limits the maximum speed­ of the entire chip. Finally, there is heat. Every time the transistors in a gate change state, they leak a little electricity. This electricity creates heat. As transistor sizes shrink, the amount of wasted current (and therefore heat) has declined, but there is still heat being created. The faster a chip goes, the more heat it generates. Heat build-up puts another limit on speed. processor speeds, or overall processing power for computers will double every two years Overclocking and burning a chip to death story 
  33. Talk about my experiment with overclocking my Athlon Was my first custom built computer, I thought I was cool beans because I figured out how to flash it, Athlons were the new shiny I overclocked the crap out of it And… it caught on fire (partially because I did the heat stuff too thin but also because I too much overclocked it) The smell of a burning processor is something I will not forget, and will never do again!
  34. (don’t guess) if you need to improve speed beyond good habits
  35. These are just a few “good habits” to cultivate when coding None of them SHOULD be new =, and these are often considered micro optimizations It’s not worth it to rewrite your code probably for these, but it IS worth it to cultivate them as a natural part of your coding style It just takes practice
  36. I’m sure there are more!
  37. There is nothing wrong with offloading work PHP scales VERY well horizontally, and often pretty cheaply horizontally as well Spin up a dedicated box for jobs If you have scaling in place, you can spin up two during heavy load times! often reports or generating files or images It’s not realistic to expect complex reports to be done in seconds, physics apply here too, good UX will mask your offloading is a good way to balance offloaded work with immediate results
  38. I’m not going to go into a lot of detail here, because what you eventually pick for jobs/queuing is going to be specific to your needs Queues.io is actually a really nice resource with lots of different queue types for many different languages
  39. Story about the render system, and how good choices here (queueing, triggering one job from another, etc) made even huge file generation just work
  40. Velocity is a physical vector quantity; both magnitude and direction are needed to define it. The scalar absolute value (magnitude) of velocity is called "speed", being a coherent derived unit whose quantity is measured in the SI (metric) system as metres per second (m/s) or as the SI base unit of (m⋅s−1). For example, "5 metres per second" is a scalar, whereas "5 metres per second east" is a vector. Speed describes only how fast an object is moving, whereas velocity gives both how fast and in what direction the object is moving.
  41. As with all other communications protocol, TCP/IP is composed of layers: IP - is responsible for moving packet of data from node to node. IP forwards each packet based on a four byte destination address (the IP number). The Internet authorities assign ranges of numbers to different organizations. The organizations assign groups of their numbers to departments. IP operates on gateway machines that move data from department to organization to region and then around the world. TCP - is responsible for verifying the correct delivery of data from client to server. Data can be lost in the intermediate network. TCP adds support to detect errors or lost data and to trigger retransmission until the data is correctly and completely received. Sockets - is a name given to the package of subroutines that provide access to TCP/IP on most systems.
  42. So your application level is the basic data you want to send in most http applications this is your http page INLUDING the headers section the transport is how you’re sending it – UDP and TCP are the most popular the Internet layer is the “IP” layer – with the header telling the system what address (ip) to send the data to and what port to take to then you get a frame header and footer on the actual piece of data the packet being sent
  43. This is a VERY simplified analogy, but for the basic idea – think of the internet as water flowing through pipes at a constant pressure (data is electricity so close to the speed of light) bigger and better pipes can handle more, you can get air bubbles in the pipes, and no matter what you did, if the pipe is longer it will take longer
  44. There are different types of socket types you can use, a lot of people use tcp and HTTP because they’re a known procol
  45. What is streamable behavorior? We’ll get to that in a bit Protocol: set of rules which is used by computers to communicate with each other across a network Resource: A resource is a special variable, holding a reference to an external resource Talk about resources in PHP and talk about general protocols, get a list from the audience of protocols they can name (yes http is a protocol) A socket is a special type of stream – pound this into their heads A socket is an endpoint of communication to which a name can be bound. A socket has a type and one associated process. Sockets were designed to implement the client-server model for interprocess communication where: In php , a wrapper ties the stream to the transport – so your http wrapper ties your PHP data to the http transport and tells it how to behave when reading and writing data
  46. By default sockets are going to assume tcp – since that’s a pretty standard way of doing things. Notice that we have to do things the old fashioned way just for this simple http request – sticking our headers together, making sure stuff gets closed. However if you can’t use allow_url_fopen this is a way around it a dirty dirty way but – there you have it remember allow_url_fopen only stops “drive-by” hacking
  47. Docker and s3 and how abstracting stuff out kept me sane Also how error handling
  48. Your checklist for not running out of PHP memory when your code runs
  49. There is SOOO much more you can do from hooking objects to hooking the engine!