0

I have a SimpleDB instance that I update and read using boto for Python:

    sdb = boto.connect_sdb(access_key, secret_key)
    domain = sdb.get_domain('DomainName')
    itemName = 'UserID'
    itemAttr = {'key1': 'val1', 'key2': val2}
    userDom.put_attributes(itemName, itemAttr)

That works a expected. A new item with name 'UserID' and values val1 and val2 will be inserted in the domain.

Now, the problem that I am facing is that if I query that domain right after updating its attributes,

    query = 'select * from `DomainName` where key1=val1'
    check = domain.select(query)
    itemName = check.next()['key2']

I will get an error because the values in the row could not be found. However, if I add a time.sleep(1) between the write and the read everything works.

I suspect this problem is due to the fact that put_atributes signals the data base for writing, but does not wait until this change has been made persistent. I have also tried to write using creating an item and then saving that item (item.save()) without much success. Does anyone know how can I make sure that the values have been written in the SimpleDB instance before proceeding with the next operations?

Thanks.

angelrh
  • 1,491
  • 1
  • 9
  • 4

1 Answers1

0

The issue here is that SimpleDB is, by default, eventually consistent. So, when you write data and then immediately try to read it, you are not guaranteed to get the newest data although you are guaranteed that eventually the data will be consistent. With SimpleDB, eventually usually means less than a second but there are no guarantees on how long that could take.

There is, however, a way to tell SimpleDB that you want a consistent view of the data and are willing to wait for it, if necessary. You could do this by changing your query code slightly:

query = 'select * from `DomainName` where key1=val1'
check = domain.select(query, consistent_read=True)
itemName = check.next()['key2']

This should always return the latest values.

garnaat
  • 44,310
  • 7
  • 123
  • 103
  • Thanks, that worked perfectly. I ran into problems using consistent_read because I was using boto version 1.9b, upgraded to 2.3.0 and solved it. – angelrh Apr 05 '12 at 17:31