# Batching and flush (/docs/memory/essentials/batching-and-flush)
`capture` doesn't make a network call. It adds the event to a buffer in your process, and `Dock` sends the buffer to Docklee in batches. Your app never waits on Docklee to capture.

<Callout type="warn">
Short-lived processes such as scripts, jobs, and serverless functions must flush or close `Dock` before they exit. Events still in the buffer are never sent.
</Callout>

## When a batch is sent [#when-a-batch-is-sent]

| Trigger | Python | TypeScript |
| --- | --- | --- |
| The buffer reaches `buffer_size` events | Yes | Yes |
| Every `flushInterval` milliseconds | No | Yes, every 5,000 by default |
| You call `flush_sync()` or `flush()` | Yes | Yes |
| You call `close()` | Yes | Yes |

## Flush and close [#flush-and-close]

<CodeTabs items={['Python', 'TypeScript']}>
  <Tab value="Python">
    ```python
    from docklee import Dock

    dock = Dock(buffer_size=25)
    for message in messages:
        dock.capture(message, type="conversation")

    result = dock.flush_sync()
    ```
  </Tab>
  <Tab value="TypeScript">
    ```ts
    import { Dock } from 'docklee';

    const dock = new Dock({ bufferSize: 25 });
    for (const message of messages) {
      dock.capture(message, { type: 'conversation' });
    }

    await dock.close();
    ```
  </Tab>
</CodeTabs>

| Method | Python | TypeScript |
| --- | --- | --- |
| Send the buffer and wait | `flush_sync()`, or `await flush()` | `await flush()` |
| Send the buffer and stop | `await close()` | `await close()` |

In TypeScript, `Dock` runs a timer while it's open. Call `close()` when you're done so your process can exit.

## When a batch fails [#when-a-batch-fails]

With `flush_sync()` in Python and `flush()` in TypeScript, a failed batch goes back into the buffer and is sent with the next flush. Both return the batch result on success, and `None` or `null` on failure.

Before giving up, the SDK retries timeouts, connection errors, and the statuses `408`, `409`, `429`, `500`, `502`, `503`, and `504`. It waits 0.5 seconds before the first retry and doubles the wait each time, up to 8 seconds.

## Tuning [#tuning]

| Setting | Python | TypeScript | Default |
| --- | --- | --- | --- |
| Events per batch | `buffer_size` | `bufferSize` | `10` |
| Timer interval | none | `flushInterval` | `5000` milliseconds |
| Retries per request | `max_retries` | `maxRetries` | `3` |
| Request timeout | `timeout` (seconds) | `timeout` (milliseconds) | 30 seconds |

A batch holds up to 100 events, so keep `buffer_size` at 100 or below.

## Next steps [#next-steps]

Your captures are reaching Docklee. See what it does with them next.

<Cards>
  <Card title="Contradictions and updates" href="/docs/memory/intelligence/contradictions-and-updates" description="How a newer fact replaces an older one." />
  <Card title="Duplicates" href="/docs/memory/essentials/duplicates" description="How repeated captures are handled." />
  <Card title="Troubleshooting" href="/docs/memory/troubleshooting" description="When captures don't arrive." />
</Cards>
