1

During localhost development the ID's generated by GAE, starts with 1.

However in a real GAE deployment in the cloud, the ID generated even for the firsts entities are quite long like, 5639412304721232, is there a work around to make the first entities to start with 1, 2, 3.. and so on?

One might suggest to use Sharded Counters, and yes I've used this, however some suggests that sharded counters are not to be used as app might get the same count as it is eventually consistent.

In this case what could be the best solution?

Community
  • 1
  • 1
quarks
  • 33,478
  • 73
  • 290
  • 513

4 Answers4

3

The official post explaining the switch from sequential to 'scattered' ids is here.

The instructions for reverting to sequential behaviour are here, but note the warning that this option will eventually be removed.

The 'best' solution depends on what you need and why. You'll get better datastore performance with scattered ids, but honestly, you might not notice much difference if your app makes gets a small number of requests and makes light use of the datastore. If that's the case, you can use roll your own sequential ids based on a simple entity with a property that holds the the current high watermark id, and rely on having a low transaction rate to keep you from running into limits on the number of transactions per entity.

Reliably handing out sequential ids without gaps in a distributed systems is challenging.

Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46
  • yes it is quite challenging. the use will be for a URL shortener, which I need to have the Long id small at first – quarks Jan 30 '15 at 04:00
2

Be aware that you may run into problems if you create a lot of entities very quickly, with sequential Long IDs. This post gives you an explanation why.

In theory there's a choice of auto ID generation policies, with scattered IDs being the default since 1.8.1, but the old monotonically increasing legacy policy is to be deprecated for the reasons discussed in the linked post.

If you're using a sharded counter, you will avoid this but, as you say, you may encounter other issues.

tx802
  • 3,524
  • 2
  • 17
  • 22
2

You might try using allocate_ds. We use this to get smaller integer values for system generated ids. In Python using a db kind:

    model_key = db.Key.from_path('your_kind_name', 1)
    key_batch = db.allocate_ids(model_key, 1)
    id_new = key_batch[0]
    idkey = db.Key.from_path('your_kind_name', id_new)
stevep
  • 959
  • 5
  • 8
1

I would assign the key's identifier as the strings "1", "2", "3"... and so on, generating them from a sequencer. You can check to see if the entity already exists with a get_or_insert() function.

Similarly, you can use the auto-increment solution by storing the sequence number in an entity.

Community
  • 1
  • 1
Brent Washburne
  • 12,904
  • 4
  • 60
  • 82