---
title: Event batching for the React Native SDK
slug: feature-experimentation/docs/event-batching-reactnative-prior-to-v4
docTags: 
createdAt: 2026-08-05T20:39:53.000Z
---

The React Native SDK lets you batch events and includes options to set a maximum batch size and flush interval timeout. The benefit of event batching is less network traffic for the same number of decision and conversion events tracked.  By default, event batching is enabled in React SDK.

The event batching functionality provides support for asynchronous, offline event batching in React Native applications. The events dispatched while the device was offline are stored in a persistent cache on users device. The failed events are then re-dispatched when:

- Internet connectivity is restored.
- A new event is ready to be dispatched.
- SDK is initialized the next time.

:::hint{type="info"}
**Note**
Event batching works with both out-of-the-box and custom event dispatchers.

The event batching process does not remove any personally identifiable information (PII) from events. You must still ensure that you are not sending any unnecessary PII to Optimizely.
:::

## Configure event batching

Optimizely provides three options to configure event batching: `eventBatchSize` , `eventFlushInterval` and `eventMaxQueueSize`. You can pass in all the options during client creation. Events are held in a queue until either:

- The number of events reaches the defined `eventBatchSize`.
- The oldest event has been in the queue for longer than the defined `eventFlushInterval`, which is specified in milliseconds. The queue is then flushed and all queued events are sent to Optimizely in a single network request.
- A new datafile revision is received. This occurs only when live datafile updates are enabled.

For the React Native users, `eventMaxQueueSize` represents the maximum number of events that can be stored in the persistent cache when the device is offline.

```javascript
import { createInstance } from '@optimizely/react-sdk';

const optimizely = createInstance({
  datafile: window.datafile // assuming you have a datafile at window.datafile
  // other options
  eventBatchSize: 100,
  eventFlushInterval: 3000,
  eventMaxQueueSize: 10000,
});
```

```javascript
const optimizelySdk = require('@optimizely/optimizely-sdk')

optimizelySdk.createInstance({
  // other options
  eventBatchSize: 100,
  eventFlushInterval: 3000,
})
```

The following table defines these options and their default values.

| Name                   | Default value | Description                                                                                                                                                                                                                                                                                                                      |
| ---------------------- | ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **eventBatchSize**     | 10            | The maximum number of events to hold in the queue. Once this number is reached, all queued events are flushed and sent to Optimizely. \* \\\*Note\\\*\\\*: By setting this value to 1, events are not batched and event requests behave exactly as in pre-3.3.0 version of the React Native SDK.                                 |
| **eventFlushInterval** | 1000 ms       | The maximum duration in milliseconds that an event can exist in the queue before being flushed.                                                                                                                                                                                                                                  |
| **eventMaxQueueSize**  | 10000         | The maximum number of event batches stored in persistent cache on a user's device when the events fail to dispatch because there is no Internet connectivity. The size of each batch stored is determined by \`eventBatchSize\`. As soon as you regain connectivity, all batches are flushed (bypassing \`eventFlushInterval\`). |

:::hint{type="danger"}
**Warning**
The maximum payload size is 3.5 MB. Optimizely rejects requests with a 400 response code, `Bad Request Error`, if the batch payload exceeds this limit.

The size limitation is because of the [Optimizely Events API](docId:822ZQOB6Zk2-kWJKavBvA), which the SDK uses to send data to Optimizely.

The most common cause of a large payload size is a high batch size. If your payloads exceed the size limit, try configuring a smaller batch size.
:::

## Close Optimizely on application exit

If you enable event batching, you **must** call the Close method (`optimizely.close()`) before exiting. This ensures that queued events are flushed when possible to avoid data loss.

:::hint{type="info"}
**Warning**
Because the Optimizely client maintains a buffer of queued events, you must call `close()` on the Optimizely instance before shutting down your application or whenever dereferencing the instance.
:::

| Method      | Description                                                                                                                                                                                                                    |
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **close()** | Stops all timers and flushes the event queue. This method will also stop any timers that are happening for the datafile manager. \* \\\*Note\\\*\\\*: You should connect this method to a kill signal for the running process. |
