Use Remote Config in server environments


Firebase Remote Config supports server-side configuration using the Firebase Admin Java SDK v9.7.0+. This capability empowers you to dynamically manage the behavior and configuration of server-side applications using Remote Config. This includes serverless implementations like Cloud Functions.

Unlike Firebase client SDKs, which fetch a client-specific configuration derived from the Remote Config template, the server-side Remote Config SDK fetches a complete Remote Config template from Firebase. Your server can then evaluate the template with each incoming request and use its own logic to serve a customized response with very low latency. You can use conditions to control and customize responses based on random percentages and attributes defined in custom signals.

With server-side Remote Config, you can:

  • Define configuration parameters for applications running on or accessed through your server, allowing for use cases like remotely configuring AI model parameters and prompts and other integrations, to ensure your API keys stay secure.
  • Dynamically adjust parameters in response to changes in your environment or other application changes, like updating LLM parameters and model endpoints.
  • Control costs by remotely updating the APIs your server calls.
  • Generate custom configurations on-the-fly for clients that access your server.
  • Record which clients received a parameter value and use this in Cloud Functions as part of an entitlement verification system.

You can deploy server-side Remote Config on Cloud Run, Cloud Functions, or self-hosted server environments.

Before you begin

Follow the instructions in Add the Firebase Admin SDK to your server to create a Firebase project, set up a service account, and add the Firebase Java SDK to your server.

Step 1: Initialize the Firebase Admin Java SDK and authorize API requests

When you initialize the Admin SDK with no parameters, the SDK uses Google Application Default Credentials and reads options from the GOOGLE_APPLICATION_CREDENTIALS environment variable. To initialize the SDK and add Remote Config:

  // Initialize the Firebase Admin SDK
  FirebaseOptions options = new FirebaseOptions.Builder()
      .setCredentials(GoogleCredentials.fromStream(serviceAccount))
      .build();
  FirebaseApp.initializeApp(options);

Step 2: Identify default parameter values for your server application

Identify the variables in your app that you want to dynamically update with Remote Config. Then, consider which variables must be set by default in your application and what their default values should be. This ensures that your application runs successfully even if its connection to the Remote Config backend server is interrupted.

For example, if you are writing a server application that manages a generative AI function, you might set a default model name, prompt preamble, and a generative AI configuration, like the following:

Parameter name Description Type Default value
model_name Model API name String gemini-2.5-flash
preamble_prompt Prompt to prepend to the user's query String I'm a developer who wants to learn about Firebase and you are a helpful assistant who knows everything there is to know about Firebase!
generation_config Parameters to send to the model JSON {"stopSequences": ["I hope this helps"], "temperature": 0.7, "maxOutputTokens": 512, "topP": 0.9, "topK": 30}

Step 3: Configure your server application

After you've determined the parameters you want to use with Remote Config, configure your application to set default values, fetch the server-specific Remote Config template, and use its values. The following steps describe how to configure your Java application.

  1. Access and load the template

    // Initialize server side Remote Config client
    FirebaseRemoteConfig rc = FirebaseRemoteConfig.getInstance();
    

    Alternatively, if you're using Java with Cloud Functions, you can use the asynchronous getServerTemplate to initialize and load the template in a single step:

     template = rc.getServerTemplate();
    
  2. To ensure that your application runs successfully even if its connection to the Remote Config backend server is interrupted, add default values for each parameter to your app. To do this, add a defaultConfig inside your getServerTemplate template function:

    // Initialize default config using KeysAndValues
    KeysAndValues defaultConfig = new KeysAndValues.Builder()
                                      .put("rc_param_1", "default value 1")
                                      .put("rc_param_2", "default value 2")
                                      .build();
    ServerTemplate template = rc.getServerTemplate(defaultConfig);
    

    Alternatively, you can pass a default cached template while initializing Remote Config which can be used even when the connection to RC server is interrupted.

    templateDataJSON = "{}" // add your default template as json
    ServerTemplate template = rc.serverTemplateBuilder()
                        .defaultConfig(defaultConfig)
                        .cachedTemplate(templateDataJSON)
                        .build();
    
  3. After the template loads, use template.evaluate() to import parameters and values from the template:

  4. Optionally, if you set conditions in your Remote Config template, define and provide the values you want:

    • If using percentage conditions, add the randomizationId that you want to use as context to evaluate your condition(s) within the template.evaluate() function.
    • If using custom signals, define the attributes and their values. Custom signals are available with Firebase Admin Java SDK v9.7.0 and higher.
    // Add custom signals and randomizationID
    KeysAndValues context = new KeysAndValues.Builder()
                                    .put("custom_signal_key", "99")
                                    .put("randomizationId", "2ac93c28")
                                    .build();
    
    // Evaluate the template with signals
            ServerConfig config = template.evaluate(context)
    
  5. Next, extract the parameter values you need from the config constant. Use getters to cast the values from Remote Config into the expected format. The following types are supported:

    • Boolean: getBoolean
    • String: getString
    • Double: getDouble
    • Long: getLong
    • Value source: getValueSource

    For example, if you are implementing Vertex AI on your server and want to change the model and model parameters, you might want to configure parameters for model_name and generation_config. Here's an example of how you could access Remote Config's values:

    // Get the model name
    String modelName = config.getString("model_name");
    // Get the origin of value assigned to param
    ValueSource modelNameSource = config.getValueSource("model_name");
    

    The method getValueSource() returns a ValueSource that indicates that the parameter's value source can be STATIC, REMOTE, or DEFAULT.

  6. If your server is long-running, as opposed to a serverless environment, use java.util.Timer to periodically reload the template to ensure that you're periodically fetching the most up-to-date template from the Remote Config server.

Step 4: Set server-specific parameter values in Remote Config

Next, create a server Remote Config template and configure parameters and values to use in your app.

To create a server-specific Remote Config template:

  1. Open the Firebase console Remote Config parameters page and, from the Client/Server selector, select Server.
  2. Define Remote Config parameters with the same names and data types as the parameters that you defined in your app and provide values. These values will override the default_config you set in Configure your server application when you fetch and evaluate the template and assign these values to your variables.
  3. Optionally, set conditions to persistently apply values to a random sample of instances or custom signals you define. For more information about conditions, see Condition rule types.
  4. When you've finished adding parameters, click Publish changes.
  5. Review the changes and click Publish changes again.

Step 5: Deploy with Cloud Functions or Cloud Run

If your server application is lightweight and event-driven, you should consider deploying your code using Cloud Functions. For example, if you have an app that includes character dialogue powered by a generative AI API (for example, Google AI or Vertex AI). In this case, you could host your LLM-serving logic in a function that your app calls on-demand.

If your application is intended to be long-running (for example, a web app with assets), you might consider Cloud Run. To deploy your server app with Cloud Run, follow the guide at Quickstart: Deploy a Java service to Cloud Run.

For more information about the best use cases for Cloud Run and Cloud Functions, refer to Cloud Functions vs. Cloud Run: when to use one over the other.