简体   繁体   English

如何处理REST API中的大量资源

[英]What to do about huge resources in REST API

I am bolting a REST interface on to an existing application and I'm curious about what the most appropriate solution is to deal with resources that would return an exorbitant amount of data if they were to be retrieved. 我正在将一个REST接口用于现有应用程序,我很好奇最合适的解决方案是什么来处理资源,如果要检索它们将返回过多的数据。

The application is an existing timesheet system and one of the resources is a set of a user's "Time Slots". 该应用程序是现有的时间表系统,其中一个资源是一组用户的“时间段”。 An example URI for these resources is: 这些资源的示例URI是:

/users/44/timeslots/

I have read a lot of questions that relate to how to provide the filtering for this resource to retrieve a subset and I already have a solution for that. 我已经阅读了很多与如何为此资源提供过滤以检索子集有关的问题,我已经有了解决方法。

I want to know how (or if) I should deal with the situation that issuing a GET on the URI above would return megabytes of data from tens or hundreds of thousands of rows and would take a fair amount of server resource to actually respond in the first place. 我想知道如何(或者如果)我应该处理在上面的URI上发出GET将从数十或数十万行返回兆字节数据的情况,并且需要相当数量的服务器资源来实际响应第一名。

  • Is there an HTTP response that is used by convention in these situations? 在这些情况下,是否存在惯例使用的HTTP响应?
    I found HTTP code 413 which relates to a Request entity that is too large, but not one that would be appropriate for when the Response entity would be too large 我发现HTTP代码413与Request实体相关,该实体太大,但不适用于当Response实体太大时
  • Is there an alternative convention for limiting the response or telling the client that this is a silly request? 是否存在限制响应或告诉客户这是一个愚蠢的请求的替代约定?
  • Should I simply let the server comply with this massive request? 我应该让服务器遵守这个大规模的请求吗?

EDIT: To be clear, I have filtering and splitting of the resource implemented and have considered pagination on other large collection resources. 编辑:要清楚,我已经过滤和拆分实现的资源,并考虑了其他大型集合资源的分页。 I want to respond appropriately to requests which don't make sense (and have obviously been requested by a client constructing a URI). 我想适当地回应那些没有意义的请求(显然是由构建URI的客户端请求的)。

You are free to design your URIs as you want encoding any concept . 您可以根据需要编码任何概念来自由设计URI。

So, depending on your users (humans/machines) you can use that as a split on a conceptual level based on your problem space or domain. 因此,根据您的用户(人/机器),您可以根据问题空间或域将其用作概念级别的拆分。 As you mentioned you probably have something like this: 如你所说,你可能有这样的事情:

/users/44/timeslots/afternoon
/users/44/timeslots/offshift
/users/44/timeslots/hours/1
/users/44/timeslots/hours/1
/users/44/timeslots/UTC1624

Once can also limit by the ideas/concepts as above. 一旦也可以通过上述想法/概念进行限制。 You filter more by adding queries /users/44/timeslots?day=weekdays&dow=mon 您可以通过添加查询/用户/ 44 /时间段来过滤更多内容吗?day = weekdays&dow = mon

Making use or concept and filters like this will naturally limit the response size. 像这样使用或概念和过滤器自然会限制响应大小。 But you need to try design your API not go get into that situation . 但是你需要尝试设计你的API 而不是进入那种情况 If your client misbehaves, give it a 400 Bad Request . 如果您的客户端行为不当,请给它一个400 Bad Request If something goes wrong on your server side use a 5XX code. 如果您的服务器端出现问题,请使用5XX代码。

Make use of one of the tools of REST - hypermedia and links (See also HATEOAS ) Link to the next part of your hypermedia, make use of "chunk like concepts" that your domain understands (pages, time-slots). 利用REST的工具之一 - 超媒体和链接 (另请参阅HATEOAS )链接到超媒体的下一部分,利用您的领域理解的“块状概念”(页面,时间段)。 No need to download megabytes which also not good for caching which impacts scalability/speed. 无需下载兆字节,这也不利于缓存 ,这会影响可扩展性/速度。

timeslots is a collection resource, why won't you simply enable pagination on that resource timeslots是一个集合资源,为什么你不能简单地在该资源上启用分页

see here: Pagination in a REST web application 请参阅此处: REST Web应用程序中的分页

calling get on the collection without page information simply returns the first page (with a default page size) 在没有页面信息的情况下调用get集合只会返回第一页(具有默认页面大小)

Should I simply let the server comply with this massive request? 我应该让服务器遵守这个大规模的请求吗? I think you shouldn't, but that's up to you to decide, can the server handle big volumes? 我认为你不应该这样,但是由你来决定,服务器可以处理大量的数据吗? do you find it a valid usecase? 你觉得它是一个有效的用例吗?

This may be too weak of an answer but here is how my team has handled it. 这可能是一个太弱的答案,但这是我的团队处理它的方式。 Large resources like that are Required to have the additional filtering information provided. 需要大量资源才能提供额外的过滤信息。 If the filtering information is not there to keep the size within a specific range then we return an Internal Error (500) with an appropriate message to denote that it was a failure to use the RESTful API properly. 如果过滤信息不是为了将大小保持在特定范围内,那么我们返回带有适当消息的内部错误(500),以表示无法正确使用RESTful API。

Hope this helps. 希望这可以帮助。

You can use a custom Range header - see http://otac0n.com/blog/2012/11/21/range-header-i-choose-you.html 您可以使用自定义Range标头 - 请参阅http://otac0n.com/blog/2012/11/21/range-header-i-choose-you.html

Or you can (as others have suggested) split your resource up into smaller resources at different URLs (representing sections, or pages, or otherwise filtered versions of the original resource). 或者您可以(正如其他人建议的那样)将您的资源分成不同的URL(表示部分,或页面,或原始资源的其他过滤版本)的较小资源。

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

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