The begging on Twitter

What is Twitter? First of all, Twitter is a News Page! It is an interactive News Page with 140 million active users and over 340 million tweets every day. And because it is “interactive“, it is kind of a “social network”, too. But first of all it is a News Page!

But it is not really a social network. I don’t post my vacation pictures on Twitter. Usually I am using Twitter for professional stuff and Facebook for private stuff. And Google+ is a good mix between Twitter and Facebook.

On Twitter everybody goes crazy for “Followers”. I was the same. Then somebody followed me, I followed back. And one day I realized that my Timeline was full with shit! Full with crab that doesn’t interest me. And so I started to unfollow people. And freed myself from the “follow” delusion.

How pathetic it is to tweet: “Please follow me. I follow back”.

Fuck you! Why to hell should I follow you?

I un-followed over 80 people in the last 7 days. And some of them un-followed me, too. And so what? Who cares? I don’t car! And I don’t take it personal. :-)

Don’t take it personal if I unfollow you. That doesn’t mean that I don’t like you. That just means that I am not interested in the topics you are tweeting. Here are the topics I am interested in on Twitter:

  • Tech Stuff (Software Development)
  • Events (Tech. Events, MeetUps, BarCamps, WebMondays, User Groups)
  • SEO

I am not so much interested in Foursquare check ins. Specially not if you are splashing my timeline with 20 checkins in 1 hour! If I want to know where you are, I am following you on Foursquare.

And I am not interested in your vacation pictures. If I want to see your vacation pictures I would follow you on Facebook, Flickr, Lightbox, Instagram or Trover.

What is Twitter?

First of all, Twitter is a News Page! It is an interactive News Page with 140 million active users and over 340 million tweets every day. And because it is “interactive“, it is kind of a “social network”, too. But first of all it is a News Page!

It is the fastest News Page on this Planet. 140 million people are tweeting about everything. If you are walking in San Francisco on the Street and some fire fighters are passing you, then you just have to do a Twitter search to find out which house in SF is burning.

What is a Tweet? 
A Tweet is a Message not longer than 140 characters. Every user on Twitter can submit a tweet. And by default the whole world can read it. A Tweet can be “re tweetetd” and/or replied.

Following
As a Twitter user you can follow other Twitter users, to get their tweets in your Timeline. To follow someone on Twitter is similar to subscribe a RSS channel.

Timeline
Every Twitter user has his Timeline. In the Timeline you see all the tweets from the Twitter users you are following.

Hashtags
A Hashtag is a hash followed by a word. For example “#Java”. That is like a label for topics. You can search for Hashtags. That makes the search easier.

Cron Jobs on Heroku

Heroku is an App Engine for Ruby on Rails … and other Languages and Frameworks. Heroku is running your application and you don’t have to care about hardware or IT-Infrastructure. It is one abstraction layer above the Amazon Cloud EC2.

First of all, there are no cron jobs on Heroku. Because it is an App Engine you don’t have access to the linux os and the native cron daemon or crontab. Forget it! You have to use heroku worker dynos. Keep reading.

Usually you push your rails app via git to Heroku like this:

git push heroku master

and then it will be deployed on x web dynos. There are different kind of dynos. The default is “web”. If you want to do some background jobs you need a “worker” dyno. You can add worker dynos with this command:

heroku ps:scale worker=1

Now you have to create a Procfile in the root of your application. That can look like this:

web: bundle exec rails server -p $PORT
worker: bundle exec rake do_work

The Procfile is defining the different types of dynos which are available for your application. With “heroku ps:scale” you can scale the dyno types. If you want to have 7 web dynos and 1 worker just execute this:

heroku ps:scale web=7 worker=1

And Heroku will immediately deploy your web application on 7 web dynos and your background job on 1 worker dyno. That is fucking awesome!

All right we added one worker dyno and we defined in the Procfile that “bundle exec rake do_work” should be executed on the worker dyno. Now we just have to define the task “do_work” in the Rakefile.

There are different GEMs for scheduling jobs. For example clockwork, rufus and Qu. I prefer a more simple/native way. With a couple lines of code you can write your own scheduler. This rake task here is executing jobs at a given time. In this example every day at 07:00 AM.


task :do_work => :environment do
  puts "START"

  start_hour = 7
  start_minute = 0

  until 2 < 1 do
    now = Time.now
    hour = now.hour
    minute = now.min
    if hour == start_hour && minute == start_minute

      puts " do work #{Time.now}"

      # execute code here !!!

      if Time.now.hour == start_hour && Time.now.min == start_minute
        sleep(60)
      end
    end
  end
end

Just replace the comment with your code. And push it to heroku

heroku maintenance:on
git push heroku master
heroku maintenance:off

That’s it.

Rewriting URLs with Nginx

Rewriting URLs with Apache HTTPD can be pretty ugly. With Nginx it is a breeze. Take a look to this example:

server {
 listen 80;
 server_name my_old_domain.com;
 rewrite ^/(.*) https://www.my_new_domain.com/$1 permanent;
}

This will not just redirect your old URL to your new URL. It passes all the parameter to your new URL, too. If you are migrating a Web Application to new URL, this what you want. All old bookmarked URL are still working with this.

Load balancing with Nginx And Unicorn

Nginx is a pretty awesome lightweight Web Server for Linux. You can use it to deliver static web content. But you can use at as a load balancer, too. If you have a Ruby on Rails Application running on multiple unicorn servers, than Nginx can do the load balancing for the unicorn servers.

Just add the following lines to your “/etc/nginx/conf.d/default.conf”:

upstream unicorns { 
  ip_hash; 
  server XXX.XXX.XXX.XXX:8080; 
  server <IP_ADDRESS>:<PORT>; 
}

XXX stands for an IP Address. You can add as many server lines as you want. The “ip_hash” is not mandatory. This just if you want to force nginx to send the requests from a particular user every time to the same app server. If your Web Application is REST ful and Stateless you don’t need it. Otherwise you should add the line.

Now you just have to add one block for your domain:

server {
  listen 80;
  server_name my-awesome-domain.com;
  location / { 
    proxy_pass http://unicorns; 
  } 
}

And that’s it.

One more thing. If you want limit the file upload size or you have problems with big HTML5 AJAX requests, you should add a little bit more. Here is a more complete example:

server {
  listen 80;
  server_name my-awesome-domain.com;
  client_max_body_size 1M; 
  proxy_buffer_size 128k; 
  proxy_buffers 4 256k; 
  proxy_busy_buffers_size 256k; 
  location / { 
    proxy_pass http://unicorns; 
  } 
}

The Power of Indexes

I am working on VersionEye.com and in the last days we had some performance issues. I added some new data to the database and to the views. The rendering of the package detail pages tooked sometimes up to 5 seconds. And because the UI is programmed with Ruby on Rails, some people told me: “Yes of course, Ruby is slow, you should use Java!”.

Hell, No! That was not the problem. I just added some indexes to the database, so that they fit perfectly to the queries. And now, it is crazy fast :-) Some pages get rendered in less than 80 milli seconds :-)