Tuesday, December 11, 2007

Video System Upgrades

I recently discovered a defect in our video server code which was causing some servers to use up 100% of CPU cycles. On Friday we released an update to address this issue which dramatically improved our quality of service and the maximum number of viewers who can watch your channel. Here's a little chart of CPU usage over time, which shows the big drop on Friday:


You probably won't notice a big difference (unless you have 1000+ viewers on your channel), but rest assured we'll be ready when you become the lonelygirl15 of Justin.tv!

Saturday, December 1, 2007

Unintended consequences

We recently decided to add events on Justin.tv to search as part of the enhanced site-wide schedule we're working on.

We thought that adding something to search would just require couple pieces of work. We need a new template for the type of search result, and then we need to add events to our search index. Shouldn't be more than a 30 minute project, all told.

Of course, as I begin implementing it, I immediately notice a complication: every other kind of searchable item on Justin.tv can be sorted by page views, in addition to newness and best text match. So now we have to decide whether to either:
  • Special case the sort code and remove the page view sort for events
  • Add a page views counter for events, requiring a small change to the database schema and a small amount of code in several places. [1]
Neither of these options is particularly difficult, but only because we got lucky and the original change was small as well. We decided to add the counter, if only to satisfy our own OCD. The "just a template" change winds up touching 6 or 7 different parts of the site. This is not a fluke, it's typical. New features nearly always require changes you would never have dreamed of prior to implementation.

This is why second system syndrome is so hard to avoid: It's like invading Vietnam or Iraq. At first everything seems perfectly fine, but the deeper in you get the more unforseen complications emerge. Eventually you find yourself under fire from all directions, and you just want to get it over with before you bleed out any worse than you already are.

[1] As a side note, this is why standard databases suck: part of the reason I want to avoid adding a page view counter to events is that it's going to require making a schema change. Of course, I could create a page_views table that solves the problem generically, but that would have been more work to set up in the beginning, when I had no idea I'd have this problem. And because schemas are costly to change in a running system, changing over to that solution would take yet more work now.

Wednesday, November 28, 2007

Reminder: Leah Culver talking at JTV

Leah Culver will be talking tomorrow, 12pm PST at the justin.tv office. A live broadcast of her talk will be available on http://www.justin.tv/hackertv

More details: http://www.facebook.com/event.php?eid=5907866589

Sunday, November 25, 2007

Ruby Shorthand

Justin.tv's web code is written in Ruby. I've found myself using the same collection idioms over and over, so I've abstracted several of them into a file called shorthand.rb.



Most of our web code consists of manipulating collections in one form or another, which is probably why all the shorthand methods are for that. I'm particular proud of my % operator, which I believe is a specialized mapcar in spirit. Without further ado, code!




module Enumerable
def %(field)
map {|o| o.send(field)}
end
end

class Hash
def keys_sorted_by_value(options = Hash.new, &block)
limit = options[:limit] || size
offset = options[:offset] || 0

sorted = map.sort_by do |key, value|
if block
yield(key, value)
else
value
end
end

sorted.reverse! if options[:reverse]

sorted.map {|key, value| key}.slice(offset, limit) || Array.new
end

def hmap
h = dup
keys.each do |k|
h[k] = yield h[k]
end
h
end

def to_params
map {|k, v| "#{k}=#{CGI::escape v.to_s}"}.join("&")
end
end


class Array
def hash_by(field)
h = Hash.new
each {|o| h[o.send(field)] = o}
h
end

def center_first
centered = Array.new
last_pushed = false
each do |x|
centered.push(x) unless last_pushed
centered.unshift(x) if last_pushed
last_pushed = !last_pushed
end
centered
end

def random_subset(n=size)
shuffle.slice(0, n)
end

def shuffle
sort_by { rand }
end

def divide
evens = Array.new
odds = Array.new
each_with_index do |x, i|
if i % 2 == 0
evens << x
else
odds << x
end
end
[evens, odds]
end

def remove!(item)
reject! {|x| x == item}
end

def remove(item)
reject {|x| x == item}
end
end


Other coders: If you have your own idioms like these, I'd love to see them! Post them!

Tuesday, November 20, 2007

Justin.tv a Finalist in the Amazon Startup Challenge

I'm pleased to announce that Justin.tv was selected as 1 of 7 finalists in the Amazon Startup Challenge. From the Amazon press release:

"Justin.tv operates a massively scalable live video platform serving about 500,000 video streams per day. Their custom server software, Python Media Server, has been written from scratch to perform optimally on lightweight Amazon EC2 instances. The end result is that live video publishers have a free, easy to use, and completely scalable platform for hosting any type of live video broadcast."

We will pitch our business head to head with the other finalists for a shot at $100K in prizes and an investment offer from Amazon. The event will be held at Amazon headquarters in Seattle. Wish us luck!


Update: Amazon has posted more details about the finalists to their blog.

Monday, November 19, 2007

Second live tech talk - Peter Seibel

We've lined up our second live tech talk. After Leah talks about OAuth on November 29th, Peter Seibel will be talking about Why Syntax [Does|Doesn't] Matter, on December 13th. Peter is the author of the best Common Lisp tutorial going, and is now working on his second book, Coders At Work.

If you're close to the justin.tv office, drop by for the talk. If not, you can catch it live (and participate in the q&a) at the hackerTV channel. If you don't catch it live, the archive will soon show up on that page.

Saturday, November 17, 2007

Live tech talks

I'm very excited about the latest project we've been brewing at justin.tv: We're going to host live tech talks.

These will be taking place every other Thursday afternoon. If you're in the area, feel free to drop by our office. If not, you can catch the event live online (and if you miss it, there's always the archives!).

Our first speaker will be Leah Culver, of Pownce, who will be talking about OAuth on November 29th. There's a Facebook page with more details.

Between talks, I'll be broadcasting a bunch of videos that will hopefully be of interest to hackers, starting with the awesome Abelson and Sussman SICP lectures. Come watch them, and chat with other hackers on the hackerTV channel page.

If you have any suggestions for speakers, or for videos to show between talks, please email me: bill@justin.tv