r/Redd•
mwpastore

Is Redd thread-safe?

Answered

Is it safe to share a Redd session between two or more threads? Or should I create one Redd session per thread?

Posted Oct 16, 2017, 9:35 PM UTC

32
MustermindRedd Developer•

Hi again! I'd recommend creating a unique Redd session per thread. Redd typically doesn't hold any shared state, but two issues come to mind when talking about thread-safety:

  1. The rate limiter. It limits the speed at which you can make requests (1 per second) by using Kernel#sleep, which isn't exactly thread-safe. You can turn it off using limit_time: 0 when creating the Redd instance, but I would turn it up from 1 second (default) to n seconds if you're making requests from n threads.
  2. Lazy-loading models. Models are lazy-loaded, i.e. the object is only fetched from the API when an attribute is called that doesn't exist locally. If you call it concurrently, you might make the model call an API twice, which is wasteful.

It's all a lot easier if you create separate instances for different threads. Sorry about the late responses, I've been busy. Hope that helps!

2
mwpastoreOP•

Okay, thank you for the guidance. I went ahead and made it a ConnectionPool of Redd instances. It works like a charm, and everything is much faster to boot.

3