Is it good to use Sharded Counter value as Entity ID to keep GAE Long ID short

随声附和 提交于 2020-01-06 02:42:07

问题


What is the viability of using a Long value generated by the GAE Sharded Counter code. In terms of having a unique Long id across datacenters?

Why do I need to use the counter value as ID? GAE generates very long Long values as entity id, which in my app I need to have short ID's like the one generated by the Sharded counter at first.

Question: Would the sharded counter at some point will generate the same value for a different request such that ID's might collide?


回答1:


It is not viable since the sharded counter goal is to keep an eventually consistent count that helps avoid contention while increasing or decreasing the value of the counter by dividing that work in different shards. The get_count method will sum up all the sharded counters to return the total count, but that value cannot be considered as a unique id because it's only sure that it will eventually count all the increase or decrease operations you performed over it, so it can return the same value for different requests, even if each request involves an increase operation.

A similar approach can be designed that would have a shard of reserved id's pools, you would have to modify the code of the sharded counters but instead of sharding the increase or decrease of the counter the operation would randomly select among a predefined set of sharded pools of id's and from one of them return an id and make sure that it's not returned again. The pools for each shard can be number ranges that fit your particular needs, and you would have to manage the situation when one of the pools runs out of reserved ranges and you have to 'refill' it again.

I recommend taking a look at this article, to get some options to this kind of problem: http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagram



来源:https://stackoverflow.com/questions/27973671/is-it-good-to-use-sharded-counter-value-as-entity-id-to-keep-gae-long-id-short

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!