Water Sensor to detect backed up drain pipe?
I once had the exact same problem. My drain pipe was only just a little bit slower than the drain pump in the washer, so the water rose relatively slowly, but fell quickly when the machine was shut off for 5 seconds. My switch would activate several times on every drain cycle, and effectively managed the problem. (And yes, before building my gadget, I did try to unclog the drain itself, but as it ran some 20 feet across the basement, embedded in the floor slab, I didn't have much luck with that. I suspect that it simply hadn't been installed with enough slope, and fixing that would have required jackhammering it out and installing a new one.)

I used a float switch that came (IIRC) from an old dishwasher. The wires were kind of short, so I simply extended them by soldering and then heat-shrinking the joints. The switch has a square base with four mounting holes, so I fashioned a simple L-bracket to hold it upside down inside the top of the drain pipe, with the L-bracket simply taped/zip-tied to the outside of the pipe. This meant that the switch became normally-closed, and it opened if the water rose too high.

The control box plugged into the wall, and then the washing machine plugged into the box. It contains a 25-amp SSR and a small isolated 12VDC power brick, so the control circuit is completely isolated from the line power. The float switch triggers a 555 timer that cuts off power to the washing machine for 5 seconds each time it opens, which in my case was about right to allow the pipe to drain sufficiently. Obviously, this was an older machine with an electromechanical timer, which was unfazed by such treatment. I don't know if this would work with a modern microprocessor-based washing machine.

We used it for quite a while, until we moved out of that house. That was 20 years ago, but I still have the switch and the control box. If you're having trouble visualizing any of this, I can take a picture or two.