growthbook / growthbook-sdk-java

The Java SDK for GrowthBook (JVM, Android)
https://docs.growthbook.io/lib/java
MIT License
7 stars 10 forks source link

Clarification - use attributes to determine a proper value #25

Closed Maciej-Monster closed 1 year ago

Maciej-Monster commented 1 year ago

Hello, thank you for bringing up java SDK!

I played around with it for my Kotlin server-side project.

Could you confirm if I use it correctly?

In my use case every request has different set of attributes and based on them GrowthBook returns a proper value.

I set up the repository for fetching & refreshing features. Then I create a new GrowthBook each time and pass features & attributes based on the request. So in order to achieve this I need to create a new GrowthBook each time, right? This is the only possible way?

tinahollygb commented 1 year ago

Thanks for your question @Maciej-Monster

The GBFeaturesRepository (for fetching and refreshing features) should be a singleton that lives for the life of your app, it should not be created for every request and there should only be one. More on this in the docs: https://docs.growthbook.io/lib/java#fetching-cacheing-and-refreshing-features-with-gbfeaturesrepository

But the GBContext and GrowthBook classes should be created for each user and should only live for the life of the request. This is the better way to do this because if you have 1 shared GrowthBook instance and you get 100 requests at the same time, there's no way to tell whose attributes will be used for the targeting conditions if they all call setAttributes() at the same time. For that reason, use a new GrowthBook instance each time.

So based on what you're saying, it sounds like you've got it right. You're welcome to join the Slack for general questions like this, and if you wanted to you can share some code there, but it sounds like you've got it right!

Not sure if you saw, but we have some examples in our examples repository:

Maciej-Monster commented 1 year ago

Thank you for responding so quickly! 🙏