← 返回日报
略读 预计 4 分钟

lobste.rs is now running on SQLite

摘要

作者于 2026 年 7 月 11 日完成部署,生产环境平稳运行。过去周六部署 SQLite PR 后,观察到周一流量高峰时 CPU 使用量下降、内存使用量下降、站点更流畅,VPS 成本减半(Mariadb VPS 关闭)。PR #539 已关闭。背景:2025 年 2 月讨论后,作者 2025 年 6 月接手项目,多次测试与调试后成功迁移。解决过首次部署的只读模式下 CPU 100% 问题,监控修复了少数报告问题。SQLite 特定事项包括:使用 UDF 支持 regexp/if/stddev 函数、切换 unsigned bigint 到 bigint、切换 utf8mb4_general_ci 到 NOCASE(仅 ASCII 支持)、启用 Contentless-Delete Tables。Rails 与 Lobsters 代码特定事项包括默认 PRAGMAs、移动旧 migrations 目录、保留搜索解析器、inline partials、测试套件。整体成功依赖良好沟通,无生产数据库访问下的迁移难度,以及未来数据集大小规划。

荐读理由

SQLite 迁移经验:为 Rails 项目用 UDF 实现缺失函数、迁移 unsigned bigint 为 bigint、改 utf8mb4 collation 为 NOCASE(仅限 ASCII)、切换 contentless-delete tables 做全文搜索。部署前测试、慢查询日志、PRAGMA 优化、inline partials 优化搜索。

原文

This past Saturday, @pushcx and I deployed the SQLite pull request to production. We were waiting till this morning to see how it would react to the Monday traffic spike before making this post. Needless to say, SQLite seems to have passed with flying colors: cpu usage is down, memory usage is down, site seems to be snappier at least for me, 1/2 the vps cost once mariadb vps is taken down, and finally "We're having a quiet Monday.". Finally #539 Migrate to SQLite was closed this morning. Let us know if you have any questions about the migration. Background Story: I got involved with this migration because back in 2019 I stumbled upon #539 and because I had lots of experience working with, managing and migrating largish databases, I left a comment suggesting MySQL as an alternative, because of the compatibility between MariaDB and MySQL. At that time I wasn't planning on getting involved since there were already conversations in place to migrate to PostgreSQL. Fast forward to 2025, Rahul left a comment mentioning K1's acquisition of MariaDB. A discussion around the details of migrating to postgresql proceeded. Then in February, Rahul asked "Can lobsters run on sqlite? which included a very detailed post around SQLite. I officially showed interest in taking on this project in June 2025. I think this somehow got mentioned in lobsters office hours but it has been so long since then that I don't rememeber for certain. In August 2025 I opened my first pull request attempt when I got busy and couldn't attend to the PR. Github closed it as stale and I couldn't reopen it so I opened another PR. The second PR attempt included some performance testing, a database x to database y script (since none of the existing mariadb/mysql to sqlite scripts satisfied me), debugging and thinking around data integrity. Then came the first deploy on Feb 21st. @pushcx and I got on a call, came up with a checklist for the deployment. Everything went right up until the deployment of the PR. Once deployed the site was in readonly mode, but just the readonly traffic was spiking all the cpus to 100%. We couldn't figure out what the problem was so we decided to revert. I didn't feel great after that first failed deploy since I knew that performance could be a problem due to not having access to the production database. Two days after the failed deploy I opened the 3rd and final pr attempt. I fixed some minor issues with search that were discovered during the failed deploy, created a bulk data creation script which took a week to get half of lobsters' data set size created locally, and committed the three changes that fixed the performance issues during the first deploy: 1, 2, 3. The performance issues boiled down to SQLite doing full table scans on the largest tables in the database for 2 of queries and the 3rd one solved an n+1 issue. During the morning of the second deployment, I also added a slow query log just in case there were more performance issues during the deployment. Then came the second deploy on July 11th. @pushcx and I got on a morning call and came up with a deployment and revert checklist. Everything was going smoothly and then @pushcx merged and deployed the PR. Once deployed the site was still live and the cpu/memory usage was still good. This was a big relief for me. We monitored site metrics and irc for people mentioning issues, which a couple did and those were promptly fixed: 1, 2. Overall the site seemed to work so we wrapped up the call and waited till Monday when the traffic spikes. Monday came and the site is stil happy so we're calling this a win and moving on. SQLite lessons: 1. The SQLite gem supports user defined functions (udfs) and we used it to implement some missing functions in SQLite like regexp, if and stddev so that we wouldn't have to deal with too many sql migration workarounds. 2. SQLite doesn't support unsigned bigints. Previously, the mariadb was using unsigned bigints for certain ids, so we had to switch those to bigints for the migration. 3. Collation in SQLite is rather weak compared to MariaDB. Lobste.rs used utf8mb4_general_ci in MariaDB, but used NOCASE in SQLite. The downside of NOCASE is that it only supports ASCII characters, not the full UTF case folding. 4. Use the preferred Contentless-Delete Tables in SQLite for your full text search tables. These are not the default. I'm constantly surprised by the default choices of SQLite. Rails lessons: 1. The default PRAGMAs in Rails seem to be working for lobsters. 2. You typically don't think about it but database migrations are database specific. I had to move the old migrations out to an old migrations directory so that db:migrate would continue to work. Lobsters codebase lessons: 1. There is a search parser in the lobsters codebase. 2. I learned about heinous_inline_partials which is a hack to speed up rendering. 3. The lobsters testsuite was essential in making sure I could migrate to SQLite without a ton of manual testing. Overall lessons: 1. I think a key ingredient in making this work was good communication from everyone that participated. I don't think this would have been possible otherwise. 2. Migrating the underlying database without having access to the production database is really hard to get right. This was my first underlying database migration without having access to production. One lesson I'll take away from this is that I'll make sure to have realistic dataset sizes before doing another underlying database migration in the future. Wishes: 1. I wish we could say in a test, "Fail if you encounter any full table scans". Which would have caught the perf issues we experienced during the first deploy. 2. I wish creating a production like dataset would be much easier than having to manually write something and waiting a week.

Lobsters · 提问/讨论 · 21 赞 · 2 评 阅读原文 →

这条对你有帮助吗?