我应该使用Global ID还是找到我的Notification系统的另一个解决方案?
So I have nearly finished my notification system, and just before I am about to implement reCAPTCHA, I test what happens if I spam the notifications.
To give you some background on my notification system. I determine the newest content, by its timestamp. I retrieve the rows from the database ORDER BY timestamp. The timestamp value is an integer formatted to Unix Time. When notifications are shown, they are hyperlinks, that follow this URL format -
http://test.com/article/id
Where id is the id for the table, each time a new article is submitted, the id increments. I noticed after spamming my notifications, that the URL's of the spammed notifications are in reverse order. After further investigation, I find that if I spam quick enough, the timestamp variable is not accurate enough, and records multiple submissions with the same timestamp.
Since my website is low traffic now, and there aren't many submissions, this is currently not an issue, but if, a very small chance, but if a piece of content is submitted at the same time as another, the notifications will rank when they were submitted wrongly, a small, but annoying bug.
So I'm wondering what I should do. Should I fix the issue, or is this an extremely minute chance of this happening. Due to the implementation of reCAPTCHA, spamming is not an issue, but there is still a chance this could happen by accident.
I have come up with 3 possible solutions. My question is which would be the most efficient
- Create a global id for all 4 types of content, which increments every time a comment, article or update is created.
- Use a more accurate PHP time function, such as microtime
- Add some sort of secondary ranking variable
所以我几乎完成了我的通知系统,就在我即将实施reCAPTCHA之前,我测试了如果发生了什么 我发送通知垃圾邮件。 p>
为您提供有关我的通知系统的背景信息。 我按时间戳确定最新内容。 我从数据库ORDER BY时间戳中检索行。 时间戳值是一个格式为 Unix时间的整数。 当显示通知时,它们是超链接,遵循此URL格式 - p>
http://test.com/article/id
code> pre>
其中id是表的id,每次提交新文章时,id都会递增。 在发送垃圾邮件后,我注意到垃圾邮件通知的网址顺序相反。 经过进一步调查后,我发现如果垃圾邮件足够快,则时间戳变量不够准确,并记录多个提交的时间戳相同。 p>
由于我的网站现在网站流量很低,而且提交的内容不多,目前这不是问题,但如果机会非常小,但是如果提交了一段内容 与此同时,通知将在错误提交时排名,这是一个小而烦人的错误。 p>
所以我想知道自己应该做些什么。 我应该解决这个问题,还是这种情况发生的极小可能性。 由于reCAPTCHA的实施,垃圾邮件不是问题,但仍有可能偶然发生。 p>
我已经提出了3种可能的解决方案。 我的问题是效率最高的 p>
- 为所有4种类型的内容创建一个全局ID,每次创建评论,文章或更新时都会增加。 li>
- 使用更准确的PHP时间函数,例如 microtime li>
- 添加某种辅助排名变量 li>
ol>
div>
Given that there can be multiple threads and even multiple nodes in a cluster inserting data in different tables there is always a possibility that any clock based value you use will get duplicated in multiple, or even the same, table.
So my first thought is to use a global id table. You could use a common content
table with an auto-incrementing primary key that all other tables foreign key into and use that for ordering.
On the other hand, by the same logic, how well can you ensure some fixed order between submissions? It is quite possible that two submissions will be committed to the database in the reverse order of receiving at the server. The only way to solve that problem I think is to a have a global gatekeeper that all requests have to pass through. If you are using such a gatekeeper, that is also the best place to assign the ordering value.
All in all, I think you should not insist on complete ordering because it does not exist unless it is a highly ordering sensitive system such as Trading or Betting. Otherwise microsecond should be good enough, as long as the notification for a comment on a article does not come before the article itself.