I recently had the opportunity to attend php[tek] 2014 in Chicago. As Tagged is a site primarily powered by PHP, it was a good opportunity to learn about upcoming features in unreleased versions of PHP, some of the newer features that are underutilized and not as well known, and just how far the language of PHP has evolved since it was released many many years ago.
Tagged : PHP
Where it started
It’s 2 a.m. on a Friday. You have been sitting in a windowless computer lab in the basement of the CS department for almost the entire day. You and your three project partners are not out that night because every single one of you really loves writing low-level C code to optimize that matrix multiplication assignment due next week. You argue about implementation, make design decisions together, pass the keyboard back-and-forth – everyone’s eyes glued to one screen. You’re completely in the zone. You’re in sync. You have flow.
Achieving that flow is one of the hardest problems in the workplace. There are countless theories as to what is the best way to get there. But what if you just tried to do exactly what you did in college? That’s what we did at Tagged, and it worked.
Here at Tagged we use memcached extensively with our PHP layer, primarily to store compressed, serialized PHP Objects. I’ve been working on a project that utilizes Node.js to interact with these objects in memcached and I’ve run into a few issues since the PHP memcached extension compresses objects above a certain size and the Node.js memcached library doesn’t support compression. Fixing this issue was not as simple as just compressing the data before putting it in memcached or decompressing the data that comes back from memcached.
The issue is how Node.js handled strings with bytes above the regular ascii range. The Node.js memcached library used strings to read data coming back from memcached, however when a byte was encountered that was not in the regular ascii range the byte would be converted into an UTF8 error byte sequence. In addition, the memcached flags indicating the data was compressed had to be properly set.
Tip: To avoid problems when dealing with buffers with non-ascii bytes, keep it as a buffer as long as possible. If necessary, the buffer can be converted to a string using the ‘binary’ protocol. However, if something is appended to this string, the non-ascii bytes will get converted to UTF8 error byte sequences. Hence, if something is added to this string, it should be converted back to a buffer and the necessary bytes should be appended.
With this in mind, I helped create this Node.js memcached library to solve these issues. It is useful to anyone who wants to use compression with memcached in node, as there are currently no other memcached libraries for Node.js that support compression. This is also useful for people who want to be able to have Node.js applications that are interoperable with the same memcached stores as their PHP applications.
Let me know what you think and please post any questions you may have! This is just one of the many issues with memcached and Node.js we’ve encountered and solved along the way. As we continue to use these technologies more extensively, we will be able to share more project stories, tips and tricks on how to use them efficiently. Stay tuned.
Nalin DeZoysa is a Software Engineer at Tagged and you can follow this project on Github.