Guru on Rails

if you don’t sacrifice for your dream then your dream becomes your sacrifice.
Will Nguyen
Interview challenge: developing a system to serve high traffic of POST and GET
Sun 17 Feb 2019

This is a challenge from "XYZ" company. My solution is using message queue for "POST". For "GET", it's just a challenge, not applying in real project. Because pub/sub system would be better than developing "GET" API like this. This is the implementation for http://www.guruonrails.com/developing-high-traffic-apis-using-message-queue-implemented-by . Please have a look on that article for more details.

This guideline for MAC OS.

GITLAB REPOSITORY

1. Installation

Installing:

  1. Rabbitmq 3.7.9
  2. Redis
  3. Ruby 2.5.1

Rabbitmq

Installing instruction https://www.rabbitmq.com/install-homebrew.html

brew install rabbitmq

Go to rabbitmq server and import this file which was included in source code.

rabbit_localhost_2019-2-17.json

Now we would see two exchanges:

  1. medwing.readings
  2. delayed.exchange

Enabling plugins by copying rabbitmq_delayed_message_exchange-20171201-3.7.x.ez file in source code and paste to plugins directory. Reference.

/usr/local/Cellar/rabbitmq/3.7.9/plugins

Run this command to enable plugin.

rabbitmq-plugins enable rabbitmq_delayed_message_exchange

Looking for this plugin here if we use another Rabbitmq version https://www.rabbitmq.com/community-plugins.html

Make sure we are using correct version.

Start Rabbitmq server by this command.

rabbitmq-server

Redis

Installing redis.

brew install redis

Start redis by this command.

redis-server

Making sure we clear all Redis data before manual testing on development mode.

redis-cli flushall

2. Testing 

Rabbitmq and Redis are not required for testing mode. I used this trick for testing message queue https://github.com/jondot/sneakers/wiki/Testing-Your-Worker

We can find message_queue_helper.rb in the source code for more details.

All we need is just run this command.

bundle exec rspec spec
Finished in 6.7 seconds (files took 2.4 seconds to load)
36 examples, 0 failures

3. Development

Please respond 'y' when you run db:setup or any other commands which don't need rabbitmq.

rails db:setup

Starting rabbitmq and redis servers.

rabbitmq-server
redis-server

Starting rails server and workers.

foreman start -f Procfile.dev

Let's give it a try by using postman. 

POST

GET

STATISTIC

One more.

Average = (200+600)/2 = 400

If we do the same with thermostat_id = 2, we would see the sequence number like below.

References:

Why sneakers?
https://github.com/jondot/sneakers/wiki/Why-i-built-it#sneakers
https://blog.stanko.io/rabbitmq-is-more-than-a-sidekiq-replacement-b730d8176fb

http://www.rabbitmq.com/installing-plugins.html
https://dl.bintray.com/rabbitmq/community-plugins/3.7.x/rabbitmq_delayed_message_exchange/