Scaling PHP Applications with Redis

Redis is an important technology when dealing with caching. This was one of the talks that I really wanted to attend at ZendCon. It was given by Josh Butts of Vertive LLC.

Redis is a NoSQL technology that rides a fine line between database and in-memory cache. Redis also offers “remote data structures”, which gives it a significant advantage over other in-memory databases. This session will cover several PHP clients for Redis, and how to use them for caching, data modeling and generally improving application throughput.

Redis is an open source in memory key value store with optional persistance. The current version is 2.4.2 as of this writing. The latest stable version is 2.2.15 which what Josh recommended as the minimum version you would want to use. He noted that 2.4 was pretty stable in it’s own right. Hoever, I have notice that since 2.4.0 release 10 days ago they have already release 2 patches (now at 2.4.2). Thus, personally, unless a feature of 2.4 is necessary I’d say go with 2.2 latest or 2.3 latest.

Once Redis is installed how to connect to it becomes an issue. According to Josh, there’s not a good GUI tool to use with Redis. Instead it’s best to use the command line interface.

On the actual functioning of Redis, it functions on hash keys. Think of it like associative arrays in PHP. However, there’s a huge difference. There is NO nesting. The array is only one level deep.

As far as connecting PHP to Redis there’s two options that Josh mentioned. Rediska and Predis.

Rediska is compatible with PHP 5.2+ and Zend Framework 1.x. A huge feature is the profiler. It allows you to understand how your application is using Redis and optimize it from there. Another positive is Rediska has native session handling.

Predis is built for PHP 5.3+ giving it  the ability to being developed for the future and not being locked in to older technology. It has profiling and lazy connections to the redis server.

Josh’s recommendation between the two was use Predis if you have 5.3 available to you, but Rediska was okay too. It seemed that one of his main reason behind this was Predis has been seeing more recent activity and was thus more likely to be maintained in the future.

My take, at the moment, is also to go with Predis since it seems like a more active project. It also looks as if it is looking forward and being developed for feature redis improvements. I haven’t had an opportunity to actively  test between the two, so my decision is temporary until I have the opportunity to factually compare instead of going on hunches.

HTML5 WebSockets – Presented by Scott Mattocks

This was the first talk I went to at ZendCon. The provided description:

The emergence of HTML 5 and related technologies opens up a new world of possibilities for web applications. Among those new technologies are WebSockets, which allow for bi-directional communication between the browser and the server. This session will introduce WebSockets by exploring a few practical applications before diving into the JavaScript API and the WebSocket communication protocol.

What I gained most from the talk was when he talked about websockets. In a gross generalization, they can be used to replace some common applications of ajax communication between the server and the client. Instead of using long polling in a web application, which has unnecessary overhead when sending small bits of data, using a web socket connection for the communication is a better solution.

Web socket communication requires work on both the server side and client side to enable the two way communication.

On the client side, like ajax, it relies heavily on javascript. Processes information in a similar fashion to AJAX. Web sockets at it’s heart is about the communication of data between the server and client. There’s some work on the client side to allow the setting up and maintaining of the connection, but really less than with a long polling ajax setup.

On the server side it’s a little bit more work. The original request for a connection is sent over the standard http port for the site. Then the web socket server will respond with the port to set the connection up with the web socket server.


More Information:

Overview of Zend Framework 2

The second talk that I attended of the conference was on an overview of Zend Framework 2 (ZF2). Although I thought it was going to go over the Event Driven Actions in ZF2, they changed it due to popular demand.

The ZendCon website gave this description:

One of the most anticipated topics of this year’s ZendCon is what is new in ZF2. Learn about the core features now available, the development approaches they enable, and get a preview of the new MVC layer of Zend Framework 2 in action

Since it was an overview there wasn’t a ton of information presented that I hadn’t read already. What was reinforced for me was the use of namespaces. ZF2 relies heavily on namespaces which is a good thing.

Another key item that was brought up was dependency injection which is a key addition to ZF2. It allows the OO code to reduce the amount of coupling and reliance on other code to function. See this very good slideshare on the advantages on dependency injection if you’re not quite sure what it is.

In general, this was an overview of the ZF2 functionality. It was really nothing more than a brief summary of the topics on the “How to start” page.

Overall it was a good overall presentation if you wanted an overview. If you were looking for a more in depth presentation, like I was, it wouldn’t fit your needs.

MySQL 5.5+

The MySQL talk was given by Dave Stokes of Oracle. He’s the MySQL community manager. The ZendCon summary:

MySQL had been evolving since the Oracle take over in 2010. This session will cover how new features like multi-threaded replication, InnoDB-Memcached plugin for NoSQL data access, or having InnoDB as the default storage engine will change life for PHP developers. Learn how partitioning and new performance metrics will allow allow better access speed and more control over your MySQL instances.

This talk was about the improvements with MySQL since the take over by Oracle. Many in the community feared the Oracle take over, but in reality it has been good for MySQL.

They are adding huge features to MySQL

  • Massive improvements for MySQL on Windows
  • RTMF method to setup replication
  • More secure
  • Innodb improvements
The key feature I was concern/interested in was the InnoDB improvements. Something I ws unaware of since Oracle bought MySQL they brought together the InnoDB team and MySQL Team leading to much tighter integration and quicker bug fixes. It’s also leading to adding features to speed it up.
The new feature I was unaware of was the creation of nosql-style memcached api access of the data. I haven’t had a chance to use it in practice but in theory this seems like an awesome step for MySQL.
On security, MySQL is doing more to protect/ensure data durability. Meaning that the data will store correctly and be retrived each time. Hoever, this does not replace developers escaping input. He restated again and again, “Escape your data”, “Escape your data”, “Escape your data”. As a developer, do not trust users. Simple as that.

I was able to connect with Dave and get the slides. You can download them below.

Download slides -> MySQL update Zendcon

The Contextual Experience of the Mobile Web

The contextual experience on the web was a talk by Jeff Carouth of Texas A&M University. The ZendCon summary:

The great native apps vs mobile web debate will live on for a little while longer, but more and more we are realizing that we can get the best of both worlds with mobile web apps. However, there is an expectation of context on mobile devices, and ignoring that experience expectation is a mistake. Let’s look at the contextual experience of the mobile web.

My summary would be history of developing for a mobile client and the emergence of adaptive design as a solution.

One of the interesting points that he brought up was something that had been going around my office lately as well.

What determines a mobile client?

Meaning an iPad and netbook have the same resolution, which one is mobile? Both? iPad only?

Within CSS3 theres specification for media queries. One of these queries that we use is to base the design on resolution. In doing so you are basing the display on the size of screen. At the outset this seems like a great idea. However, there is a drawback. The design to fit the size doesn’t account for data size. You could design a photo heavy site to fit in a 600 wide window but if you don’t compensate for data size performance on a 3G network user experience will still suck. So while media queries are part of the answer they are not all of the answer.

To fix the second part, one option discussed was image compression or replacement. For compression there’s JS resizing. It works but it is ugly. You look at that code and the processing overhead and cringe.

Then there’s responsive images, downloading a small image and replacing it with a higher quality one. This adds overhead and bandwidth. You’re download two images for every image on your page. Not ideal.

Ideally this should be done on the server side. The technology and/or programs haven’t been written for this yet. You’d need to inspect the client agent and then respond with the appropriate image size and resolution. According to him, there’s not a good implementation of this at the moment. However, I think there’s vast opportunity for someone to develop such a tool.