New System Design Knowledge

CREATED 2021/11/08 22:16 PM

Redis

How Redis deletes expired keys

Redis does 10 times per second:

1 Test 20 random keys from the set of keys with an associated expire.

2 Delete all the keys found expired.

3 If more than 25% of keys were expired, start again from step 1.

This is a trivial probabilistic algorithm, basically the assumption is that our sample is representative of the whole key space, and we continue to expire until the percentage of keys that are likely to be expired is under 25%.

Reference

https://redis.io/commands/expire#how-redis-expires-keys

Idempotency

Update 2021/12/19

Idempotency is a Web API design principle defined as the ability to apply the same operation multiple times without changing the result beyond the first try.

Solution: Clients can safely retry requests that include an idempotency key as long as the second request occurs within 24 hours (expiration time) from the first time they receive the key. To perform an idempotent request, provide an additional Idempotency-Key: <key> header to the request.

An idempotency key is a unique value generated by the client which the server uses to recognize subsequent retries of the same request. Results are only saved if an API endpoint started executing. If it failed validation or conflicted, no idempotent result is saved because no execution. Hence it is safe to retry these requests. The idempotency key can be V4 UUIDs.

Save the resulting status code and body of the first request made for any given idempotency key, regardless of whether it succeeded or failed. Subsequent requests with the same key return the same result, including 500 errors.

All POST requests accept idempotency keys. Sending idempotency keys in GET and DELETE requests has no effect and should be avoided, as these requests are idempotent by definition. (The potential problem with DELETE, which if successful would normally return a 200 (OK) or 204 (No Content), will often return a 404 (Not Found) on subsequent calls.)

Retry with exponential backoff and random jitter(Avoid thundering herd problem).

Reference

https://stripe.com/docs/idempotency
https://stripe.com/docs/api/idempotent_requests
https://www.youtube.com/watch?v=nnMqSQtSZUQ&list=PLy1nL-pvL2M50RmP6ie-gdcSnfOuQCRYk&index=7
https://tianpan.co/notes/43-how-to-design-robust-and-predictable-apis-with-idempotency

Interview Example

https://www.1point3acres.com/bbs/thread-748060-1-1.html
https://www.1point3acres.com/bbs/thread-814188-1-1.html

posted @ 2021-11-09 14:21  YBgnAW  阅读(38)  评论(0编辑  收藏  举报