Categories
English Optimization & Speed

Web App Development and Caching

Any web developer who works with external services or databases (that’s probably almost every web developer) has probably run into performance problems. The problem is that running code by itself is pretty fast. Databases and external services / APIs are very slow. Waiting on an external API to load is basically the computer equivalent of waiting for a brontosaurus to walk a kilometer.

As web developers, we have a very powerful tool called caching. It’s being used in your computer reading this sentence every microsecond, with various levels of caching happening between the CPU, memory, and hard drive (or SSD). The act of caching is saving the result of a slow operation in a easily accessible space. In this case, we will be talking about caching database results and API results.

There are only two hard things in computer science: cache invalidation and naming things.

-- Phil Karlton (Adapted)

Cache invalidation is a hard problem. Let me illustrate:

  1. Server A fetches Record A and associated records from the database, and caches it.
  2. Server B updates Record A.
  3. Server A continues serving its cached copy from step 1 (until it expires).

There are a few ways to solve this problem.

You can manually invalidate caches:

  1. Server A fetches Record A and associated records from the database, and caches it.
  2. Server B updates Record A, notifying all servers to remove cached copies of Record A.
  3. Server A continues serving its cached copy from step 1 (until it expires).

This is tenable with one or two cache servers, but clearly not scalable — you’ll need to send cache purge requests to all your cache servers.

Then there’s my favorite — key-based cache invalidation:

  1. Server A fetches Record A, and looks up the cache key “Record A [timestamp when Record A was updated]”. It doesn’t exist, so it fetches associated records and stores everything in the cache.
  2. Server B updates Record A.
  3. Server A fetches Record A, and looks up the cache key “Record A [timestamp when Record A was updated]”. It exists, so it serves the cached copy.

This method has some drawbacks – it still requires one query to the canonical data store, and you need to remember to update the updated_at attribute of your record when any associated records change. If you’re using Rails, this is trivial:

class MyRecord < ActiveRecord::Base
  has_many :associated_records
end

class AssociatedRecord < ActiveRecord::Base
  belongs_to :my_record, touch: true
end

Another drawback is that your cache is going to be full of old keys, when a record is updated. Luckily, there are caches that already deal with this! LRU, or Least Recently Used, is a cache eviction policy that removes the least recently used records first, making room for new records. Redis can be used as a LRU cache and Memcached is sort of LRU. The Rails Memory Cache Store also uses a LRU algorithm.

“Caching sounds great! How do I use it?”

Caching is not something that you should “tack on” to an app. There are awesome tools, such as Varnish, that are based around this concept, but it is not ideal. The ideal web application will be designed from the ground up with caching in mind — even in the development environment. If you’re writing tests, make sure your test environment is connected to a cache, then test cache invalidation and lookup. Ideally, you should use the cache you use in production in both development and test environments.

Categories
App.net English Open Source Projects

App.net Object Sync

I recently released my first app on the Mac App Store, Toki, and I decided that talking about the inner workings of the sync mechanism I’m using would not only be interesting, but helpful for me to think about some of the problems I’m having[footnote]This is inspired by the “Vesper Sync Diary” series of blog posts by Brent Simmons[/footnote].

Toki uses an App.net sync mechanism that I’ve been thinking about and working on for a while — I thought that Toki would be a good low-profile app to test out some ideas in real-world use.

Although Toki is a production app, the sync portion is not feature-complete yet. I’m working on ironing out the basic bugs (planned to be released in 1.0.1 and 1.0.2 releases), before moving on to the more advanced features. My “end goal” for the App.net sync mechanism is an open-source multi-platform library that anyone can use in their own app to use the App.net backend to keep their objects synchronized[footnote]There already has been some work been done with a Ruby-based command-line client.[/footnote]. From now on (and until I think of a better name), I will call this sync mechanism “App.net Object Sync” or “ADNOS”.

ADNOS uses the private message / channel feature of the App.net API, using a channel for the app namespace, and a message representing each object.

There are two fundamental steps of the sync protocol (as of version 1.0.2, still in development):

  1. Pulling remote changes: ADNOS pulls all new messages from the channel, processing them one by one, creating or updating local records as necessary. If the record is deemed “duplicate”, it will overwrite local changes[footnote]Conflict resolution is not applicable in the context of Toki, but is high on the list of features I want to implement.[/footnote]. If the record was newly created in the local database, ADNOS will also record the App.net Message ID in the local database.
  2. Pushing local changes: ADNOS makes a new message post for each record that does not have a App.net Message ID recorded in the local database. When the message is successfully created, the message ID is set.

This has some limits, some inherent in the protocol, and some inherent in the App.net backend:

  1. Message creation is rate-limited to 20 messages per minute[footnote]This is also why Toki works with such low-resolution data.[/footnote].
  2. Each message is limited to 8192 bytes. Less, actually, because of serialization and metadata.
  3. Recreating the database requires a full playback of messages — this takes time, especially on databases with more than 200 objects.

These limits are pretty much unavoidable. I work around the rate-limiting problem by queueing sync up requests, and pausing the queue when X-RateLimit-Remaining gets dangerously low. The queue is re-started after X-RateLimit-Reset elapses. Any app that plans on using ADNOS will want to be aware of these limitations before implementing it.

And, now for the features I’m planning on implementing (in no particular order):

  • Proper conflict resolution.
  • Streaming support for pulling new changes.
  • Updating and deleting previously synced items. This isn’t implemented yet, but should be relatively easy.

I’ll be writing more as I progress. I hope to get some sort of open-source library out there in the near future.

Categories
English Uncategorized

Kerbal Space Program – Asteroid Redirect Mission

Kerbal Space Program — KSP for short — is an incredibly addictive game about… Space exploration! In the game, you are in charge of the space program on planet Kerbin. Kerbin is located in a solar system quite similar to our own solar system, with a few differences. I’ve been playing this game for a few months, and finally decided to write a blog post about my experiences and thoughts.

Jeb on Mün

Note that KSP is still being developed with regular updates. Consider it alpha software — feature set is incomplete, and there may be bugs along the way. Regardless, KSP has fostered a very strong community of fans, players, and even modders. I haven’t developed a mod / plugin for KSP myself, but it is architected to be easily extensible. My favorites are MechJeb – autopilot and maneuver assistant and Kethane – in-situ resource utilization, but there are many more to choose from.

As of version 0.23.5, there are two modes of play – “Sandbox” and “Career”. Career mode is relatively new to the game (since 0.22), and is still being heavily developed. The point of the game in Career mode is to build rockets, use those rockets to send Kerbals to space, and gather science! In return for these science points, you can unlock additional parts to make bigger and faster rockets.

Rocket Science

When I was a kid, I often enjoyed constructing and flying model rockets with my father and brothers. I never understood the phrase “it’s not rocket science” — I thought building rockets were relatively easy. Make an aerodynamic body, attach the motor, and light the fuse!

I was wrong. I can’t begin to start on how many ways I was wrong. KSP taught me not only where I was wrong, but how wrong I was.

New words, words I have never heard before, words like “periapsis” and “apoapsis”, became not only familiar, but required knowledge. I realized that rocket science is not only about building rockets, but also about controlling them.

Periapsis. Apoapsis. Hohmann transfer. Plane-change maneuver. Gravity assist. The Tsiolkovsky rocket equation. Specific impulse. ΔV.

These words all mean something in KSP. You can play the game without really knowing about them, but if you want to get good, you probably should.

I wish I had this game when I was a child, not only because it would’ve been great fun, but also because it would have taught me the importance of physics and mathematics from an early age. In elementary school, our teachers would always tell us that math was very important! But they didn’t have any concrete examples. KSP changes all of that. NASA has already seen the potential in this game — the latest version was actually a collaboration between the KSP developers and NASA. While entry into the classroom may be difficult, I believe that this game has incredible value for children. How many times have you heard “<insert product here> makes math fun!”? KSP is one of the few games that actually makes math fun.

And now, here are some photos of me “researching” and “preparing” for this blog post 🙂

Categories
App.net English WordPress

App.Net Comments Widget on WordPress

I noticed that most of the good discussion I was having about blog posts were on App.net. Turns out, many other people feel the same way, and there’s even an official App.net comments widget!

I’ve been testing a WordPress plugin to replace the default WordPress comments with ADN comments on this site for quite a while, so I decided to publish it in the WordPress Plugin Directory. Just search for “ADN Comments”, and it should be the first result.

ADN Comments

Installing this plugin will completely replace WordPress comments. You won’t even be able to see them from the admin dashboard — the data is still there, but just hidden. If you don’t like ADN Comments, just disable the plugin and all your old comments will reappear.

If you’d like to display your username as the default “reply-to” username, please set your App.net username in the “Contact Info” section of your profile.

WordPress Admin => Users => Your Profile => Contact Info => App.net Username

Source code is available under the GPLv2 (or later) license. Code contributions, bug reports, etc, are welcome at the GitHub repository.

Categories
English Nginx Optimization & Speed PHP WordPress

WordPress Meetup Tokyo — WordPress Server Optimization

I recently gave a quick talk about how I use Nginx, HHVM, MariaDB with WordPress on this blog at the March WordPress Meetup in Tokyo. Here are the slides:

I’ve published a Vagrant template for the setup detailed in the slides.

Categories
English Nginx Optimization & Speed WordPress

SSL and SPDY

In my quest for faster response times and page load speed, I’ve been playing around with Google’s SPDY. I finally got around to getting a SSL certificate for this website and installing the latest version of the SPDY module for Nginx.

WordPress, alone, doesn’t really support SSL on all pages out of the box — here are some extra things you probably want to implement.

  1. Redirect all non-HTTPS traffic to the HTTPS server. For example, this is what I use:
    server {
      listen 80;
      server_name keita.blog;
      return 301 https://keita.blog$request_uri;
    }
    
  2. Use HTTP Strict Transport Security:
    server {
      listen 443 ssl spdy;
      ...
      add_header Strict-Transport-Security "max-age=31536000";
      ...
    }
    

    Using HSTS has the benefit of letting the user agent know that all requests should be using the HTTPS protocol for this domain. This is important because some WordPress plugins and/or themes will prefer to use HTTP, even though the connection is HTTPS. I had a problem with some AJAX functions and Jetpack’s Infinite Scroll.

  3. (Optional) Install a HTTPS plugin. Not required, but it might help some problems with non-HTTPS content domains, et cetera.

Categories
English Nginx Tools WordPress

WordPress on Nginx, HHVM, and MariaDB

Update 2014/4/24: I’ve updated the template to work with the latest HHVM 3.0+, and also squished some bugs.

I’ve been talking quite a bit about WordPress on HHVM recently, and it’s gotten a bit of attention, so I decided to open-source my Vagrant setup for running WordPress on HHVM.

I originally made this Vagrant setup to test HHVM on my local machine before deploying updates to the theme, plugins, etc.

Have fun, and please ping me if you have any problems!

GitHub link

Details

Categories
English PHP WordPress

HHVM and WordPress

Update 2014/4/17: This site now runs WordPress 3.9, which seems to be working fine with HHVM. Also, compatibility has improved, thanks to a patch in the WordPress core specifically for HHVM.

I recently posted about how I switched out PHP-FPM (PHP’s FastCGI pool) for HHVM. Today I’ll be talking more about the install process on the server, and using it to set up WordPress.

What is HHVM?

This is usually what happens in a (successful) startup[footnote]Twitter is (in)famous for its use of Ruby on Rails, which eventually had to be re-written in Scala, a language that runs on the JVM.[/footnote]:

  1. Initial codebase is written in an interpreted language, trading agility and developer productivity for slower response speeds and increased CPU usage on the servers.
  2. The service sees new users coming in and the initial codebase becomes strained.
  3. The service scales up the number of servers, but hits a wall.
  4. The codebase is re-written, either wholly or partially, in to a more performant and efficient language.

Facebook did steps 1 to 3, but refused to do step 4. Compiled languages, while having strengths in performance and efficiency, are usually harder to program in than interpreted languages. So, Facebook made something called HipHop for PHP. HipHop for PHP, or HPHPc for short, compiles PHP into C++, then compiles that into a big binary file. This allows PHP developers to do what they do best — develop in PHP, without worrying (too much) about performance.

As Facebook grew even more, HPHPc began to show its limitations[footnote]Compile time, compiled binary size, lack of MARKDOWN_HASH0f953f8e8cc058300e5041e6f079ab63MARKDOWN_HASH and MARKDOWN_HASH346e614b0b5901e8cdf0ca89b1b3950cMARKDOWN_HASH support, to name a few.[/footnote].

Out of this, came HHVM. HHVM is short for HipHop Virtual Machine, and uses a Just-in-Time (JIT) compiler to run PHP code. The main benefit developers do not need compile the whole binary every time they want deploy new code. With HHVM, Facebook has also worked towards full PHP 5.5[footnote]Wow HHVM is fast…too bad it doesn’t run my code[/footnote] support, support of create_function() and eval(), enabling the majority of PHP application frameworks to work with HHVM.

This includes…

WordPress

WordPress is written in PHP, and runs very well on HHVM. Previously, getting WordPress to work on HHVM required some patches to the core code[footnote]Getting WordPress running on HHVM « HipHop Virtual Machine[/footnote], but these issues have been resolved.

I’ve been running this blog on HHVM for about a week now, and response time is consistently 10x faster than before — 100x if using something like Batcache.

Installation

This server is running CentOS 6.3. While there are packages with HHVM pre-compiled, these packages are not officially supported by Facebook, and are usually compiled by individuals. Your mileage may vary, but package conflicts prevented me from installing any pre-compiled HHVM packages — so I had to compile it myself.

If you plan on running a server with HHVM from scratch, I highly recommend using Ubuntu, and the official precompiled packages for Ubuntu 13.10 / 12.04.

Bugs / Things to watch out for

Because HHVM is not PHP, you may occasionally run into unexpected behavior. Here are some problems I had while setting this site up:

  • No MySQLi support — this means tools like phpMyAdmin and database backup plugins that rely on MySQLi won’t work. Edit: HHVM 3.0 and above have MySQLi built in.
  • In the WordPress “General Settings” area, setting “Timezone” to something like “Asia/Tokyo” caused WordPress to crash. The “UTC +/- (hour)” settings seem to work fine.

That’s all, for now. When I run into more problems, I’ll make sure I post about them (with a workaround, if available).

Categories
English Tools

Japanese Input in Sublime Text

I really like Sublime Text. Today, I like it even more. Thanks to a blog post by Uesugi Shu, this long-open bug regarding Japanese character input has a viable workaround!

The link is in Japanese; I’ve (roughly) translated the steps required to achieve the same result below:

  1. Open the Sublime Text keymap file. This is at ~/Library/Application Support/Sublime Text 3/Packages/Default/Default (OSX).sublime-keymap — Sublime Text 3 didn’t let me edit the file in-place, so I had to use a different text editor.
  2. Find the following code blocks, and comment them out:
    // Find panel key bindings
    { "keys": ["enter"], "command": "find_next", "context":
        [{"key": "panel", "operand": "find"}, {"key": "panel_has_focus"}]
    },
    
    // Replace panel key bindings
    { "keys": ["enter"], "command": "find_next", "context":
        [{"key": "panel", "operand": "replace"}, {"key": "panel_has_focus"}]
    },
    
    // Incremental find panel key bindings
    { "keys": ["enter"], "command": "hide_panel", "context":
        [{"key": "panel", "operand": "incremental_find"}, {"key": "panel_has_focus"}]
    },
    
  3. (Optional) Comment out the following to fix kana-kanji conversion while typing regularly, as well:
    { "keys": ["tab"], "command": "insert_best_completion", "args": {"default": "\t", "exact": true} },
    { "keys": ["tab"], "command": "insert_best_completion", "args": {"default": "\t", "exact": false},
        "context":
            [
                { "key": "setting.tab_completion", "operator": "equal", "operand": true }
            ]
    },
    
  4. Re-open Sublime Text!

Categories
English Meta

Moved… Again!

So, I’m making some changes to this blog.

I’ll start with the first, which is most obvious. I’ve decided to change the theme I’ve been using for 2 years to Expound by Konstantin Kovshenin.

Second, which may (or may not) be so obvious. I’m running this site on NGINX (blog post), but I’ve switched out PHP-FPM for Facebook’s HHVM. Based on my preliminary benchmarks, response time is around twice as fast, and throughput has increased ten-fold. I will write a more comprehensive post about using HHVM in the near future.

The third is quite long, so bear with me:

I’ve been thinking recently, and in the first two to three years of blogging, I have started three blogs with more than three posts. This blog, which was previously hosted at http://keita.flagship.cc/, has always been my “main blog”. The idea of this blog from the start has been to make a repository of mistakes I’ve made, hopefully helping someone else who has run in to the same mistake later in time.

This core concept, will remain unchanged. I will continue to help people by writing about mistakes and problems I have made along the way. What I am changing, is the content on this blog.

Recently, I have become infatuated with the space-simulation game Kerbal Space Program. This infatuation has led to a re-kindling of my childhood desire to go to outer space — while I may never have the privilege to see our Earth from outside its atmosphere, I want to do whatever I can, to help the advancement of our civilization to a true space-faring civilization.

There are many hurdles humans face before this goal can be achieved, and we’re going to need as many people working on problems as we can manage. If just one person is motivated by reading about what I have to say on the issues that are preventing us from advancing as a civilization, I will be satisfied — the greatest obstacle we face may very well be ourselves.

Of course, I will still be writing about programming problems and solutions I encounter in my daily life and work — don’t worry!