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 it is simple, over time, the speed of Waline 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 found some issues with the loading speed. So, I decided to switch databases and migrate from 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
.
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 from a repository in Waline.
You can find the multi-node connection information here:
Select
Drivers
as the connection method,Driver
as Node.js, andVersion
as2.2.12 or later
The part highlighted by the yellow marker is the multi-node 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 that Vercel's serverless function has 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 user registration information. Keep it and do not delete it. Then, redeploy Waline on Vercel. After deployment, access the admin page, register a new account or use OAuth to register. After logging in, perform the import operation 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 indexing 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 has 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 retry the import operation, 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, such as page views and comment information (Counter
and Comment
collections), can improve Waline's response speed.
References#
Hexo 去 LeanCloud 依赖 | Finisky Garden
Pitfall#
Later, I found that almost all the green checkmarks indicating registered users were missing, which was quite annoying.
Then, when I checked the database, I found that almost all the comments had an empty user_id
field. When I compared it with the User
table, I realized that the user_id
field here is actually the id
at the beginning of each data in MongoDB. Now, I had to set the user information field for the previous comments again.
Because I don't have much experience in writing scripts, I did all these operations manually in Navicat. It was quite tiring...