StaffCoder
Redis
RedisJava 25Node.js 24Python 3.13
1 module · 4 challengesSign up
Module 1 · Strings & Key Basics

Conditional Sets & Counters

easy50%1 of 2 solved

Problem statement

Two workers can try to claim the same job at the same time, and a read followed by a write leaves a gap where both of them succeed. Redis can decide each of these writes, and every counter update, in a single command.

Requirements

The connected Redis client and key_for(token), logins_key(user_id) and spend_key(user_id) are already provided. Your task has seven methods in src/token_claim.py. Each write must be one Redis command so another worker cannot change the result between a read and a write.

  • claim(token, owner, ttl_seconds) stores the owner only when the token is absent and returns whether it stored the owner.
  • A successful claim(token, owner, ttl_seconds) gives the token a positive lifetime no greater than the requested number of seconds.
  • refresh_if_present(token, value) replaces an existing token and returns whether it replaced one. It does nothing for an absent token.
  • swap(token, value) replaces the value and returns the previous value, or None when there was none.
  • record_login(user_id) increases the integer counter and returns the new total.
  • add_logins(user_id, n) adds n to the integer counter, and undo_login(user_id) subtracts one.
  • record_login(user_id) on a counter that holds non-integer text raises an error to the caller and leaves the stored text unchanged.
  • add_spend(user_id, amount) adds a decimal amount and returns the new total.
Sample Usage

claim("job-1", "worker-a", 60)

True

claim("job-1", "worker-b", 60) while worker-a holds the token

False

add_spend("u1", 10.50)

10.5

record_login("u3") when the counter holds not-a-number

redis.exceptions.ResponseError: value is not an integer or out of range

Note: Refer to the README.md file for detailed implementation requirements.

More in Strings & Key Basics

See the module
First KeysMove a session store off a per-process map and into Redis. Store session values under a defined key, read them back exactly as written including blobs that are not text, report whether a key exists and what type it holds, and delete keys — returning the number Redis actually removed.40%2 of 5 solvedeasyKey Naming & ScanningGive a sprawling keyspace one namespaced scheme, then iterate it without stopping the server: drive SCAN's cursor to completion with MATCH and COUNT rather than calling KEYS, discarding the duplicates a cursor can return. Rename keys with and without overwriting, set expiries, and remove large keys with the non-blocking delete.100%1 of 1 solvedeasySession Token StoreBuild the session store the whole service signs people in with: issue a token exactly once with an expiry, refresh it while it is in use, revoke it on sign-out, count logins per user, and audit a user's open sessions on a live keyspace — using a cursor, never KEYS, and freeing keys without making other clients wait.100%1 of 1 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.