Skip to content

🐛 Bug Report: Appwrite main container freezes when executing cloud-functions.  #5629

Closed

Description

@byawitz

👟 Reproduction steps

The problem

Appwrite main container freezes and Swoole is crashing for 30 seconds +- when accessing Appwrite api from a cloud function.

Take this JavaScript function for example: (Applied to other platforms as well)

const sdk = require("node-appwrite");

module.exports = async function (req, res) {
    const client = new sdk.Client();

    const database = new sdk.Databases(client);

    client
        .setEndpoint('https://appwrite.domain.com/v1')
        .setProject('projectid')
        .setKey('KEY')
        .setSelfSigned(true);

    await database.createDocument('DATABASE_ID', 'COLLECTION_ID', sdk.ID.unique(), {time: (+new Date()).toString()});

    res.json({'success': true});
};

When running this function it works perfect.

But when trying to run it more than 3 times per second - with benchmark or simple browser request - the function runs till the function it hit its own timeout and responds with failed status.

A workaround

When running a function in synchronized mode it goes through the same route as any Appwrite API endpoints, Plus, The additional process required to go from the executor to the function (open-runtime) itself.
like such:

User -> Traefik -> Appwrite -> Executor -> function

Then the function itself is calling Appwrite service - let's say to database one - then we have this path:

function -> internet --> Traefik -> Appwrite -> database endpoint,

Something like this. the dashed line marked the sequential request

flowchart BT
    A[User] --> I((Internet))
    I --> tr
    I -.-> tr
    function -.-> I

    subgraph Appwrite
        tr[[Traefik]] --> ar
        tr -.-> ar
        ar{{appwrite}} --> executor
        executor --> function
        ar -.-> database
    end
Loading

So, what happened - from what I've observed - is that the 6-per core Swoole workers get blocked by their own execution.

Meaning, if 3 functions execution happened simultaneously than half of the workers are in busy handling the createExecute function while the other half are handling the
createDocument function.

But if more than half of the available workers are busy handling the createExecute function, then only 2 left for handling the createDocument function, and the
last 2 left stuck in midair causing a chain reaction of functions timeout error.

It also happened when the value _APP_WORKER_PER_CORE is set to a higher value. In this case Swools's freezes after crossing half of the workers per second.

In order to prove it I've tried to run a stressed benchmark on the server for the createDocument endpoint, and because no worker was taken from the inside the request can wait
in line and got execute one after each other.

Also, I've deployed Appwrite-free function and run a stressed benchmark on that one. Like the createDocument Appwrite was able to process all the request by order, Like a charm.

Semi-solution

The final solution for this issue will probably be decided by Appwrite team.

In this solution I've added another container to docker-compose.yml that uses the same appwrite image as the rest, This container acts as duplicate of the main container.

The container is also connected to the runtimes network, so functions can connect to the container using is internal address.

  appwrite-functions:
    image: appwrite/appwrite:1.3.5
    container_name: appwrite-functions
    restart: unless-stopped
    networks:
      - appwrite
      - runtimes
    volumes:
      - appwrite-uploads:/storage/uploads:rw
      - appwrite-cache:/storage/cache:rw
      - appwrite-config:/storage/config:rw
      - appwrite-certificates:/storage/certificates:rw
      - appwrite-functions:/storage/functions:rw
    depends_on:
      - mariadb
      - redis
      - influxdb
    environment:
      - _APP_ENV
      - "Same variable as in the main Appwrite container (this can be change to have less in the future)" 

Then the user can set the function endpoint as follows:

    client
    .setEndpoint('http://appwrite-functions/v1')
    .setProject('projectid')
    .setKey('KEY')
    .setSelfSigned(true);

Using this method makes sure that the function and the API the function is pointing to are using a completely different sets of Swoole workers, which guarantees fail-proof execution by avoiding recursive call to the same workers, Something like this:

flowchart LR
    A[User] --> I((Internet))
    I --> tr
    function -.-> f

    subgraph Appwrite
        f{{appwrite-functions}}
        tr[[Traefik]] --> ar
        ar{{appwrite}} --> executor
        executor --> function
        f -.-> database
    end   
Loading
Same solution in a separate file As many won't want to edit the `docker-compose.yml` file, And, it can make the upgrade process of Appwrite real annoying.

For that you can do as follows:

  1. Create another file next to your docker-compose.yml. name it docker-compose-functions.yml for example.
  2. Add the external snippet below.
  3. Now you can use Docker compose -f flag to start/stop this file.
docker compose -f docker-compose-functions.yml up -d
docker compose -f docker-compose-functions.yml down

Using this method, will let you upgrade Appwrite as much as you want.

External snippet

version: '3'

services:
  appwrite-functions:
    image: appwrite/appwrite:1.3.7
    container_name: appwrite-functions
    restart: unless-stopped
    networks:
      - appwrite
      - runtimes
    volumes:
      - appwrite-uploads:/storage/uploads:rw
      - appwrite-cache:/storage/cache:rw
      - appwrite-config:/storage/config:rw
      - appwrite-certificates:/storage/certificates:rw
      - appwrite-functions:/storage/functions:rw
    depends_on:
      - mariadb
      - redis
      - influxdb
    environment:
      - _APP_ENV

volumes:
  appwrite-cache:
    external: true
    name: appwrite-cache
  appwrite-uploads:
    external: true
    name: appwrite-uploads
  appwrite-certificates:
    external: true
    name: appwrite-certificates
  appwrite-functions:
    external: true
    name: appwrite-functions
  appwrite-config:
    external: true
    name: appwrite-config
  appwrite-executor:
    external: true
    name: appwrite-executor

networks:
  appwrite:
    name: appwrite_appwrite
    external: true
  runtimes:
    name: appwrite_runtimes
    external: true

Another possible solution.

In order to avoid connecting the open-runtime generated container function to the appwrite_appwrite network, the adding of the new duplicated container can work in a similar way of the realtime one.

  appwrite-functions:
    image: appwrite/appwrite:1.3.5
    container_name: appwrite-functions
    restart: unless-stopped
    labels:
      - "traefik.enable=true"
      - "traefik.constraint-label-stack=appwrite"
      - "traefik.docker.network=appwrite"
      - "traefik.http.services.appwrite_function.loadbalancer.server.port=80"
      # http
      - traefik.http.routers.appwrite_function_http.entrypoints=appwrite_web
      - traefik.http.routers.appwrite_function_http.rule=PathPrefix(`/v1/functions`)
      - traefik.http.routers.appwrite_function_http.service=appwrite_function
      # https
      - traefik.http.routers.appwrite_function_https.entrypoints=appwrite_websecure
      - traefik.http.routers.appwrite_function_https.rule=PathPrefix(`/v1/functions`)
      - traefik.http.routers.appwrite_function_https.service=appwrite_function
      - traefik.http.routers.appwrite_function_https.tls=true
      - traefik.http.routers.appwrite_function_https.tls.certresolver=dns
    networks:
      - appwrite

Tests

To check this assumption I've run these tests with oha benchmark tool.

Create Document API

Command

oha https://appwrite.domain.com/v1/databases/DATABASE_ID/collections/COLLECTION_ID/documents \
  -z 180s \
  -m POST \
  -H "Content-Type: application/json" \
  -H "x-appwrite-project: projectid" \
  -H "X-Appwrite-Key: key" \
  -d '{"documentId": "unique()",   "data": {"time": "1"}}'  

Execute function - Just JSON

Command

oha https://appwrite.domain.com/v1/functions/63eae4aedfd2b97fef68/executions \
  -z 60s \
  -m POST \
  -H "Content-Type: application/json" \
  -H "x-appwrite-project: projectid" \
  -H "X-Appwrite-Key: key" \
  -d '{"data":"function-data"}'

Executing this function that doesn't connect to Appwrite at all.

const sdk = require("node-appwrite");

module.exports = async function (req, res) {
    res.json({success: true});
};

Execute function - With Appwrite

Command:

Same as that one

Function code - connects to instance main domain.

const sdk = require("node-appwrite");

module.exports = async function (req, res) {
    const client = new sdk.Client();

    const database = new sdk.Databases(client);

    client
        .setEndpoint('https://appwrite.domain.com/v1')
        .setProject('projectid')
        .setKey('KEY')
        .setSelfSigned(true);

    await database.createDocument('DATABASE_ID', 'COLLECTION_ID', sdk.ID.unique(), {time: (+new Date()).toString()});

    res.json({'success': true});
};

Execute function - With Appwrite (Tunneling)

Command:

Same as that one

Function code - tunnels the client request through the internal docker-host domain appwrite-functions

const sdk = require("node-appwrite");

module.exports = async function (req, res) {
    const client = new sdk.Client();

    const database = new sdk.Databases(client);

    client
        .setEndpoint('http://appwrite-functions/v1')
        .setProject('projectid')
        .setKey('KEY')
        .setSelfSigned(true);

    await database.createDocument('DATABASE_ID', 'COLLECTION_ID', sdk.ID.unique(), {time: (+new Date()).toString()});

    res.json({'success': true});
};

Benchmarks table

Request per second

Server Create Document API Execute function
Just JSON
Execute function
With Appwrite
Execute function
With Appwrite (Tunneled)
Tunneled is
% Faster
1vCPU 1GB 42.81 30.03 0.30 16.23 5400%
2vCPU 2GB 61.64 52.24 0.61 32.03 5250%
4vCPU 8GB 184.01 122.03 1.20 68.68 5720%
8vCPU 16GB 483.07 285.06 113.53 173.48 152%
16vCPU 128GB 1947.54 745.16 343.44 486.80 141%
32vCPU 256GB 3182.35 1389.30 436.87 893.83 204%

As you can see the Tunneled way is faster in any case. and it's a must in low-budget - up to medium - servers.

P.s. This partially related to #4626

👍 Expected behavior

Appwrite should be able to handle all calls, Also in low-budget servers.

👎 Actual Behavior

Up to 8vCPU 16GB Swoole is not able to handle the recursive workers call.

🎲 Appwrite version

Different version (specify in environment)

💻 Operating system

Linux

🧱 Your Environment

I've tested from version 1.1.2 up to 1.3.5

👀 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

🏢 Have you read the Code of Conduct?

Activity

  1. added
    bug
    on Jun 2, 2023
  2. added
    product / functions
    on Jun 2, 2023
  3. joeyouss commented on Jun 5, 2023

    @joeyouss

    Hi
    As discussed in discord, we have started looking into this and discussing about this. More updates soon!

  4. obiwanzenobi commented on Sep 11, 2023

    @obiwanzenobi

    Any update on this?

  5. Meldiron commented on Jan 30, 2024

    @Meldiron
    Contributor

    I have revisited the issue and all of it is very technical and correct.

    I have one concern 🤔 Considering 6 workers (1 core), you seem to get functions to freeze within 4 executions. I cant see logical reason for that, as even with 5 function executions, everything should remain functional - just slower.

    My theory is that if at least 1 worker is doing any request different from synchronous function execution, the stack should not freeze. Because such worker always finishes and becomes available for another request very quickly.

  6. byawitz commented on Jan 30, 2024

    @byawitz
    ContributorAuthor

    Hey Matej, thank you for taking the time to go over the issue.

    The problem is that the second a function calls Appwrite again when there's no available worker, then in this case the function will wait till the timeout is passed which causes a waterfall of timeouts one after another.

    For example:
    4 execution per second, in the first nanosecond.
    Execution 1 -> Appwrite -> Occupying first worker.
    Execution 2 -> Appwrite -> Occupying second worker.
    Execution 3 -> Appwrite -> Occupying third worker.
    At that exact moment, all workers are occupied.

    Execution 4 -> Can't even start the execution as there no worker is available for process the first request.
    But even if only one worker is released and the function starts to execute it is still unable to get a worker starting to wait in the timeout.

    Like so 👇
    image

    After a few executions that bottle neck gets bigger and bigger, and the running function blocks existing workers.

  7. moved this to In Review in 1.5 releaseon Mar 30, 2024
  8. moved this from In Review to Done in 1.5 releaseon Jun 11, 2024
  9. moved this from Done to In Progress in 1.5 releaseon Aug 21, 2024
  10. eldadfux commented on Oct 25, 2024

    @eldadfux
    Member

    We should have a patch ready for this. I pinged the team to release it as a patch version asap.

  11. Colton127 commented on Aug 27, 2025

    @Colton127

    This issue still exists in 1.7.4.

    With the default appwrite configuration, performing more than ~5 concurrent API calls within an execution would lead to a timeout. This problem occurred even while running a single execution.

    After implementing your fix (firing up a 2nd appwrite container to handle requests from functions), I can execute 500+ concurrent API calls from my function without a crash. (Hetzner arm64 server; 4 cores, 8GB RAM).

    For reference, here's my docker-compose.override.yaml

    version: '3.6'
    
    services:
      appwrite-workers:
        image: appwrite/appwrite:1.7.4
        container_name: appwrite-functions
        restart: unless-stopped
        volumes:
          - appwrite-uploads:/storage/uploads:rw
          - appwrite-cache:/storage/cache:rw
          - appwrite-config:/storage/config:rw
          - appwrite-certificates:/storage/certificates:rw
          - appwrite-functions:/storage/functions:rw
        depends_on:
          - mariadb
          - redis
        networks:
          - appwrite
          - runtimes
        environment:
          - _APP_ENV
          - _APP_WORKER_PER_CORE
          - _APP_LOCALE
          - _APP_COMPRESSION_MIN_SIZE_BYTES
          - _APP_CONSOLE_WHITELIST_ROOT
          - _APP_CONSOLE_WHITELIST_EMAILS
          - _APP_CONSOLE_SESSION_ALERTS
          - _APP_CONSOLE_WHITELIST_IPS
          - _APP_CONSOLE_HOSTNAMES
          - _APP_SYSTEM_EMAIL_NAME
          - _APP_SYSTEM_EMAIL_ADDRESS
          - _APP_EMAIL_SECURITY
          - _APP_SYSTEM_RESPONSE_FORMAT
          - _APP_OPTIONS_ABUSE
          - _APP_OPTIONS_ROUTER_PROTECTION
          - _APP_OPTIONS_FORCE_HTTPS
          - _APP_OPTIONS_ROUTER_FORCE_HTTPS
          - _APP_OPENSSL_KEY_V1
          - _APP_DOMAIN
          - _APP_DOMAIN_TARGET_CNAME
          - _APP_DOMAIN_TARGET_AAAA
          - _APP_DOMAIN_TARGET_A
          - _APP_DOMAIN_FUNCTIONS
          - _APP_REDIS_HOST
          - _APP_REDIS_PORT
          - _APP_REDIS_USER
          - _APP_REDIS_PASS
          - _APP_DB_HOST
          - _APP_DB_PORT
          - _APP_DB_SCHEMA
          - _APP_DB_USER
          - _APP_DB_PASS
          - _APP_SMTP_HOST
          - _APP_SMTP_PORT
          - _APP_SMTP_SECURE
          - _APP_SMTP_USERNAME
          - _APP_SMTP_PASSWORD
          - _APP_USAGE_STATS
          - _APP_STORAGE_LIMIT
          - _APP_STORAGE_PREVIEW_LIMIT
          - _APP_STORAGE_ANTIVIRUS
          - _APP_STORAGE_ANTIVIRUS_HOST
          - _APP_STORAGE_ANTIVIRUS_PORT
          - _APP_STORAGE_DEVICE
          - _APP_STORAGE_S3_ACCESS_KEY
          - _APP_STORAGE_S3_SECRET
          - _APP_STORAGE_S3_REGION
          - _APP_STORAGE_S3_BUCKET
          - _APP_STORAGE_S3_ENDPOINT
          - _APP_STORAGE_DO_SPACES_ACCESS_KEY
          - _APP_STORAGE_DO_SPACES_SECRET
          - _APP_STORAGE_DO_SPACES_REGION
          - _APP_STORAGE_DO_SPACES_BUCKET
          - _APP_STORAGE_BACKBLAZE_ACCESS_KEY
          - _APP_STORAGE_BACKBLAZE_SECRET
          - _APP_STORAGE_BACKBLAZE_REGION
          - _APP_STORAGE_BACKBLAZE_BUCKET
          - _APP_STORAGE_LINODE_ACCESS_KEY
          - _APP_STORAGE_LINODE_SECRET
          - _APP_STORAGE_LINODE_REGION
          - _APP_STORAGE_LINODE_BUCKET
          - _APP_STORAGE_WASABI_ACCESS_KEY
          - _APP_STORAGE_WASABI_SECRET
          - _APP_STORAGE_WASABI_REGION
          - _APP_STORAGE_WASABI_BUCKET
          - _APP_COMPUTE_SIZE_LIMIT
          - _APP_FUNCTIONS_TIMEOUT
          - _APP_SITES_TIMEOUT
          - _APP_COMPUTE_BUILD_TIMEOUT
          - _APP_COMPUTE_CPUS
          - _APP_COMPUTE_MEMORY
          - _APP_FUNCTIONS_RUNTIMES
          - _APP_SITES_RUNTIMES
          - _APP_DOMAIN_SITES
          - _APP_EXECUTOR_SECRET
          - _APP_EXECUTOR_HOST
          - _APP_LOGGING_CONFIG
          - _APP_MAINTENANCE_INTERVAL
          - _APP_MAINTENANCE_DELAY
          - _APP_MAINTENANCE_START_TIME
          - _APP_MAINTENANCE_RETENTION_EXECUTION
          - _APP_MAINTENANCE_RETENTION_CACHE
          - _APP_MAINTENANCE_RETENTION_ABUSE
          - _APP_MAINTENANCE_RETENTION_AUDIT
          - _APP_MAINTENANCE_RETENTION_AUDIT_CONSOLE
          - _APP_MAINTENANCE_RETENTION_USAGE_HOURLY
          - _APP_MAINTENANCE_RETENTION_SCHEDULES
          - _APP_SMS_PROVIDER
          - _APP_SMS_FROM
          - _APP_GRAPHQL_MAX_BATCH_SIZE
          - _APP_GRAPHQL_MAX_COMPLEXITY
          - _APP_GRAPHQL_MAX_DEPTH
          - _APP_VCS_GITHUB_APP_NAME
          - _APP_VCS_GITHUB_PRIVATE_KEY
          - _APP_VCS_GITHUB_APP_ID
          - _APP_VCS_GITHUB_WEBHOOK_SECRET
          - _APP_VCS_GITHUB_CLIENT_SECRET
          - _APP_VCS_GITHUB_CLIENT_ID
          - _APP_MIGRATIONS_FIREBASE_CLIENT_ID
          - _APP_MIGRATIONS_FIREBASE_CLIENT_SECRET
          - _APP_ASSISTANT_OPENAI_API_KEY
    

    Endpoint in functions: "http://appwrite-functions/v1"

  12. eldadfux commented on Jan 7, 2026

    @eldadfux
    Member

    Update: this has been addressed and should work much better on latest versions. Closing this issue for now.

  13. moved this from In Progress to Done in 1.5 releaseon Jan 7, 2026
  14. sir-editor commented on Jan 11, 2026

    @sir-editor

    I'm using 1.8.1 self-hosted on x86 server; 4 cores, 8GB RAM.
    I had the same issue with more than 5 executions.
    Suggested fix works for me (500 concurrent requests), thank you!

  15. hej2010 commented on Feb 16, 2026

    @hej2010

    This is still an issue in 1.8.1. Running on 4 cores and 16 GB RAM, and the entire Appwrite stack becomes unresponsive and the logs are spammed with "Risky branch", and the only fix is to stop and restart the Docker containers. You can't even access the Appwrite console because the request times out.
    Image

  16. RadekSip commented on Jul 24, 2026

    @RadekSip

    The bug still exists in 1.8.1, it think it should be reopened.

  17. RadekSip commented on Jul 24, 2026

    @RadekSip

    Thank you very much for explanation of the problem and workaround with a duplicate of "appwrite" and calling of "http://appwrite-functions/v1" in the functions.
    The hint for another - if it fails with error 500 and function is not able to connect to "http://appwrite-functions/v1", check you have disabled forcing of HTTPS in .env file - the container is listening on port 80. (In my case it was useless because my project depends on Cloudflare cfg.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugproduct / functions

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions