Sunday, July 27, 2008

Playing with Memcached Part - 1



Reactions: 

One technology I am truly excited about is memcached. One word that comes to my mind when I think of it is mesmerized. I am so excited about it that all I can think of is to modify all famous open source softwares and make them work with memcached out of the box.

This three part series on memcached is brain dump of whatever I know about it. Following is how I plan to divide the information
  • Part one: Answers to what, where, how, who. Advantages. Components. Overall idea.
  • Part two: Component details. Client-Server division. Detailed discussion of BeITMemcached
  • Part three: A simple utility using memcached
Instead of answering what, let me start with where and who.

Where?
Memcached is everywhere :). Every architecture that matters.

Who?
Every site that matters. Facebook, Digg, Twitter, Slashdot.

Now, I come back to what.

So, what is Memcached?
Memory + Caching + Daemon. That's all it is. It is a in-memory process which allows software developers to store data in a giant hash table which can be distributed amongst several machines. It is common to misunderstand how memcached distributes. I will explain it with more detail.

Hmm, what is so great about it?
It is great because it was first to solve a great problem. Its greatness amplifies because it is simple.

What is the problem?
Reason I listed answers for where and who before writing anything about memcached is to insinuate that it is being used by the sites with the highest traffic on the Internet. For these websites scalability is a major concern. They have to service ever growing, ever demanding user base without compromising on QoS.

Disk is the slowest component of the physical architecture of an application. Writes are especially the killers. Look at the following diagram.
basic application architecture
It is the most common client-server architecture. Requests to serve content is coming from the Internet. Application is replicated for load distribution and database is clustered. Database is essentially the disk and hence the biggest bottleneck when it comes to scaling.

One may argue to use in memory tables. Reasons why memcached is advantageous over that

1) In memory tables essentially requires complete structure to be in memory. Some tables might be so huge that they may not even fit in the memory available.
2) No distribution. This results in lots of space left unutilized.
3) Scaling them again requires complex schemes. Master-Slave variants for example. Which after a level is tedious.

Coming back to distribution:
Memcached is so powerful because it is distributed. But unlike databases or common distribution schemes there is no central controller which distributes the data. Also there is no self automated distribution amongst nodes running memcached. Look at the following diagram. Which is wrong in case of memcached
memcached wrongAs I mentioned earlier, memcached's biggest features is its simplicity. Instead of entirely being a server only application, it is divided amongst two components. This is the most important part to understand from developer's perspective.

1) Client: Runs as part of application code. Gets list of servers running memcached server and uses an internal hashing scheme to distribute data evenly.
2) Server: Very simple and concise process supporting commands to store, delete and retrieve data.

Look at the following diagram. This is how distribution works in memcached.
memcached rightThis an important point to understand. Application code communicates with memcached client library. Which generates a hash based on the key passed to it. Based on this key it selects the server to communicate. The hashing scheme should be consistent across all client servers.

Typical application architecture with memcached:
typical architecture with memcachedMost important addition here is a separate process which can be independent from application server. This server can have schemes to persist data on the disk.

Some issues:
  • No authentication support. So servers has to be firewalled.
  • By default supports maximum of 1 MB data as value.
  • By default key length is 256 characters.
Some Links
http://www.danga.com/memcached (origins, FAQs, more links)
http://www.splinedancer.com/memcached-win32/ (latest Windows port)

Friday, July 25, 2008

The Digg Story



Reactions: 

Digg was my favorite website for a long time. Until I discovered Reddit and liked it instantaneously because of broad range of topics and simple user interface.

In a nutshell, Digg is a crowd powered filter for the content on the Internet. People submit stories. Other people vote for these stories. If number of votes become considerable enough depending on various other parameters, story hit the frontpage. Digg is so popular that most of the times it breaks the servers hosting the websites who reaches the frontpage. Must be around three years old, it has already become part of the Internet history.

But this was not always the case. Site's founder Kevin Rose gave his all for creating this product. His girlfriend left him because he used money reserved towards home deposit in developing the website. I can't imagine the trauma he must have gone through. But I also can't imagine the joy he must have had felt when investors knocked his doors for funding the expansion plans.

By August 2006 his net worth was $60 million according to BusinessWeek. This was after 18 months of launch. Kevin quipped that he still have to save money for a couch as site is still not profitable.

Recently, Google offered $200 million for Digg.

Thursday, July 24, 2008

Back to "The Apprentice"



Reactions: 

"The Apprentice" was my favorite show. I was crazy about it. Imagine some of the most successful people. Boasting degrees from world famous schools. Managing multi-million dollar operations and many have net worth in millions, created from scratch. Never been to school entrepreneurs to scientists. Chosen from over a million applicants. But they are all human beings, hence fallible. Especially because all of them want the same thing. It is like civilized version of death tournaments.

But around more than a year and a half back I decided to get rid of cable at home. And for some reason I totally forgot about this series and never downloaded any episodes. Totally randomly, I recalled about it couple of days back and started downloading season five.

I watched first two episodes last night. As soon as one guy mentioned Mensa as one of his achievements, I was laughing. Well, I have my reasons. But I had a feeling that this dude won't last for long. Because of him being a "Mensa" member, Trump chose him to take the lead for the first task. Tarek. That was his name. Rival team had one Harvard graduate lady as project manager.

This is good time to mention Brent. This guy was chosen last when teams were getting picked. Purely because of genetics playing role. He is fat and do not look remotely like other contestants. It is apparent that he reached where he is by lots of hard work and will power. Eugenics anyone? Because of some reasons his teammates never respected him. I would say at no fault of his.

In the first task, Tarek's team lost but not because of his actions but rather decision to not act by one lady in his team. It was apparent that she be fired. Until, Tarek made some really bad decisions. The one that shocked me was him confronting Lee (youngest but strong contestant from morals perspective) that he should not mention Tarek's failure to not guide above lady in the boardroom. In the boardroom, Tarek brought three candidates back including Lee(which was ridiculous), lady in question and one guy who's inaction was justifiable. Tarek was about to get fired because of his own insecurities but unthinkable happened. This lady just couldn't resist keeping silence and Trump fired her and shouted at Tarek "she saved your ass by her own stupidity".

Second task, Lee was leader for his team and Peppy was leader of other team with Brent. Peppy's team fell apart because other members could not keep their peace with Brent and Peppy was a horrible leader. Lee won. Although more credit goes to Lenny. Who in my opinion understood task better. Peppy's team lost very badly. He was fired with a defense attorney who accused Brent of threatening her.

Both Peppy and Tarek made me think how the hell have they reached where they are?

How do you actually define success? and
Are the right people actually at the right places?

Wednesday, July 23, 2008

Lessons from PagalGuy



Reactions: 

I first learned about pagalguy.com from my friend Amit. Who told me that he finds it very helpful and gets lots of information easily, obtaining which otherwise can be tedious. Amit later went to a prestigious school. So, I can infer pagalguy did played some role in his life. And that says a lot about a website's utility.

I got opportunity to meet PagalGuy's founder Alwyn at Startup Saturday (will blog about it in a separate post). Following are some key points I noted from his speech.
  • Started as a hobby
  • Started cause he couldn't find any MBA dedicated message board
  • Hired first employee in 2006
  • Founded in 2002
  • Primary revenue source is online advertisements
  • Looking for investments for creating more services for current user base
  • Have people from prestigious institutes working for them
  • ~15 people team, mostly(read all) engineers or have engineering background
Growth
  • 2002 -> nothing
  • 2003 -> 1000 hits
  • 2004 -> 10000
  • 2005 -> 30000
  • Gone small e :) (exponential)
  • ...
  • 2008 -> 400000 unique visitors per month
And most interesting part.

Why named PagalGuy.com?
Apparently, he just owned it as he uses this name for various online accounts. In a nutshell it is not about how cool your name is. It is about how superior your product is and how well you can sell it.

Later, I had a chat with him. I primarily wanted to know his views on offering stock options to employees. I find it odd that Indian startups have yet to accept offering stocks as norm. I was happy to learn his views on this matter.

Monday, July 21, 2008

Breaking the Singleton Pattern



Reactions: 

I like design patterns. They provide intuitive and reusable solutions to many software design problems. Singleton is the most basic of them. I use it everywhere from creating a Library class which initialize series of application wide objects to threaded queues used by various components within a system to lazy write the data.

In a nutshell Singleton guarantees only one instance of a class. Simple enough. See the following code.



package home;

public class AlwaysSingle
{
private String _name = null;
private static AlwaysSingle _single = null;

private AlwaysSingle( String name )
{
_name = name;
}

public static AlwaysSingle getSingle( String name )
{
if( _single == null )
{
_single = new AlwaysSingle( name );
}
return _single;
}

public String getName()
{
return _name;
}
}


Hmm, can somebody tell me how can I have code coloring in Blogger?
[After writing this article I searched for online code beautifiers. Most of the tools do not perform code coloring. They are mere formatters and others are standalone! I will write one in free time.]

So, this code has private constructor and have a static instance which can only be reached by calling single entry point. Nothing can be done to create more instances. Class is final, so one can't do inheriting tricks.

Reflection. Ring any bells? I still have to see writing security contexts as part of standard development practices. Look at the following code snippet. Pretty straightforward. Here goes Singleton.


import home.AlwaysSingle;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Driver
{
public static void main(String[] args)
throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException
{
AlwaysSingle single = AlwaysSingle.getSingle("me");

System.out.println(single.getName());

Class klass = Class.forName("home.AlwaysSingle");

Class[] paramTypes = {
String.class,
};

Constructor one = klass.getDeclaredConstructor(paramTypes);
one.setAccessible(true);

Object[] ags = {
"you",
};

AlwaysSingle notanymore = (AlwaysSingle) one.newInstance(ags);

System.out.println(notanymore.getName());
}
}


I can automatically retrieve the knowledge I have regarding this class. Sorry for not discussing this code. I am out of time and lazy for that :). I am assuming anyone who cared to read after reflection will be able to figure out things.

Primary Site



Reactions: 

I am spending considerable amount of available bandwidth in developing primary website. Yesterday, I created following header logo. Earlier plan was to have different header for different primary sections but as of now I am sticking to a global header.

Following is the complete layout idea for http://mohit.me

Above header
Below that button bar
Palette: white, black, gray
Font: courier or verdana

Site's landing page will have delicio.us, flickr and this blog's feed with some text.

Button bar will have following sections
Home, Creations, Blog, About, Contact

Home: Takes to the landing page
Creations: Software, presentations, papers, websites I have worked on. Most of the stuff from college but some good material. Will be adding open source and some hobby projects soon as they pick up.
Blog: Link to this blog. Earlier I was thinking of having a dedicated wordpress installation but chosen blogspot because I am familiar with it.
About: Some text about me
Contact: linked-in link, email

No visible footer.

I do not want to use a site-builder. Although that be a viable option as most of this site will be static. Also it will result in much more aesthetic design as compared to what I can create with my limited graphics editing capabilities. But I want to keep things very simple and concise.

Update: I just learned about FriendFeed. It is awesome. I think instead of me writing some widget to parse feeds from different services. I will simply display feed from FF on homepage.

Sunday, July 20, 2008

Woke up by Good News



Reactions: 

I am immensely happy today. For my parents. This note is like a placeholder so that I can come back to it again and again. I am glad with the pace things are falling in place.

Waiting for things to get more concrete and approach unhindered completion.

Zip Zap Zoom!!



Reactions: 

I was contemplating on writing a non-anonymous blog since quite some time. One of the items in my todo was to "evaluate having a public blog". Most updated blog that I wrote was years back on gaming. But it is as good as dead now.

I have a lot to share but I am a major procrastinator and too lazy to jot down things. But one of the reasons to start this blog is that I truly intent to write prolifically. I am fortunate to have people around me who blogs regularly. Which is one of the primary motivating factor.

So, what will I write about?

This blog has no agenda. I will write onto anything and everything which I think I should be writing upon. Opinions, anecdotes, notes, plans, work, interests, dreams, ideas.