如何确保分页 REST API 中的数据完整性? [英] How to ensure data integrity in paginated REST API?
问题描述
我目前正在研究 REST API.API 返回的资源预计是来自数据库的海量数据(DB 中千万行).分页是必须的,以避免在将数据写入 HTTP 响应时消耗大量内存.
I am working on REST API currently. The resource that the API returns is expected to be huge data from database (tens of millions of row in DB). Pagination is a must in order to avoid enormous memory consumption when writing the data to HTTP response.
在客户端请求之间删除/添加数据库中的行时,如何确保数据完整性?
How to make sure the data integrity when there is deletion/addition of rows in DB in between client requests?
例如:
page 1: [ John, Mary, Harry, David, Joe ]
page 2: [ Mike, Don, Alex ]
客户端请求第1页并将其存储在本地(文件/内存)后,在请求第2页之前,数据更改为:
After client requested for page 1 and stored it locally in (file/memory), before asking for page 2, the data is changed to:
page 1: [ John, Mary, Harry, David, **Mike** ]
page 2: [ Don, Alex, **Terry** ]
推荐答案
真正的 RESTful(因此是服务器端无状态)答案是:
A true RESTful (and therefore server-side stateless) answer would be:
- 请求前五个记录(最后一个是Joe"),
- 然后询问Joe"上级[1]的五个记录,
- 等等.
使用此策略,您将在第 2 页中看到Mike"和Terry".
With this strategy you'll get "Mike" and "Terry" in page #2.
[1] 它们必须有一个排序顺序(字母或其他).
[1] They must have a sort order (alphabetical or other).
这篇关于如何确保分页 REST API 中的数据完整性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!