Should I instantiate an object every request or once upon app launch?

103 views Asked by At

In my Rack app, I need to use an AWS Cognito Client for signup & login requests.

Should I initialize:

  1. a separate client (assigned to a local variable inside the call method) for each request?
  2. one client (assigned to a global variable outside the call method) for all requests?

Concerns:

  1. Speed & cleanup: Is initialization time fast? Is the client automatically destroyed at the end of each request? I studied the source code but didn't grasp the metaprogramming.
  2. Thread safety: Is this option thread safe? I have Puma configured to use multiple threads.
1

There are 1 answers

2
Albert Vaca Cintora On BEST ANSWER

Cognito is most useful when you delegate to your end users obtaining credentials and making calls to AWS themselves, so it's not usual to need Cognito in the server side.

Edit: If you want to implement developer authenticated identities, then it definitely makes sense to use a Cognito service client so you can call GetOpenIdTokenForDeveloperIdentity. It is safe to do so from a multi-thread environment, because no state at all is saved between invocations of that call.