Code RoomView count diverges between systems
HardPrep Room Coding #2274

View count diverges between systems

Code reviewDatabases & SQLSenior–Staff~26 min

Review this Python function that increments a counter cached in Redis backed by Postgres.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 26 min
Mark a line and say what kind of problem it is.0 findings
1def increment_views(redis, db, post_id):
2 cached = redis.get(f"views:{post_id}")
3 if cached is None:
4 row = db.execute(
5 "SELECT views FROM posts WHERE id = %s", (post_id,)
6 ).fetchone()
7 cached = row[0]
8 new_val = int(cached) + 1
9 redis.set(f"views:{post_id}", new_val)
10 db.execute("UPDATE posts SET views = %s WHERE id = %s", (new_val, post_id))
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.