Saturday, November 22, 2014

Configure Nginx for load balancing


Recently, in our project, I was asked to configure Nginx for load balancing as our app was becoming slow, owing to high traffic. I haven't worked much with Nginx, so I was a bit apprehensive at first. As it was Diwali holidays, nobody else was there in office and I had no choice. However, on fumbling around, it turned out to be much more easier than I had thought.

Imagine that you wish to run two different instances of your application to distribute the high traffic.
With Nginx, you can enable all requests to be proxied to your main domain, while Nginx takes care of distributing the requests to different servers in a round robin fashion.

Open the Nginx configuration file located at /etc/nginx/nginx.conf and add an upstream block and modify the server block as shown below: 

http {
    upstream yourapp {
        ip_hash;
        server domain1.com:8080;
        server domain2.com:8080;
    }

    server {
        listen 80;
        location / {
            proxy_pass http://yourapp;
        }
    }
}


That's it! Make sure your app is running on both the servers on your specified ports and then restart Nginx by running:
/etc/init.d/nginx restart

The ip_hash specified in the upstream block is used for session persistence so that subsequent requests of the same client can be directed to the same server. Nginx uses the client's ip address as a hash key to determine which server should be selected for the client's requests. Nginx also offers other options like weighted load balancing, least connected load balancing and health checks.


Sunday, November 16, 2014

At Wayanad - In the lap of nature


"Actually, the best gift you could have given her was a lifetime of adventures..."
- Lewis Carroll in 'Alice in Wonderland'

Last week, owing to Muharam and Guru Nanak Jayanthi, we again had a couple of holidays and as I was heading out on another trip, my friends at office were kidding me on how "she never misses a holiday". I never realized that I was travelling that frequently. But yeah, I love travelling and adventures. As a kid, I loved reading "Alice in Wonderland", her adventures as she falls through the rabbit hole and meets queer, interesting characters. The ever tensed rushing White Rabbit, the mischievous grinning Cheshire cat quoting philosophical points now and then, the Mad Hatter and March Hare always having a tea-party and singing an Unbirthday song, the hookah-smoking caterpillar, the melancholic and sobbing Mock Turtle, the Queen of Hearts whose only way of settling difficulties was "Off with his head!", every character and adventure was interesting and exciting. As a kid, I always hoped that some day I too would fall into a rabbit hole and have such wonderful adventures. But growing up, I realized that adventures and interesting people are all around us and we only need to open our hearts to embrace them.

As Roald Dahl says in the book "Charlie and the Chocolate Factory":
“And above all, watch with glittering eyes the whole world around you because the greatest secrets are always hidden in the most unlikely places. Those who don't believe in magic will never find it.”

So coming back to my holiday, this was a "Deja-vu" of my dad's MIT batch-mates and their family, all getting together at Wayanad for four days of fun and reminiscing the old college days.

Nov 5th, 2014 (Wed): We arrived at Calicut railway station via Mangalore mail at around 8:30 am. A Tempo traveller had been arranged to take us to Wayanad. The drive uphill was dizzy and had lot of hairpin bends. We reached our accommodation at Silverwoods Resort around mid-day. The hotel staff courteously received us with a welcome drink. Sipping the fresh mint lime juice, I stepped out into the balcony of the lobby which overlooked the beautiful Banasura Lake. The clean, fresh air of the hill station and the calm, peaceful tranquility surrounding the entire place was enough to relax and rejuvenate the mind and soul. The resort was neatly maintained and as we walked to our rooms, we could see a variety of flowers and greenery all along. Our room was quite big with a nice comfy bed, airconditioning, LCD TV, and a kettle to make hot water/coffee/tea along with coffee, milk and sugar packets. The bathroom was so big that it was a room in itself complete with bathtub, a hairdryer and an assortment of kits - dental kit, shaving kit, shower kit, sewing kit, etc. To make everything perfect, there was the stunning view of the lake from the balcony. The entire day, we were relaxing from the travel and evening we all got together in a conference room and I got to know many of my dad's friends and their families.

Nov 6th, 2014 (Thu): Morning, we visited the Thirunelli temple located around 70kms from our resort and had fun playing in the waters of the joyful Papanasini stream nearby. Post-lunch we went for boating in the Banasura lake. The lake had an aura of serenity and utter placidity. There were little ripples on the surface here and there, but as the boat moved deeper, the surface was clear, calm and silent. Observing the still waters running deep evokes an inner stillness within and you feel a sense of connectedness with everything and everyone. The lone duck swimming, a kingfisher searching for its prey, the surrounding mountains and trees, the vast blue sky reflected in the waters, the people in the boat clicking pictures and joking around.. For a moment the mind stills, you become aware of awareness and experience a profund sense of peace and Oneness.

Nov 7th, 2014 (Fri): Today we visited the Kuruva Island, a dense uninhabited evergreen forest in the middle of the Kabini river. We crossed the river in bamboo rafts pulled by overhead ropes. The island was a dense forest with a narrow trail leading around the whole place. At one place, we could get down into the river and the rushing waters massaged my tired feet. After lunch, we took a nap in the room while the ladies went out for shopping. Evening was a perfect relaxation. I made a cup of coffee for me and my dad and we chilled out in the balcony, breathing in the fresh air and spotting some rare flowers in the garden. It was a full moon night and it added to the magic and mystery of the place. A refreshing warm bubble bath to revive the senses and a quiet, stroll around the place in the night were enough to end the holiday.

Nov 8th, 2014 (Sat): After breakfast, we checked out of the resort and on the way to Calicut visited Pookot Lake. The lake was pretty big and a lovely path wound around the entire lake. After lunch, we also spent some time in the Calicut beach and then finally reached the railway station for boarding the train to Chennai.

Some thoughts about the entire trip:
* I got to taste a variety of Kerala cuisine during this trip. Aappam with veg stew and coconut milk, rice and wheat puttu with kadala curry, avial, more curry, ela ada, palada pradhaman, kappa, etc.
* I seem to have fallen in love with spiders. Inside the resort, I saw a couple of spiders with beautifully designed webs hanging from the trees. The web was so carefully, intricately designed with the same repeating pattern all over in a circle. Again at Pookot Lake, I saw one clever spider which had weaved a thin line between two trees, like a telephone line. One cannot help but marvel at the wonderful creation of nature. Where did the insect learn to be an artist? I so wished I could be a spider, weave my own net and sleep in it hanging in mid-air.
* It was interesting to meet all my dad's friends. Each of them were unique - some were always joking around and making people light-hearted, some always wanted to make their opinions heard, one of them had a unique talent of conducting magic shows and confusing people, one of them was a passionate photographer amazing people with his works, some were good at organizing games and fun activities, some had travelled far and wide and talked about lots of places, and some were nerds sitting quietly in a corner and observing the entire chaos. Some of the ladies were talkative, some had a wide variety of home remedy tips and some of them were so keen on shopping green pepper before going home. It is in the midst of such a group that you realize how unique everyone is, yet beneath all those differences, at the level of spirit, we are all but one, like distinct waves of a larger ocean. Each is but a reflection of the other and like in a hall of mirrors each one reflects a different version of ourselves.

Our room at Silverwoods Resort

Sunset view from the balcony

Bamboo raft to Kuruva Island

I would love to hear your feedback! What do you think of my travel blog posts? Are they too long and descriptive? Please share your thoughts in the comments.