2012
07.27

## How many url’s can you shorten?

I mentioned in the last post that I’ve been doing a programming project for a few months.  One of the things that we found necessary was url shortening.  You know, that’s where you take a web address that looks like this:

http://www.slate.com/articles/sports/fivering_circus/2012/07/london_olympics_viewers_guide_slate_s_day_by_day_cheat_sheet_for_the_2012_games_.html

and turns it into something that looks like:

http://sobr.org/aF

This is beneficial for services like Twitter, that have limited content space. You start with “/0″, then go to “/1″, “/2″ and so on until you get to “/Z”. Then you flip to “/00″, “/01″, “/02″ and so on. When you get to “/ZZ”, you flip to “/000″. You get the idea.

Anyway, I started thinking about it today and wondering how many possible url’s we will be able to shorten with that scheme.

We use this character set:  0-9, a-z, A-Z, for a total of 62 possible characters. So, the math is 62 ^ x, where x is the number of character spaces in the shortened url.

Here’s the result:

1 chars: 62
2 chars: 3844
3 chars: 238328
4 chars: 14776336
5 chars: 916132832
6 chars: 56800235584
7 chars: 3521614606208
8 chars: 2.183401055849E+14
9 chars: 1.3537086546264E+16

So, by the time we get through 6 characters (http://sobr.org/ZZZZZZ), we would have shortened a total of 57,731,387,490. I’d say 57 billion url’s is probably gonna last me the rest of my life.

Here’s the php script that runs the calculation:

``````
\$charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
\$charcnt = strlen(\$charset);

for( \$i = 1; \$i < 10 ; \$i++ ) {
echo "\$i chars: ".pow(\$charcnt, \$i)".\n";
}
``````

2012
07.10

## Feed aggregator programming travails, part 1 (rssCloud).

**I know most of my blog readers are not programmers, but this type of thing still might be interesting in a theoretical way.

I’ve been writing a news feed aggregator as a side-project for a few months now and I’d like to share some of what I’ve learned. In particular, the most recent change that I made was implementing support for the rssCloud protocol. This protocol has been around for a long time, and it’s purpose is very simple: provide a way for apps that generate rss feeds to “ping” the readers of their feeds when the feeds add new items. Think of it as paleo-twitter. If you write aggregator code, you should definitely implement it. It makes the web a better place.

While the concept is simple, the implementation ended up being quite a chore. This was mostly because my aggregator is running on an Amazon Ec2 micro-instance (600MB of ram, 8GB hard drive, 1 cpu core). That’s a very limited environment when you’re already running a MySQL database and a copy of Apache. The system currently has close to 700 feeds in it, of which roughly 250 have a cloud tag. When you drop in the potential for 250 people to “ping” you at any given moment, you have to be able to handle that without crashing. But, no matter how you slice it, a micro-instance can’t handle that. It was this issue that took most of the planning.

What was needed was to spread the incoming traffic out as wide as possible, and also keep ping subscriptions to only the minimum needed to satisfy users. I think I mostly achieved that, although time will tell.

So, here’s the high points of the implementation as it’s running now:

1. Only subscribe to cloud pings for feeds that have been updated within the last 14 days. There’s no reason to keep re-registering every day for instant updates to feeds that rarely update. Once the feed updates, we’ll subscribe to it again.
2. Only subscribe to cloud pings for feeds that have a subscriber count greater than zero.
3. The cgi handler for incoming pings simply flags the feed as “updated” in the feed table. No other processing is done during a ping.
4. A scanner runs once per minute in a cron job and grabs fresh content for any feeds marked with the “updated” flag. It then un-flags the feeds it updated. We use locking to make sure the scanner doesn’t run twice over top of itself.
5. A feed’s cloud ping subscription is renewed only if the “last rsscloud reg time” is greater than 20 hours.
6. Renewals are randomized within a four hour window to ensure that we don’t have a large batch of incoming subscription challenges all at once.

All of this means that incoming pings are effectively queued and then handled the next time the scanner gets a chance, which is normally once per minute. In this context I consider a one minute ping time to be more than acceptable. It might not be as fast as something like the twitter API, but it’s pretty darn close.

I’ll post more as I learn more. And this isn’t meant to be a how-to. It’s just a log of what I’m learning and think might be of value to others wanting to write an aggregator.