PHP Conference 2015

I’ve just attended the PHP Australia conference on the 12th and 13th of March. This was my first ever actual full blown conference in anything, and I have to say, it was pretty amazing. Apart from the creators of PHP and MySQL (now working on MariaDB of course), there was an awesome list of speakers and lots of great food. This is probably a good time to give Seb kudos for managing to organise such a packed event.

I’ve been compiling notes for work (props to 4mation for paying for my ticket), and so far I have almost 4 pages, which is pretty cool. There are so many takeaways, it seems pointless to even try to get into them, however I will say that talking to Rasmus and Monty was by far the highlight of the two days. Lesser so, it was also interesting to discuss technologies in use with people from all around Australia, what their stack is, and how they go about development.

The Value of Learning

Today at work, at our Monday general meeting, I gave a brief speech about my take on our value of Learning… so why not also share it with the world?

I believe that no other industry other than ours is so fast paced in terms of the technology needed to accomplish our jobs. Something which might be relevant one year, will be shunned and largely disused the next. Every week there is a new acronym for a new developer tool, every month there is an update to a framework we use. Development paradigms are always changing.

In short, if we are to excell above our competitors, we must truly embrace all facets of the learning process. We quite simply don’t have the right to call ourselves professionals without fully exhausting all the avenues we have at our disposal to learn new things.

These will come in the form of online seminars, meetup groups and conferences, discussing projects with colleages, work outside of work, always helping people – on stack overflow, forums and IRC, hours and hours of reading online documentation at midnight, and a good old fashioned book or two.

The media and news programs always report that our profession has benefits and culture which is the envy of every worker on the planet. I for one believe we should work hard to earn this place of ours in the world.

Remote control your phone through ADB

This weekend I’ve dropped my phone once too many times, and the screen controller died. The man at the phone repair shop said the motherboard is broken, and nothing can be done, and there’s no way any data can be retrieved. The touchscreen is non responsive, hence there’s no way to unlock my phone, even if the screen is replaced.

Very very luckily, I still somehow had my phone’s ADB key stored on my computer, and a colleague of mine told me about a program which without me having to do anything on the phone at all, can use that key to remote control my phone from the computer, and emulate the touch screen. You can find more details about that here, but long story short, the existence of that program, and me having hooked up my phone to adb to play around with it once before totally saved me from losing 2 years of important personal data from my phone.

I’ve never been so relieved.

The Laravangular Stack (with Postgres)

Let’s start March with some tech talk.

I’m going to be talking about technologies that we use to develop new web applications from scratch. For normal applications (ie. the 98% thereof which contrary to what hipster devs on your team believe, do not have a legitimate use case for mongo), this is the technology stack which I feel is far superior to any other choice. I call it the Laravangular Stack.

To begin with, all modern web applications must be designed API first with a separate backend and frontend. The reasons for this are obvious;

  • Separation of concerns
  • Easier automated testing
  • Code once for multiple consumers (ie. web, mobile, device, API for 3rd parties, etc)
  • Performance
  • Easier to upgrade components
  • Easier to debug
  • Easier to divide dev workload
  • Your application architecture isn’t a sack of shit
  • Ability to have a rich, interactive frontend (and stop pretending that jQuery can accomplish this)

The question then becomes, what specific technologies do we use for such an application? In my view these answers are obvious.

Laravel has you covered on the backend
Laravel is the best thing that’s ever happened to web programming period. The most popular framework has you totally covered for API-first projects, and will deliver more features with more simplicity than anything else in existence – while the community is hard at work churning out thousands of packages for you to leverage in your project. It’s an obvious choice.

Angular for a superb user experience
Angular, at least for now, is the only proper actual front-end framework. Yes, it’s far from perfect, and some have even gone so far as to call it a tech demo…. however until Angular 2 comes out, there isn’t anything which can compete with it because everything else is essentially a bit of wet lettuce. Frontend JS frameworks are a very recent invention, and it’s an interesting new space – but the sooner you come to terms with the fact that Angular at the moment simply shits on everything else out there, the sooner you can begin to create world-beating applications.

PostgreSQL for RDMS
Many people still use MariaDB (or god forbid MySQL), however I’ve felt for a long time that it is rather inferior to PostgreSQL. A project I’m working on now has cemented my view, with 2 rather sharp examples.

Our first problem stemmed from having a very high write workload for one logging aspect in our system. The use case involves a very large number of external devices writing logs to our system every few minutes. In our testing, InnoDB is a severe bottleneck for write-heavy workloads. I have read up on this, and the tl;dr is that there is no way to fix it – it’s just the way it’s designed. That rather sucks.
The second problem are UUIDs. UUIDs are pretty much the gold standard now, especially when it comes to anything API. How do you store them in a database though? Well, in the case of MariaDB, there is no perfect solution. If you want optimal performance, you can choose to store it as a binary 16 – and say goodbye to ever being able to manually read or write any UUID. Alternatively you could store it as varchar 36 (or 32 without the dashes), but take a non-trivial performance hit.

PostgreSQL on the other hand has had a naitive UUID field for a while now, which is internally stored as if it were binary, but behaves like a char. Likewise, it has no performance problems with write-heavy workloads. Perfect!

Of course when you come down to the bottom of it, there are many more differences between MariaDB and PostgreSQL – and you’ll likely find that PostgreSQL can do everything MariaDB does, but also more. It just so happens that PostgreSQL is always first to the party with any number of features that it’s had first in the last decade or more, including being an actual database system with full ACID compliance. It’s only pitfall if any is the lack of tools (relatively speaking) compared to MariaDB. If you can get past that barrier however, I would definitely recommend it.

Everything else is irrelevant
Whether you use Apache or Nginx, they both proxy pass PHP requests straight to php-fpm in all modern configurations, and let’s face it, nobody serves static assets anymore – it all comes from CDNs. Whatever other technologies used, wherever you are hosted – it’s less relevant than getting the fundamentals right.

Laravel Groupwise Max

For a project I’m working on, I really needed to do a groupwise max in Laravel. If you’re here, you likely don’t need an explanation of what that is, so much as how to actually bloody do it, so without further delay;

Let’s say that we have Actions, and we have an ActionLog, and you want to get the latest ActionLog entry for a series of Actions – determined by their “timestamp” field. You would write this:

Unfortunately there’s no way to do it without the dreaded DB::raw, but otherwise it turns out to be pretty simple.

Phunconf 5.0

Just attended Phunconf 5.0 yesterday. It was a great event with lots of very interesting discussions, however probably the most salient point I want to draw attention to is the following image.

To provide some context, at Phunconf events our tradition is to open the floor to the community to suggest topics for circle discussions. This year, all attendees were given two red circles, which they could put on any topic of their choice, in order for everyone to democratically pick the most popular topics which we would then discuss. The outcome was as follows:

Phunconf 5 Whiteboard

The two post-it notes with so many circles on them that you can’t see what’s written there, are of course Angular and Laravel.

Missing Auth header in Apache

For some reason Apache 2.4 (and maybe earlier versions, but nobody should care about them) drop the Auth header. No idea why, but here’s the solution:

PostgreSQL vs MariaDB – a recent experience

In developing API-centric backend applications, there are two main points of contention which make me want to use PostgreSQL over MariaDB (or MySQL) right now.

Native Support for the UUID data type
Why is this important? Well, every backend system should only make it’s users (be it an Angular frontend or a mobile app or a 3rd party consumer) aware of the UUID of a given object. Internal incrementing IDs are not the business of the consumer of the API, for all sorts of security and privacy reasons. Plus, dealing with UUIDs allows API consumers to create related objects themselves including their UUIDs and send them straight to the API all together.

To that end, MariaDB simply has no nice way of dealing with them. Performance wise, the best thing to do is to make them a BINARY 16 length field. However, this will make them unreadable in any sort of interface, and certainly not editable – which is simply unacceptable for development and testing. And yes, I am aware it’s possible to filter them down to readable format, but who the hell has the time to do that everywhere and everytime you need to see a UUID? Ridiculous!

That just leaves the option of making them a char 32 length field – but that has a significant performance drag over binary (which is slower still than an integer field already!).

Enter PostgreSQL. It has a naitive UUID field whereby it is internally stored as a binary for maximum performance, but as far as it’s displayed to the user, it will in all situations act like a character field type. This is precisely the sort of functionality developers want in modern applications. I know it’s on the bucket list for MariaDB, but who knows when it will be implemented.

Horrible Performance for mass inserts
InnoDB is a terrible engine – no two ways about it. That’s why MariaDB devs are working furiously on XtraDB, to replace it. Meanwhile, one of the biggest problems users are stuck with is horrendous insert performance.

Let’s say you have an application that is constantly receiving a large number of records, which must all go into a certain table. Sure you can store them in memory as they come in – concurrency in the application itself need not be a problem. But what happens when you have too much data for InnoDB itself to insert in a timely manner? A really big problem is what.

This is another use case where PostgreSQL simply does not suffer from the same performance issues that InnoDB does. And don’t even dare to mention MyISAM, you may as well use floppy disks if you want to go down that route.

Magento 1.9 breaks cart rules and more

Just upgrading a massive site to Magento 1.9, and I’ve encountered two particularly odd problems.

  1. Cart rules are broken – such that discount rules will only apply to the first product in your cart. What the hell man, right? Luckily, Hussey Coding have written a fix here.
  2. For whatever mad reason, the directory /code/core/Mage/GoogleCheckout should actually be called /code/core/Mage/Googlecheckout, otherwise the compiler does not work.

For that last one, a small fix that I did was to add a class alias at the bottom of /app/code/core/Mage/GoogleCheckout/Helper/Data.php

Thought I’d share those with everyone just in case you run across these issues.

Overly personal emails from new relic?

Something our team at work has found quite comical lately is the increasingly personal and nonsensical emails from our new relic “contact” or “account manager”. Just have a read of these;

This was his first email to us.

6th August 2014

I hope all is well. We recently had a re-org of accounts here and I will be your new contact. So I wanted to introduce myself and see how the New Relic trial is coming along. It appears you have successfully deployed the agent into your app and it is reporting back some interesting data. Do you have any questions I can help with?

Let me know if you are seeing value as we have a promotion running right now that offers a significant discount.

Nothing suss so far, pretty standard. But then just a bit over a month later, he seemed to be ready to move on;

Hello there, This message is to notify you that I will not be your account manager for 4mation Technologies starting next quarter since I was promoted. How does this help you? Still considering a paid account? Great! bc now is the PERFECT time since I have every reason to help you out plus you wont have to start this whole process over again with a new account manager next quarter.

To review current end of quarter promotions (plus something extra!) for your account and get yourself an awesome deal, please contact me ASAP! 🙂

tl;dr:
I wont be your account manager soon and if you think you want NR Pro instead of Lite, now you can get an awesome discount & I can go out strong.

Let’s make a win-win, cheers!

Some really casual use of language there. Then 3 days later, he follows up with a follow-up;

Hey again, did you have any update on this? Something you wanted to take advantage of by chance? -Troy

Then another month later, the guy must have totally forgotten about the whole affair, because he informs us he’s leaving again;

Hello there, This email is to let you know I will not be your New Relic account manager for 4mation Technologies next month since I’ve been promoted!

Why is this great news for you?
I have approval for OVER 40% discounts.
I can enroll you for discontinued startup packages
there’s nothing to stop be from giving you as great a deal possible

Love the tool, looked into a paid account but was too expensive? Sounds familiar?

This is the PERFECT time to review your options again! Bc if you upgrade the 3 hosts you have connected, you get the maximum discount possible!

Please contact me if you are interested to see what this would look like. Thank you!

Hi again, just a brief follow up to my last email to say again how much I’d like to be the one welcoming you as a customer by making it as easy for you as possible.

It looks like you are NR a lot recently so I thought you might be interested. Can you let me know if you would like to review pricing?

I’d really like to do everything I can for you.

Thanks,
Troy

P.S. This is not an automated email, me = human 🙂

This string of same emails with follow ups is so ridiculous, that I suppose he does feel the need to state they are not automated (or are they?).

Then over a month later, we get another email from him titled, “what happened today will never happen again”.

Pardon the spam fellow data nerd, but this email is worth reading if you are opportunistic and actively using New Relic…

tl,dr;
new relic flash sale on APM now gets you additional products Browser, Synthetics for free. why = big push by sales org due to recent events. YOU QUALIFY.

See us in the news recently? It’s a super exciting time for New Relic right now- check it out! Because of what you’ll find, our Sales org just rolled out a packages that give a over 40% discount AND includes Browser Pro & Synthetics Pro at no additional cost- FREE! Yes, 40% discount on APM when including Browser & Synthetics.

The minimum number of licensed hosts needed to qualify for this discount pack is 4, you currently have 3 connected for your 4mation Technologies account.

Does this sound attractive to you? Maybe you were considering a smaller or larger plan instead?

Either way, please contact me to start a conversation about this- what happened today will never happen again.

So 4 months after he said he is promoted and will no longer be our account manager, he… still is? While perfectly harmless, we just found all these emails and their timing to be quite funny.

Update: After that, we’ve gotten 2 more (and slightly more generic) emails from the buy, latest one being March.

CDNJS

Forget about Google’s hosted libraries and say hello to CDNJS. I can’t believe I’ve only now found out about this!

Elevator pitch:

  • Has servers in Australia wheres Google doesn’t, so latency is an order of magnitude faster
  • Has tonnes of javascript, css and other frontend libraries – much larger selection than google
  • List of libraries is community managed via github

Those are pretty much the key points. Read more here, and it’s located at cdnjs.com.

My own testing confirms the massive latency benefit over Google – just check out this download of jQuery, 0.9 seconds for Google versus 0.06 seconds from CDNJS. That’s a crazy big difference!

If you’re supplying Australian customers, you would be mad not to use it.

Running PHP through PHP-FPM with Apache 2.2

Running PHP through PHP-FPM is pretty easy in Apache 2.4 (or Nginx for that matter) with ProxyPass, however Apache 2.2 has no built-in convenient way to do it.

There’s a number of solutions that exist to accomplish this, but it seems that the ambiguously named mod_fastcgi.c is the least bad. One of the biggest issues I ran into while setting it up, was allowing for multiple fpm pools for the different virtual hosts, and the specific set of configurations that I’ve figured out allowed me to do this quite easily.

There’s are number of intricate things which must be done to get it working correctly, and while I hope that I nor anyone else ever has to do this again – let’s face the bleak reality, it’s not entirely unlikely that we will… so here’s how we get it going;

  1. Download and compile (yep) the apache module from their website. I’ll spare the installation instructions since they are described in detail in the module files. Ensure you have the httpd-devel or equivalent package installed before commencing.
  2. In your httpd.conf, surround the configuration include directives with the following code:

  3. Inside your virtual host definition file, but before and outside of the definition itself, define the fpm server like so:

  4. Then define it’s use inside the virtual host definition:

Hopefully those configurations are all that you need. As always, different pools will require different names (in this case mine is called prod) and ports. Also note the directories, and ensure they exist – particularly /var/lib/php-fpm and /var/lib/httpd/fasicgi .

Magento Vagrant

Upon being tasked to do a Magento training session at my current company – 4mation, one of the things I wanted to do was to set up a simple standard development environment, which people can easily grab and play with.

Magento being as big as it is, at first this seemed pretty easy – and sure enough, I found around 5-10 github projects of various sorts to accomplish this. Unfortunately, I pretty quickly discovered that they all had some combination of the following problems;

  • An out of date Magento version
  • Very poor LAMP setup
  • Overly complicated and/or (usually causing) VM breaking problems

Having some experience with Vagrant previously, I decided to take the traditional approach with not liking any present projects which poorly accomplish your goal, and simply rolled my own.

After figuring out how to set up a vagrant box, getting everything working on Windows (which is forever and always a major hassle for doing anything productive), and writing a bash file to do a simple yet efficient LAMP setup on CentOS 7, I was up and running pretty quickly.

I aim to do my best to keep this repository updated as Magento itself updates, so I hope others in my situation find this useful!