tumblr / colossus

I/O and Microservice library for Scala
Apache License 2.0
1.14k stars 96 forks source link

Example for http caching proxy #593

Closed ryudice closed 7 years ago

ryudice commented 7 years ago

Any examples on how to use colossus as a caching proxy? would be good to have one in the examples.

benblack86 commented 7 years ago

@ryudice can you give more details of what you are looking for? You want to see an example of a service that receives a http request, if in internal memory then return that, otherwise use http client to get the response. Is that the idea?

benblack86 commented 7 years ago

Going to close due to no response

ryudice commented 7 years ago

Hi @benblack86, We are trying to use colossus as a caching proxy. We want to have a microservice built with colossus that will receive all requests and cache the response if it is the first time receiving the request. We are going to use Redis for the caching.

We already built something, however we are having issues with the synchronization. When we get 100 simultaneous requests, all requests will go to the proxied microservice, instead of just the first request and subsequent requests going only to redis.

Hopefully I make myself clear.

Thanks, Roberto

benblack86 commented 7 years ago

I understand. If you can share the code, maybe I can identify the problem.

ryudice commented 7 years ago

@benblack86, sure, here is the code. Thanks!

https://gist.github.com/ryudice/4b0e397c2b7698974c925944fc62c67f#file-colossus proxy-scala

benblack86 commented 7 years ago

Ok oh, I understand. You seem to be using the url object request.head.url to synchronize on, but that object will be different on every request.

If you receive 100 simultaneous requests, then presumably most of them will be able to check the cache and notice it is empty, before the cache gets populated by the first request. If you really want to synchronize this behavior then you will want to create a global lock that is passed into the ProxyInitializer, which can be shared between all colossus worker threads. However, I'm not sure why you want to do this as it would badly affect performance. The race condition doesn't seem to be a harmful one.