Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8837083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:38:15+00:00 2026-06-14T09:38:15+00:00

Please help me find my misunderstanding. I am writing an RPG on App Engine.

  • 0

Please help me find my misunderstanding.

I am writing an RPG on App Engine. Certain actions the player takes consume a certain stat. If the stat reaches zero the player can take no more actions. I started worrying about cheating players, though — what if a player sent two actions very quickly, right next to each other? If the code that decrements the stat is not in a transaction, then the player has a chance of performing the action twice. So, I should wrap the code that decrements the stat in a transaction, right? So far, so good.

In GAE Python, though, we have this in the documentation:

Note: If your app receives an exception when submitting a transaction, it does not
always mean that the transaction failed. You can receive Timeout, TransactionFailedError, or
InternalError exceptions in cases where transactions have been committed and eventually will
be applied successfully. Whenever possible, make your Datastore transactions idempotent so
that if you repeat a transaction, the end result will be the same.

Whoops. That means that the function I was running that looks like this:


def decrement(player_key, value=5):
  player = Player.get(player_key)
  player.stat -= value
  player.put()

Well, that’s not gonna work because the thing isn’t idempotent, right? If I put a retry loop around it (do I need to in Python? I’ve read that I don’t need to on SO… but I can’t find it in the docs) it might increment the value twice, right? Since my code can catch an exception but the datastore still committed the data… huh? How do I fix this? Is this a case where I need distributed transactions? Do I really?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:38:16+00:00Added an answer on June 14, 2026 at 9:38 am

    First, Nick’s answer is not correct. DHayes’s transaction is not idempotent, so if it’s run multiple times (ie. a retry when the first attempt was thought to have failed, when it didn’t), then the value will have been decremented multiple times. Nick says that “the datastore checks if the entities have been modified since they were fetched”, but that doesn’t prevent the problem since the two transactions had separate fetches, and the second fetch was AFTER the first transaction completed.

    To solve the problem, you can make the transaction idempotent by creating a “transaction Key” and recording that key in a new entity as part of the transaction. The second transaction can check for that transaction key, and if found, will do nothing. The transaction key can be deleted once you’re satisfied that the transaction completed, or you give up retrying.

    I’d like to know what “extremely rare” means for AppEngine (1-in-a-million, or 1-in-a-billion?), but my advice is that idempotent transactions is required for financial matters, but not for game scores, or even “lives” 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could you please help me find out the time complexity of the Fleury' algorithm
ereg and eregi functions will be deleted from Php. Please help to find alternatives
Please help me to find out error in my SQL query. I have created
Please help me, I'm trying to find some documentation about pre-installed applications at Android.
Could you please help me out to find the correct deceleration rate for the
I cannot find where the sqlcmd is? i just want the prompt. please help!
I have one intresting requirement to achieve. could you please help? Please find the
Please help me find the MongoDb equivalent for the SQL: select val from tblName
Can someone please help me find a list of flags that are needed by
the following regexp is wrong - can someone please help to find all :-)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.