Code Room
Code reviewHardcr-g650
Subject Database cache consistencyLevel Senior–Staff~26 minCommon in Databases & SQL interviewsIndustries Software development, Technology

Question

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.

Talk through your review
Code to reviewpython
def increment_views(redis, db, post_id):    cached = redis.get(f"views:{post_id}")    if cached is None:        row = db.execute(            "SELECT views FROM posts WHERE id = %s", (post_id,)        ).fetchone()        cached = row[0]    new_val = int(cached) + 1    redis.set(f"views:{post_id}", new_val)    db.execute("UPDATE posts SET views = %s WHERE id = %s", (new_val, post_id))
Run or narrate your approach, then ask the coach.