Despite the temptation of buying new servers and adding CPU and RAM – you need to do what you can with the code first. I am exploring and cataloguing these methods to speed up Django QuerySets before server scaling.
The general idea is simple – use more QuerySet methods instead of Python methods to avoid evaluation.
As it always happens, 4am Sunday it well went to hell – Django create two instances of ‘2021-08-15’.
Eventually every requests that had this line in it just crashed.
Why did this happen? Well, apparently two instances are not supposed to be made.
My friends and I came to a conclusion that with a very high amount of requests Django missed that tiny bit of time between executing get_or_create method and actually writing an instance to PostgreSQL.
Well good thing - I went to bed late, and my team woke me up much earlier than I would wake up.
What is he solution? If the field is meant to be unique, make it unique:
date = DateField(unique=True)