How to ensure data consistency in WordPress

I’m currently tackling the implementation of a web app with WordPress.

However, right now, I’m facing what I think is a general issue for everyone trying to implement a wep app with WordPress. Nonetheless, nobody seems to talk about it.

That issue relates to data consistency. After looking at different sources on the subject and playing for quite a while now with WordPress code, I have seen no trace of database ACID transactions or any other means to ensure data consistency. Taking into account that a user request might trigger multiple write operations on the database, it seems that such a critical issue might cause data inconsistencies, as something might fail in the middle of a request processing, causing partial writes.

This is something that appears to happen even internally in the WordPress API functions.

As an example, let’s take wp_insert_post (which is a common core API operation). It seems that this function, performs multiple write operations and they do not get executed under the scope of a database transaction. That looks right to me since the transaction lifecycle, from my point of view, should be managed at a higher application layer.

However, no transaction is used, even in higher layers.

What’s more, to make things worse, I’ve seen that wp_insert_post hides the return value of most of the write calls that it performs on the database (such as saving the thumbnail as metadata, categories, tags, etc.).

This fact, might make callers (such as other third party plugins or the wordpress admin pages themselves) think everything has been saved correctly to the database when in fact it’s not, preventing them from noticing it, so there would be no way for them to handle the transaction properly (knowing when to commit or rollback) or giving appropriate feedback to users.

Am I missing something? How can one make data, at least, eventually consistent in WordPress apps?

How can we make sure that, in general, developing a web app with WordPress, will not lead to a nightmare, trying to manage these kind of data inconsistencies?

Any help in this respect would be really appreciated.

1 Answer
1

No, you are not missing something. But you are using the wrong “framework” (which WordPress is not) for your use case, it seems. And you are mixing things up. What you are talking about, A.C.I.D. transactions, is by no means what you think it is for.

  • ACID: save crash recovery (due to soft- or hardware fails)
  • Locking: Exclusive writes to rows and other parts

Both can be done by using the databases that WordPress supports … with the right engine:

  • MySQL: InnoDB
  • MariaDB: XtraDB (an InnoDB fork)

Take a look at InnoDB Locking for more details.

The alternative is to have either hard disk locks (write a LOCK file to disk) or, if you are in the same request, then use a software lock (a variable or constant that you switch to TRUE/FALSE). The third option would be to set the lock natively in the database.

More information in this answer here on the site.

Leave a Comment