Short answer: Depends on the engine.
Most of the cars have the max. hp and torque
@ X rpm written down in the Car Specs tab (far right in the Advanced setup menu, called 'Details' in my English version) and many also feature a graph with more precise hp and torque curves.
This for example is the one for the VW Golf VI R Cup:
As you can see you have most torque between 2-4k rpm (blue line in Nm) and the hp (red line in PS) rising pretty consistent up until 6000rpm, where the power output of the engine plateaus. Just over 7000rpm both torque and power output drastically drop off, so it's quite clear that with this engine there's no advantage to running at a higher rpm than that because it's not providing any further power increase going over that rpm number.
Instead (as you unfortunately found out) the engine runs at a speed it's not designed to endure for extended amounts of time and will take damage over time. How much damage that is is very mod-dependent - some are very lenient and others less so.
Going away from the Golf example now with certain other engines you will continue to gain power output up into and throughout the range where the engine will already take damage over time when running at or near max rpm, making the higher rpm setting(s) viable in Qualifying whereas you'll need to tune it down somewhat for a whole race distance.
For example in the 24h Le Mans for decades the drivers were instructed to run no higher than a certain rpm number for the majority of the race, sacrificing even seconds per lap in the process but keeping the (at that time still more fragile) engine in one piece to survive the race of attrition that it was.
So checking in which rev range the engine performs best and then also keeping an eye on the engine health during Practice for a couple of laps on the rpm setting you set should keep an engine failure like yours from happening again in the future.
