Delete the row as soon as a column reaches a specific value
<p>If this be my model:</p>

<pre><code>class Entity(Base):
    id = Column(Integer, primary_key=True)
    count = Column(Integer, default=0)
</code></pre>

<p>I want to delete any rows that reach <code>count</code> value of 3 (or any number).
How can I do that?
should I implement such deletion in the Controller of my web application and every time <code>count</code> is incremented, I check if it's value is 3 or not? How should I care about the concurrent requests that are changing the value? What is the best solution? </p>

<p>After sometime pondering and consulting with experts I figured out that solution to this matter is either implementing a lock in database or using redis. Since locking may cause performance problems in multitude requests, It's better to store the value of <code>count</code> in a redis database and try updating the <code>count</code> value in the object-relational database in specific periods of time.</p>

python postgresql concurrency sqlalchemy locking