Update django record in short period of time (ms)?

1

Greetings I hope you can help me with this topic I would really appreciate it!

The situation is as follows:

In my project django I have an endpoint in which the client connects makes a request to update a record in the db. The endpoint takes the data of the request and makes several security checks; If everything goes well, the record is updated, otherwise an error will be returned.

So far so good, the endpoint works correctly, the detail is presented when the client sends 5 or more requests almost at the same time (differences of ms) to update the same record, the server does not have time to process all the requests and of the 5 only updates 3. The other 2 requests return an incorrect result.

I have already optimized the query as much as I could, which is why I think that is not the problem, the server takes an average of 0.1 ms to process each request!

I hope you can give me some advice or solution for this topic. Thanks!

Edit: The 5 (or more) requests do not generate any type of error when processing them but return incorrect results.

I will try to illustrate with an example so that it is understood a little better: suppose I have a table in the db called Sweets, this has an int field Quantity with the total cavity of sweets available, the client calls the endpoint to update the amount of sweets available, passing as agurment the amount to add to the value of the field Amount of the database, up to there no problem the request is processed correctly.

When the customer makes many simultaneous calls in a short period of time (0.05 ms), the first requests are processed well and the quantity of candies is updated correctly, but there comes a time when the requests take on an Old Quantity value because the server I suppose is halfway through processing another request, and that causes it to return incorrect values.

I hope you have given me to understand. Thanks!

    
asked by AnthonyGDM 13.09.2016 в 16:42
source

1 answer

1

You have several options:

  • Increase the capacity of your server so that it can resolve all requests in the shortest possible time. You can do it vertically, increase memory and processors or horizontally, increasing more machines and balancing the load
  • Optimize your code. Verifying the indexes or the SQL code created by Django. There are several tools that help you that, like django-debug-toolbar .
  • Accept all requests, but do not try to process them immediately , place them in a row, first enter first exit. To the users you can return a promise of processing. The concept is called, promises .
  • Reduce the speed at which a user can make requests, limit the number of requests per minute to be equivalent to the capacity of your server. The concept is called throttling .

By the way, have you read this guide: "How to create a minimal, complete and verifiable example" ? If you have not read it, I recommend you do it, it will be very useful.

    
answered by 13.09.2016 / 19:36
source