20 days ago
Hello, is there any way I could go about reducing the amount of memory use by the provided MySQL database? It's bumping up the monthly bill quite a lot.
Would it be better to migrate to a different DB on Railway that uses less memory, or is there some way to reduce MySQL memory use?
1 Replies
20 days ago
Before thinking about migrating to a different DB you could try to tweak the MySQL start command with the following settings
innodb_buffer_pool_size: This is the most critical memory setting for InnoDB, the default storage engine for MySQL. It caches data and indexes. Set this to an appropriate size based on your available RAM and data set size. A common recommendation is 50-70% of available RAM if MySQL is the primary application on the server. On Railway, you are sharing resources, so you need to be mindful of your service's allocated memory.key_buffer_size: Relevant if you still use MyISAM tables. If you are using InnoDB exclusively, you can set this to a small value (e.g., 8-16MB).tmp_table_sizeandmax_heap_table_size: These settings control the maximum size of in-memory temporary tables. If a temporary table exceeds these limits, MySQL writes it to disk, which is slower. Increase these if your queries frequently create large temporary tables, but be careful not to set them too high as they are per-connection.sort_buffer_sizeand *join_buffer_size: These are also per-connection buffers. Set them to reasonable values based on your typical query patterns. Lower values can reduce overall memory.max_connections: Each connection consumes some memory. Reduce this if you have more allowed connections than your application truly needs.thread_cache_size**: Caches threads for new connections. A larger cache can improve performance for applications with many short-lived connections, but it consumes more memory.You can modify these settings by adding them to your MySQL service's start command on Railway, e.g.:
docker-entrypoint.sh mysqld --innodb-use-native-aio=0 --disable-log-bin --performance_schema=0 --innodb_buffer_pool_size=128M --max_connections=50
Otherwise PosgreSQL is a good option that i've used for a long time. MySQL uses about ~250MB and PostgreSQL uses ~50MB