-3

I notice on sites like Stack Overflow and Reddit they have the ability to put the entire site in 'read only' mode e.g. during maintenance.

My question is, what kind of architecture is needed to make this happen? Does it happen at the database level, i.e. some command to 'lock' the database writes? It's a bit of a broad question, so to narrow it down I'm interested in the implementation for:

  • PHP
  • MySQL
  • Laravel
halfer
  • 19,824
  • 17
  • 99
  • 186
Gary Green
  • 22,045
  • 6
  • 49
  • 75
  • 4
    No, the database won't be set in read-only mode, though you could also do that to be on the safe side. Just lock down the UI. You'll want this flag to be available everywhere, so maybe a getter method in a base controller class? – halfer Apr 22 '15 at 14:07
  • could be something as simple as setting `readonly=1` in a config file somewhere, and having every code block that does write operations check for that flag. Setting the DB read-only would work, but most likely kill the entire code base because any write query would fail, and spit out "fatal error: unable to write your changes" everywhere. – Marc B Apr 22 '15 at 14:07
  • You really just have to decide what are the most expensive (and often least important) operations of the website - then add mechanisms to disable them. In reddit's case, that includes user logins, up/down voting, posting, and commenting. Hate to be a stickler but if you didn't have a high-rep, I'd see this question getting closed and downvoted pretty quickly. I feel like this question would be better suited for programmers.stackexchange.com? – sjagr Apr 22 '15 at 14:08
  • @MarcB: it would be a good test of the robustness of the error handlers though `:-)` – halfer Apr 22 '15 at 14:08

1 Answers1

2

Whether the database itself is locked down or not, we cannot determine.
But I would say it is unlikely, especially since you have to change the front end behaviour anyway, to avoid tons of error messages.

Instead, all interfaces (front end and APIs if present) are told to lock down, presumably via a config variable, and will then not do any write operations to the database, and probably even use a local cache instead of reading from the database.

I have no insider info on StackOverflow, but I can't think of any other way this could work properly.

As for your "implementation":

On MySQL level you shouldn't really do anything, especially since you probably need to change some things in the database during maintainance.

In PHP, you can do something like this for the APIs or all GET/POST target pages:

if(!$config['readonly'])
{
    $db->query('UPDATE `someTable` SET `whatever` = "whocares"');
}

and the same for the front end:

$template->renderViewButton();
if(!$config['readonly'])
{
    $template->renderUpdateButton();
    $template->renderLikeButton();
}

Just hide any elements that change things in the database, and don't forget to restrict the receiving pages too, as anyone with some dev knowledge can craft their own HTTP request and could probably break your website if you don't secure it (same reason adding readonly to an HTML <input> is not sufficient).

Siguza
  • 21,155
  • 6
  • 52
  • 89