Introduction#
Since the era of Hexo, my Waline has been hosted on Vercel, and the data storage used is LeanCloud International Edition. This is also the most recommended deployment method by the official Waline. Although simple, over time Waline's speed will inevitably be limited by LeanCloud. Previously, Teacher Du commented on my blog post "Talking about Interpersonal Communication" that my Waline was loading too slowly.
Later, when I was reading articles and replying to comments, I also noticed some speed issues. So I decided to switch databases and migrate out of LeanCloud to optimize the performance of the comment section.
Process#
Configuring the Database and Waline#
First, I created an Azure free M0 database on MongoDB Cloud in the Hong Kong region, with a storage space of 512 MB, which is more than enough for Waline. If it's not enough, I can migrate to the free 5 GB TiDB in the future.
After creating it, allow access requests from all IP addresses on the "Network Access" page, and then copy the connection string in SRV format. In Navicat, use the connection string to connect to MongoDB and create a database named waline
, and remember the name for later use.
Open the Waline project deployed on Vercel, go to the environment variable settings, delete the original LeanCloud-related configuration items, and then fill in the MongoDB connection information according to Waline's documentation. Here's one thing to note: MongoDB Cloud's M0 database does not have cluster information. If you set the MONGO_REPLICASET
incorrectly, Waline will encounter a 500
connection timeout error when retrieving comment information. I got stuck here during the migration, and later found out about this issue in the Waline repository through a search.
You can find the multi-machine connection information here:
Select
Drivers
as the connection method,Driver
as Node.js,Version
as2.2.12 or later
The part highlighted by the yellow marker is the multi-machine connection information. Do not include the port number and fill it into the environment variables according to the format in the Waline documentation.
:::warning Note
The region of your Vercel project also needs to be set to the same region as the database or as close as possible, otherwise the speed will be greatly reduced!
Location of the database
Location of the project
:::
Backup and Restore Data#
After configuring, go to the "Import/Export" page in the Waline admin panel to export all data. If the exported JSON file is not automatically downloaded by the browser after a while, it is likely because Vercel's serverless function timed out. In this case, try exporting again and it should work.
The exported file is a standard JSON file that contains all the comments and registered user information. Keep it and do not delete it. Then redeploy Waline on Vercel. After the deployment is complete, access the admin page, register a new account or use OAuth to register. After logging in, perform the import operation again on the "Import/Export" page, and select the JSON file that contains all the Waline data exported earlier. Waline will automatically start the import and index creation process.
Here, it is recommended to open the browser's DevTools and switch to the "Network" panel. This can help determine if Waline's import operation is proceeding normally. If the import suddenly stops and the progress indicator on the admin page disappears before completion, it is likely that Vercel's serverless function timed out (each Vercel serverless function in the Hobby plan can only run for a maximum of 10 seconds, otherwise it will time out and return a 504
error). In this case, no matter what, just perform the import operation again, and it should work.
I did not encounter any import failures, so I can only provide this solution.
After the import is complete, everything should be back to normal. Log out of the comment section, and log in using the account and password used when Waline was using LeanCloud as the database. Then, go through each blog post and you will notice a significant improvement in the loading speed of the comments. Test the comments on the demo page and you will also notice a significant improvement compared to LeanCloud.
Other Optimizations#
MongoDB has an "index" feature that can speed up the response time of the database when reading large collections. Therefore, setting indexes for some large collections can improve Waline's response speed.
These large collections are usually page views and comment information (Counter
and Comment
collections), so setting indexes for them is sufficient.
References#
Hexo 去 LeanCloud 依赖 | Finisky Garden
Pitfalls#
Later, I found that almost all the green checkmarks indicating registered users were gone, which was quite frustrating.
Then I checked the database and found that almost all comments had an empty user_id
field. When I opened the User
table for comparison, I realized that the user_id
field here is actually the id
at the beginning of each data in MongoDB. Now, I had to reset the user information field for the previous comments.
Because I don't have much experience in writing scripts, I did all these operations manually in Navicat. It was quite tiring...