The Stateless Development Machine

Are you spending 2-3 hours and sometimes a day getting your new laptop ready for the development? I used to do it with ansible and be happy about it. With Docker, it has now become easier. I have following in my .zshr and it works pretty great.

The .zshrc aliases

alias npm='docker run -ti --rm -v $(pwd):/src:rw -e "PUID=$UID" -e "PGID=$GID" mkenney/npm:latest npm'
alias gulp='docker run -ti --rm -v $(pwd):/src:rw -e "PUID=$UID" -e "PGID=$GID" mkenney/npm:latest gulp'
alias node='docker run -ti --rm -v $(pwd):/src:rw -e "PUID=$UID" -e "PGID=$GID" mkenney/npm:latest node'
alias bower='docker run -ti --rm -v $(pwd):/src:rw -e "PUID=$UID" -e "PGID=$GID" mkenney/npm:latest bower'
alias bundle='docker run -ti --rm -v $(pwd):/src:rw -e "PUID=$UID" -e "PGID=$GID" rails:latest bundle'
alias composer='docker run -ti --rm -v $(pwd):/app:rw -e "PUID=$UID" -e "PGID=$GID" composer:latest composer'
alias go='docker run -ti --rm  -w /usr/src/myapp -v $(pwd):/usr/src/myapp:rw -e "PUID=$UID" -e "PGID=$GID" golang:latest go'
alias nghttp='docker run --rm -it dajobe/nghttpx nghttp'
function dbash() {
    docker exec -it $1 /bin/bash
}
alias dbash=dbash

Understanding

Obviously, the list can go on but understanding here is to keep your system in a stateless mode. Simply by having dotfiles backup, you can move your system with no trouble. Now days everything is in Git already!

Worth Sharing?

Ease your Node Web development with these 10 npm packages.

When we code, we often don’t rely on single bulkier packages. Since a bulkier framework would tie you up and some of its core-functionality isn’t really good for your app performance or maintainability.

For instance, you would like to bring your own ORM which you are using in small to big products. A native ORM is more likely to support more drivers than a full-fledged framework.

Top 10 must have npm packages

  1. Express – Express framework is extremely lightweight and many developers have expanded it. You can find many extensions of express in npmjs site.
  2. Moment – Moment is a goto package for time-based calculation or formatting time.
  3. Sequelize Or Mongoose – Sequelize is an AR-based ORM and support multiple relational databases. It comes with all the important functionality. File mongoose is for MongoDB
  4. Gulp – Depending on your choice, you would want Grunt or Gulp, my personal choice is Gulp as it gives more control.
  5. Bluebird – More of the advanced promises features are available in this package. My personal favorite is Promise.race.
  6. Lodash – Lodash comes up with a lot of small functions which you can use to modify, manipulate data.
  7. Chalk – Chalk is a complete solution to style terminal
  8. Bunyan – Bunyan enables you to format codes in more readable and expressive
  9. Got – Got is the most powerful package to send out requests.
  10. Webpack – Webpack bundles the javascript for a browser.

Worth Sharing?





How to write the high-performance application in PHP?

We coded Product Kits app and it worked pretty well. Peak hits were 5000 hits per seconds. Read Story of Product Kits from 100 to 100000 hits per minute. We had a lot of issues, but issues with PHP wasn’t scalability. We were able to handle everything but the problem came when we had to know what other workers were doing.

The first question that might come to your mind is if you have chosen the write tech stack? Since we are aware of the fact that PHP doesn’t support multi-threading. However, there is a trade-off between the speed of development and the performance. PHP is not very fast, in fact, it is slow – but it is fast enough. As long as you don’t want individual PHP scripts to know the state of each other, you are in a pretty good shape – most of the time.

Trade-Off – Scalable VS Speed

While using PHP, our major concern was RAM, it was much easier to get high RAM usage and CPU. We had to deal with a lot of data and data most of the time either stays in RAM or required us to increase the HIT if we wanted to keep it outside. If your PHP codes are using a lot of RAM, you will have to solve a scalability problem. However, if your app doesn’t require a lot of RAM, better is to optimize it for speed.

Writing the Right Codes:

  1. Rely on always running PHP codes – If a worker is written in PHP, tie the worker in an infinite loop which will wait for an event (A Queue, MySQL entry) instead of invoking PHP every second or so.
  2. Cache sooner – Although there are a couple of cache options in PHP – OPcache and Memcache. However, Redis is favourite which can further help you scaling by having multiple master or other topology. Combination of opcache and redis will be best.
  3. Load fewer classes – Ensure that you are not loading a lot of classes, rely on dynamic loading. This will increase the speed and reduce the memory.
  4. Keep over-writing variables – This is a pretty bad practice but it ensures that your memory is limited.
  5. Make smaller blocks – A heavy code or multiple functional calls under the loop are your sworn enemy. It is better to write multiple loops few smaller blocks than to have one large block.
  6. Use JSON instead of XML – JSON is a new standard and takes lesser memory.
  7. Use classes – Obvious but – having functions inside class will make it less memory hogger as long as you are loading classes when needed.

Micro Optimization of your codes:

These optimizations are not something you should do after the development as it has a very little effect. However, right from the beginning, a good practice is to follow it.

  1. Promote ‘static’ – This alone can increase the execution speed by 3X.
  2. Use single quotes – ‘ – As long as there is variable inside.
  3. Use str_replace instead of preg_replace_all
  4. Use ‘===’ instead of ‘==’
  5. Use ‘isset’ instead of count/size

Worth Sharing?