简体   繁体   English

如何确保REST API分页中的数据一致性

[英]How to ensure data consistency in REST API pagination

I'm building an instant messenger on mobile client that interacts with RESTful API through HTTP requests. 我正在移动客户端上构建一个即时通讯程序,该通讯程序通过HTTP请求与RESTful API进行交互。 The pagination endpoint is quite standard - it has starting location (offset) and number of items in a page (limit). 分页端点非常标准-它具有起始位置(偏移)和页面中的项目数(限制)。 I'm having trouble figuring out how to ensure 100% data consistency with pagination when the database can rapidly change. 我在弄清楚如何在数据库可以快速更改时如何确保100%的数据分页一致性方面遇到麻烦。

For example, with some dozen participants, there could be a dozen new messages in a conversation within a second. 例如,有几十个参与者,在一秒钟内的对话中可能有十二个新消息。 I don't think it's far-fetched to guess that some of those messages can alter the database within the time the HTTP request for pagination comes back from the server. 我认为猜测其中的某些消息可以在服务器发出HTTP分页请求的时间内更改数据库并不难为情。 Fortunately, since this is a messenger I do not have to consider the possibility of data deletion and consider only the data addition. 幸运的是,由于这是一个使者,因此我不必考虑数据删除的可能性,而只考虑数据的添加。

Among my research, following two links were quite helpful but didn't provide clear solution: 在我的研究中,以下两个链接很有帮助,但没有提供明确的解决方案:

How to ensure data integrity in paginated REST API? 如何确保分页REST API中的数据完整性?

How to implement robust pagination with a RESTful API when the resultset can change? 当结果集可以更改时,如何使用RESTful API实现可靠的分页?

The only potential solution I can come up with is using the timestamp of the last object in the previously fetched page. 我唯一能想到的潜在解决方案是使用先前获取的页面中最后一个对象的时间戳。 So the HTTP query would have timestamp as a parameter, and the server would return a page of objects created after that timestamp. 因此,HTTP查询将使用时间戳作为参数,并且服务器将返回在该时间戳之后创建的对象页面。

Is there any potential problem I'm not seeing, or even better, a much better solution to this issue? 有没有我看不到的甚至是更好的解决此问题的潜在问题?

It seems that the method I've thought of has a name - cursor based pagination. 我想到的方法似乎有一个名称-基于游标的分页。

The link below has a great graphical description and explanation, plus an example in php. 下面的链接具有出色的图形描述和说明,以及php中的示例。

http://www.sitepoint.com/paginating-real-time-data-cursor-based-pagination/ http://www.sitepoint.com/paginating-real-time-data-cursor-based-pagination/

There's also a helpful guide from Django Framework that compares two different pagination techniques (LimitOffsetPagination and CursorPagination). Django框架中还有一本有用的指南,比较了两种不同的分页技术(LimitOffsetPagination和CursorPagination)。

http://www.django-rest-framework.org/api-guide/pagination/ http://www.django-rest-framework.org/api-guide/pagination/

Cursor based pagination requires a unique, unchanging ordering of items. 基于游标的分页需要唯一,不变的项目排序。 Facebook and Twitter use some generated IDs. Facebook和Twitter使用一些生成的ID。 As for me, I've decided to simply use timestamp at object creation, as it supports up to milliseconds precision. 对于我来说,我决定在对象创建时仅使用时间戳,因为它支持高达毫秒的精度。 That should be good enough for now. 现在应该已经足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM