知识卡片
客户端预分配资源标识
多资源写入若必须逐次等待服务返回ID,会形成原本可以消除的串行依赖。
允许客户端预先生成资源ID,并在URL或请求正文提交,服务检查格式及唯一性;冲突必须拒绝,不能盲目信任客户端值。UUID库比书中的简短Math.random演示更适合表达规范生成意图;示例不是安全随机或绝不碰撞保证。
如果账户和销售记录只需引用客户ID,预先生成三个ID可消除等待服务返回ID的串行依赖,允许并行发出写入。但若服务要求引用对象先存在,依赖仍未消除;失败恢复也须另行设计。可另加可读名称,避免把机器ID承担人类记忆用途。
关联:[[承诺稳定服务起点]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 超媒体服务 · 查看原书依据
Client applications should be instructed to generate their own unique identifier and either include that in the URL or supply it in the body of the HTTP POST, PUT, or PATCH request sent to the service API. Service APIs can then inspect the value and confirm it is unique before processing the request.
参考来源
- 位置:《RESTful Web API Patterns and Practices Cookbook》第5章《Hypermedia Services》(来源项目book-sources:extracted/RESTful Web API Patterns and Practices Cookbook Connecting and Orchestrating Microservices and Distributed Data (Mike Amundsen)/_epub-src/OEBPS/ch05.xhtml,配方5.14)
- 结论依据:原文演示客户端生成ID及服务验证,比较依赖服务返回ID的顺序写入与预生成ID后的并行调用。
- 原始内容:Client applications should be instructed to generate their own unique identifier and either include that in the URL or supply it in the body of the HTTP POST, PUT, or PATCH request sent to the service API. Service APIs can then inspect the value and confirm it is unique before processing the request.