Search code examples
ruby-on-railsloggingrabbitmqbackground-processrecommendation-engine

Tracking activities best practice in Ruby on Rails


I want to save product or posts views in the database with additional meta data, like who viewed the object, referral, time etc.

All the activities will be used for recommendation engines, update tables and more background work.

The main concern is speed, for both user experience and SEO.

I came with 3 approaches:

  1. Read the record from the database, and write an activity (2 transactions).
  2. Instead of read, update the record for the view (1 transaction).
  3. Read the record and send the activity object to Redis or RabbitMQ.

I'm using Ruby on Rails with PostgreSQL.

Does the 1st approach faster than the 2nd? Does the 3rd option is overkill for a small web app?


Solution

  • for performance other scalability, use redis or rabbitmq from option #3.

    application size does not matter so much in determining when to use a message queue. rather, systems needs determines that.

    storing views and activities for a user is a great example of when you should use a queue, to keep the original http request fast.

    make a single network call to publish the event data to rabbitmq, and have as many consumers as you need, to record all of the event data in your database, in the background.

    meanwhile, the user is happily looking at your web page already, while all of that is happening on another server, somewhere else.