Kohana Virtual Hosts

If you intend to run multiple Kohana applications from the same code base, the separation of the system & modules directories from the application and it's themes is the standard way to go.

For most shared applications you'd develop the functionality and it's views as separate individual modules, while the only thing the application directory needs to hold are configuration files for separate databases and the theme-specific views of which there may only be one or two along with the cache & log directories.

The other overhead is maintaining Apache virtual hosts, setting up mod_vhost_alias which may not be possible if you don't rent a VPS or dedicated server. By making a few small modifications to your Kohana index.php file you can keep all your applications in a single directory and cut down the amount of admin stuff you have to do.

$kohana_application = $_SERVER['SERVER_NAME'];
if( ! file_exists($kohana_application) )
{
	$kohana_application = 'default';
}

If you have a dedicated IP address you can just point new domains to it and presto! it'll work as long as you have an application directory with the same name as the domain name. An example directory structure could be like:

kohana/                   # Kohana SVN trunk
modules/                  # Application modules
root/                     # Web root
|-index.php               # Kohana dispatcher
|-@harry2.example.com/    # Symlink to harry.exmaple.com
|
|-harry.example.com/
| |-cache/
| |-log/
| `-views/
|   |-header.php
|    `-footer.php
`-admin.example.com/
  |-cache/
  `-log/
    .. etc.    
Skip to Page:  1 2 3 … 8

Powered by SimpleCDN

With a little bit of persuasion by the YSlow plugin for FireBug I took the plunge and decided to try out SimpleCDN.com with the 15 free credits.

Although I use nearly very few images on my website, having an alternate server local to the user should speed things up even more and integrating AutoCDN with your site is just a matter of prefixing your links.

Considering most of my images are under 1kb each, and the stylesheet is a meager 11k, it would allow me 150 similar items to be hosted which fall in the under 100kb category in their pricing structure. With free updates to existing files of the same size it's a win-win situation; I presume the updates are free as long as they don't go into the next pricing category which leaves you some leeway for small differences in size.

World Coverage

With SimpleCDN being a relatively new startup and no concrete information on their website about their I can only presume they are US centric at the moment, based on traceroutes from the UK, USA, Australia and India they are peering with resisoft.com which has some pretty good links with HE, Teleglobe, GlobalCrossing and all the other usual suspects.

This is a disadvantage compared to other CDN services which provide truly global distribution with local bandwidth in Europe or even bandwidth starved countries that matter like Australia.

Account Management

Simple CDN Account Management screenshot

All I can say is the management interface is painful to use, iframes within iframes and multiple scrolling areas on the page at any time (often within each other) makes it very distracting to find the information you need; on top of this it's very very AJAX heavy.

Not to mention the whole management interface is designed for a widescreen display at presumably 1440 pixels or wider, so those of us using only 1280x1024 have to scroll around and are greeted with an strange page that's only half there or where the menu consumes more than a third of the screen space available.

The final result (even at full screen) is a user interface which I need to scroll around to get basic things done, with page tearing at slow frame rates.

Conclusion

While it's extremely cheap for small images such as avatars, CSS stylesheets, Javascript or other small files with very reasonable pricing for larger files if you just need to offload bandwidth and static files, the lack of global coverage and poor web admin interface equate it to being a feature-rich scalable file hosting service trying to play with the big boys.

Don't get me wrong, there are lots of cool features, but my original aims were to find a global content distribution service, so with the low price you'll have to compromise.

Skip to Page:  1 2 3 … 8

Flexible PHP Interfaces

While working on converting old and flaky code to our current framework, making it more object orientated, with less duplication and easier to understand and use I thought I'd cover a few things which to me make classes, methods and interfaces "Programmer Friendly".

1. Doc Comments

You might be in a hurry, but documentation comments can speed up development to an order of magnitude. With most advanced PHP editors such as PDT supporting JavaDoc style comments and subsequently type hinting from them for auto-completion, taking a few extra seconds to add these annotations is well worth it.

interface BelongsToCompany {
  /**
   * @return Company
   */
  public function company();
}

2. Flexible Parameters

Always require the minimum amount of data from the programmer to run the function. For example if you only require the `Company` primary key to run some queries, the only require the primary key as a parameter.

However it can be useful to convert known input types into what you want, allowing the programmer to pass a Company object or a User object (which belongs to a Company) as the parameters and to get the company ID from that.

class Something {
  /**
   * @param integer|Company $company_id
   */
  public function staffCount( $company_id )
  {
    if( $company_id instanceof BelongsToCompany ) {
      $company_id = $company_id->company();
    }

    if( $company instanceof Company ) {
      $company_id = $company_id->id;
    }
  }
}

3. Use method Entry contracts

Wherever you implement an interface method, and generally any method, add input contracts using asserts() for more robust code and faster less obscure error feedback to other programmers. A fine balance is needed between allowing flexible parameters usually setup above the entry contract, and ensuring that the main body of code only runs with the correct types of parameters.

I'm including this under the topic of flexibility because these provide safeguards to let the programmer know when then underlying function cannot handle or automatically convert the parameters.

Some situations would be:

  • $parameter is a number
  • $parameter is an instance of SomeClass or SomeInterface
  • If $param1 is passed, ensure $param2 is there too
  • Ensure $parameter is not empty

However, avoid using `is_int` or `is_bool`, or other underlying functions which query the underlying ZVAL structure in entry contracts; instead use the `ctype_*` functions or type-casts first, then check that the values are within expected ranges.

/**
 * @param integer $p1
 * @param boolean $p2
 * @param string $p3
 */
function testContract( $p1, $p2, $p3 ) {
  $p1 = (int)$p1;
  assert( $p1 > 0 );

  // Boolean is implicitly within range
  $p2 = (boolean)$p2;

  assert( !empty($p3) );
  assert( strlen($p3) < 100 );
}

4. Be Stateful and Refactor

One of my major gripes are groups of 'stateless' static methods which all accept a common parameter. These should be flagged early on for re-factoring as they make your classes brittle and inhibit changes in future. Common places where these pop up are when procedural code has been hastily converted into classes.

class Company {
  public static function getUsers( $company_id ) {}
  public static function getPeople( $company_id ) {}
  public static function getCustomers( $company_id ) {}
}

One of the problem factors here is that the programmer is required to carry the objects state around wherever it's being used and implicitly pass it when calling the methods. A particularly bad example would be:

$users = Company::getUsers( $this->company->id );
Skip to Page:  1 2 3 … 8

Amazon S3 as a Content Delivery Network

It seems that Amazon S3 is perfectly situated to act as a content delivery network, competing with the likes of Akami as long as you don't mind getting your hands dirty.

S3 supports your own domain names when setup as a CNAME with a bucket of the same name, but requires you to manually synchronize the files that it hosts.

On the cost side, other providers such as SimpleCDN boast one-off fees per file hosted with no additional bandwidth or storage charges and while Amazon S3 comes a close second with it's low fees it still doesn't support some features we need.

Automatic Sync

Yes, a big feature that S3 doesn't have - automatic syncing of image files from your website to S3, at least not in a fully transparent web-hosting friendly way.

Tools exist such as S3Sync to find modified files and upload them to S3, however these usually require more access than the majority of webhosts provide and aren't available on Windows or <your J2EE container of choice>.

SimpleCDN provide 'AutoCDN', which by prefixing your images with http://dl<your-account-id>.simplecdn.com/autocdn/<your-autocdn-folder>/ their servers will cache your images and serve them via their distributed CDN without any intervention required.

To add insult to injury, Amazon S3 doesn't support FTP upload, although this capability could be added relatively easily with third party software (and it's a project I'm considering) it seems that Amazon are by far more targeted at developers, narrowing the selection of people who might want to adopt it.

Pricing

Taken directly from SimpleCDN's pricing page for a single 85 KiB image serving 50,000 hits a day for a year.

CDNBandwidthStorageUpload BWService FeesTotalCompared
SimpleCDN$0.00$0.00$0.00$0.10$0.10-
Amazon S3$192.32$0.00$0.00$0.00$192.32+ $192.22
Nirvanix$266.29$0.00$0.00$0.00$266.29+ $266.19
Limelight$547.37$0.00$0.00$0.00$547.37+ $547.27
Akamai$1,449.80$0.00$0.00$0.00$1,449.80+ $1,449.70
CacheFly$269.85$0.49$0.00$3,588.00$3,958.33+ $3,958.23

Although this comes from the providers own data and is presumed to be at least a little bias, their pricing structure for small or even large images really blows the competition away (Amazon have since reduced the cost outbound bandwidth to $0.10 per GiB, reducing the total to $148).

Disadvantages

Personally I wouldn't consider Amazon ready for mainstream content distribution, especially if you have a large customer base in Australia or generally Asia.

  • It's not transparent to developers
  • No Asia or Australia coverage
  • No video encoding services

With the exception of additional data-centers which are largely in Amazon's hands you can implement the others, but it requires developer time and additional costs (your own servers or Amazon EC2) for video conversion and/or hosting.

Skip to Page:  1 2 3 … 8

About

Harry is a professional developer and sysadmin from London, UK.

He's an atheist, unemployed, a socialist and has a pragmatic outlook on life, love and religion.

Bookmarks

I'm constantly finding interesting stuff, here are some of the things I've bookmarked recently:

HarryR on Faves.com