Golang in the process of interface development, which of these two approaches is better, or what is the problem?

when you write the interface, for example, there is a scenario when users browse the article, they need to increase the number of views to the article

.
  • scenario 1: when adding pageviews, there is a channel listener, and the program is running all the time. If you browse a post, open a new goruntime to write data to this channel (the channel that ps: increases pageviews may block), and notify the listener channel to execute
  • .
  • scenario 2: this process adds one
  • to the number of page views of the post before the user requests the interface to return it.

which of these two designs is better? I think the first one will be better. Will there be any problems?

Mar.18,2021

the user's browse request for the article, which is a read operation, can be cached by Cache (if any) and does not need to manipulate the database.
the operation of increasing pageviews involves writing to the database, and usually pageviews do not require strong consistency and real-time performance, so you do not have to wait for the pageview increase operation to be completed (database write is successful) before returning.

so the first way is to asynchronously process the operations that increase pageviews without waiting for the processing result, which can reduce the processing time of user browsing requests and improve performance.
may need to pay attention to avoid malicious or sudden large number of requests, resulting in goroutine flooding, taking up too much memory, of course, this should do some flow control processing when the user establishes a connection.


  • I don't think it's right to open a new goroutine solution every time. If you choose one of the two solutions, then I will choose the second one.
  • provides another practice that I think is more common: introduce redis, to put pageview data in redis and write it asynchronously. When reading browsing values, read from redis and display approximate . For example, if the read value is 1234 , then 1.2k is displayed.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b37204-2c060.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b37204-2c060.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?