On this page

Server Persistent Assignment

Configure persistent assignment in Statsig server SDKs so users stay in the same experiment group across requests, sessions, and devices over time.

Persistent assignment lets you ensure that a user's variant stays consistent while an experiment is running, regardless of changes to allocation or targeting.

Persistent storage adapter

The persistent storage adapter lets you plug in your own storage solution that the Statsig SDK uses to persist user assignments. The storage interface provides a load and save API for read/write operations.

Currently only supported in Go, Ruby, Legacy Node, Node Core, Java Core, Kotlin, .Net, Python Core, PHP Core, Rust Core

Persistent storage logic

  • Providing a storage adapter at Statsig initialization gives the SDK access to read and write to your custom storage.
  • Providing user persisted values to get_experiment tells the SDK to:
    • save the current user's evaluation on first evaluation (only when the experiment or layer is active)
    • load the previously saved evaluation on subsequent evaluations
  • Statsig deletes persisted values when:
    • You call getExperiment with user_persisted_values=None
    • The experiment isn't active

Persistent assignment options (limited SDK support)

  • Enforce Targeting: boolean, default: false
    • Whether or not to enforce targeting rules before assigning persisted values
kotlin
val options = GetExperimentOptions(
  ...
  persistentAssignmentOptions = PersistentAssignmentOptions(
    enforceTargeting = true,
  )
)

Example usage

ruby
Statsig.initialize(
  'secret-key',
  StatsigOptions.new(
    user_persistent_storage: DummyPersistentStorageAdapter.new
  )
)
persisted_user = StatsigUser.new({ 'userID' => 'test-123' })
exp = Statsig.get_experiment( # User gets saved to persisted storage
  persisted_user,
  'active_experiment',
  Statsig::GetExperimentOptions.new(
    user_persisted_values: Statsig.get_user_persisted_values(persisted_user, 'userID')
  )
)
puts exp.group_name # 'Control'
exp = Statsig.get_experiment( # User evaluates using values from persisted storage
  StatsigUser.new({'userID' => 'unknown'}),
  'active_experiment',
  Statsig::GetExperimentOptions.new(
    user_persisted_values: Statsig.get_user_persisted_values(persisted_user, 'userID')
  )
)
puts exp.group_name # 'Control'

Was this helpful?