R
R
Roman Nazarkin2014-12-26 20:33:36
MySQL
Roman Nazarkin, 2014-12-26 20:33:36

Asynchronous database queries in Rails. How?

Good day to all.
There is a task - to count the number of requests to a certain page of a site built on the basis of RoR, but at the same time it is necessary to maintain the maximum page performance (the server response speed should not fall).
My idea is this: first we generate the page (without making a request to the database), then we give it to the visitor, we close the connection, and only after that we make a request to the database.
And now the question is: is it possible to implement such a mechanism and by what means?
And along the way - maybe there is a better way to implement this?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
anyd3v, 2014-12-26
@anyd3v

read about asynchronous queues, as an example rubyamqp.info/articles/getting_started

V
Viktor Vsk, 2014-12-27
@viktorvsk

1. Syndrome of premature optimization detected.
2. If in this case (in a specific project, resource, application) there really would be such an urgent need to save time for forking a process or queuing, then you would not be doing this.
3. In such cases, you can use triggers, callbacks, stored procedures in the database, which is generally correct, in principle. Although the use of queues is also logical. Which of these two approaches will eventually turn out to be more acceptable (in terms of speed, reliability, scalability, maintainability) must be clarified already on the spot. By the way, note that only some of the important points are listed, except for speed (and what kind? on the client, on the application server, on the database server? ...), which you always want to cram everywhere if you don’t know anything.
4. If you still want to waste part of your life in order to satisfy the syndrome indicated in the first paragraph, first familiarize yourself with what output buffering is, how it works in certain ruby ​​servers (webrick, mongrel, unicorn, puma ...), as well as reverse proxy (nginx, apache...) and what to do with rack-middleware that do not support streaming. Plus, what problems can appear with headers.
Here's where to start:
stackoverflow.com/questions/2832010/what-is-output...
blog.plataformatec.com.br/2012/06/why-your-web-fra...
blogs.sequoiainc.com /blogs/easy-streams-with-rails...
stackoverflow.com/questions/2148114/why-use-output...
www.sitepoint.com/php-streaming-output-buffering-e...
weblog.rubyonrails .org/2011/4/18/why-http-streaming
railscasts.com/episodes/266-http-streaming
https://gist.github.com/igrigorik/3058839
blog.phusion.nl/2012/08/03/why-rails-4-live-stream...
evaleverything. com/2013/09/07/response-streams-wit...

V
vsuhachev, 2014-12-27
@vsuhachev

If you need to count the number of hits to the page without "side effects", then there are at least 2 ways
1) Use external counters Yandex Metrics, Google Analytics, etc.
2) Read statistics from web server logs. There are various programs for such an analysis, look for
each method. Each method has its own characteristics: N1 - it is more focused on detecting people, N2 - it gets full statistics, including bots, search engines, etc.

B
Boris Penkovsky, 2014-12-27
@Able1991

Run your mechanism in a before/after filter via

Thread.new do
  call_some_method(params)
end

S
Sergey, 2015-01-04
@mastedm

Add a request from the page itself: ajax or loading a one-pixel image - whatever.
It may well be that, for example, the page is cached by the web server and not every request from it reaches the application. Or a search bot makes a request - you probably don't need such "views" either.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question