go-redis/cache

Catering for concurrent caching

charbel14 opened this issue · 3 comments

in a distributed system where stateless services use central Redis for caching, is there a way to protect against concurrency.

For example 1st in wins?

So the flow would be:
Pre-req: Key is the same between Service 1 and Service 2.

Service 1 attempts caching X
Service 2 attempts caching X1

Service 2 should in this specific case fail and instead of setting X1 should issue a fetch of X.

Ideally Service 1 should issue a lock request if successful it issues a set and unlocks. Service 2 attempts a lock but fails so issues a Get.

Any thoughts?

https://github.com/golang/groupcache is designed specifically for such case, but it is not a Redis in any way.

There is https://godoc.org/github.com/go-redis/cache#Codec.Once, but it deduplicates requests locally/separately for service 1 and service 2.

With https://github.com/bsm/redis-lock it is possible to improve Codec.Once to do it globally for service 1 and service 2, but I don't have anything working.

That should work too, but it uses redigo.