我应该在公共API中使用UUID作为资源吗? [英] Should I use UUIDs for resources in my public API?

查看:68
本文介绍了我应该在公共API中使用UUID作为资源吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建SaaS应用程序,并希望公开与当前数据存储实现无关的资源ID(Postgres自动增量ID).这些Stack Overflow帖子(一个 两个)建议创建本地唯一的ID很困难,我不妨使用UUID,UUID当然可以用几乎所有语言轻松,安全地生成.

I'm building a SaaS application and want to expose IDs for resources which are not tied to my current data storage implementation (Postgres auto-increment IDs). These Stack Overflow posts (one two) suggest that creating locally unique IDs is hard and that I might as well use UUIDs, which are of course easily and safely generated in pretty much any language.

我对这种方法感到满意,但是我想知道为什么我找不到大型SaaS/托管播放器提供的相同功能的API吗?例如:

I'm happy with this approach, but I wonder why I can't find any APIs from big SaaS/hosted players which do the same? For example:

  • Shopify: 9 digit numbers
  • Twilio: 34 character strings
  • Twitter: 20+ digit numbers
  • AMEE: 12 character A-Z0-9

因此,基本上没有人似乎使用UUID.这是否有原因-不是在这里发明的,更聪明的内部ID算法还是其他?就我而言,在没有任何内部算法的情况下,使用UUID是否最有意义?

So basically nobody seems to use UUIDs. Is there a reason for this - not-invented-here, cleverer internal ID algorithms or something else? And in my case, in the absence of any internal algorithm, does it make most sense to go with UUIDs?

推荐答案

您列出的其他供应商有可能拥有自己的ID或哈希方案,以允许他们在内部使用更类似于UUID的同时公开较小的数量.但是最后,必须提出一个问题:只要您的URI是由代码(API客户端)而非人类使用的,那为什么重要呢?

It's possible that those other vendors you listed have their own ID or hashing scheme to allow them to expose a smaller number while using something more akin to a UUID internally. But in the end, the question must be asked: as long as your URIs are intended to be consumed by code (API clients) rather than humans, why would it matter?

不要对那些供应商的所作所为感到害怕.无法保证(a)他们正在做正确的"事情,并且(b)他们的需求与您的需求相同.

Don't get too freaked out by what those vendors have done. There's no guarantee that (a) they are doing the "right" thing and (b) that their needs are the same as yours.

继续使用UUID.

这篇关于我应该在公共API中使用UUID作为资源吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆