Should you use MySQL with a Minecraft plugin?
Posted on December 23, 2020 in minecraft
Tagged with: minecraft servers
The Minecraft server community has many misconceptions floating around about using MySQL or other databases with plugins, some of which cause servers to perform significantly worse. In this article, I'll go over how to make the best decisions around database use as both a server admin and a plugin developer.
While configuring a plugin, you'll often notice the ability to store data in MySQL instead of in files. MySQL can have advantages, as well as drawbacks. The main point of an external database such as MySQL is to offload fetching of data, allowing high-speed access to small sections of a large amount of data.
For plugins that only need small amounts of data at a time, such as economy or block logging, this can be a great idea. For other plugins that need all data loaded at startup, using MySQL is a bad idea. Some examples of plugins that need to load all data are region or town plugins.
In terms of plugins that load data for a single player when they log in, this is a bit of a grey area. In general, you should not use a database in this case, unless the data persists across multiple servers. Some examples of plugins like this would be permission plugins.
In general, if a plugin supports both MySQL and files, it's likely not written in a way that will benefit from MySQL. While this isn't always the case, a good rule of thumb is only to use an external database if it's the default option or needed for other functionality (such as sharing data between servers).
When deciding whether your plugin should use a database such as MySQL or not, you should first identify how your plugin needs to access data. If your plugin requires a large portion of your data always readily available or does wide-spread lookups of the information on the main thread, it may be best to load the data from a file.
Databases are best suited for accessing small amounts of data asynchronously. You should never perform a data lookup from the main thread to a database in a Minecraft plugin.
The biggest differentiator in loading data from a file or database is that generally files load the entire contents, while a database loads small amounts of the data. If you only need a player's data while connected, storing a file per-player and loading on player join is likely a good option. If you have a considerable amount of data with unpredictable access patterns, using a database makes more sense.
The most common form of NoSQL database used in Minecraft plugins is a document database, such as MongoDB. These are very different than MySQL and other SQL databases, and should never be used in the same way. Document databases act more like remote files, rather than a way to query data. These can be the right choice for plugins that store a file per-player and load the data on player join but need the data available on multiple servers.
There's no definitive rule of whether you should or should not use a database, but there are situations that better suit one or the other.
The most important thing to know is that no format is better than others; they suit different situations.
Like this article? Why not share it with others!
Check out these popular related posts!
Posted on August 26, 2020
Paper and Spigot are the two leading platforms for Minecraft servers. Which is better? This article goes over the main benefits of using both platforms
Posted on July 01, 2019
The reload command in Bukkit is fundamentally broken and flawed, here's why, and what you should be doing instead.