Guru on Rails
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.
- Rabbitmq 3.7.9
- Ruby 2.5.1
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.
Now we would see two exchanges:
Enabling plugins by copying rabbitmq_delayed_message_exchange-20171201-3.7.x.ez file in source code and paste to plugins directory. Reference.
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.
brew install redis
Start redis by this command.
Making sure we clear all Redis data before manual testing on development mode.
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
Please respond 'y' when you run db:setup or any other commands which don't need rabbitmq.
Starting rabbitmq and redis servers.
Starting rails server and workers.
foreman start -f Procfile.dev
Let's give it a try by using postman.
Average = (200+600)/2 = 400
If we do the same with thermostat_id = 2, we would see the sequence number like below.