What are the reasons for using 9 engines in the first stage of the Falcon 9? Why not 8 or 10? Looking at the Raptor engine, they seem to be looking at 9 engines again. Is there some specific advantage to using 9 engines?
SpaceX initially was looking to buy an engine but could not find one on the market that would allow them to meet their goals.
Once they decided to develop their own, they had to consider their goals: Reliability, and reduced costs.
Nine engines means at most any time in the flight profile, you can handle an engine out event. The Saturn V with 5 F-1 engines experienced this, and took advantage of it. A SpaceX Falcon 9 1.0 on a CRS mission lost an engine, and continued to deliver the primary payload to orbit. (Secondary payload could have been delivered, but NASA safety rules around ISS orbits meant they did not meet the criteria to release the payload with sufficient certainty it would not impact the ISS if things went wrong).
Beyond simple launch, they have planned on recovery for a long time. The theory being you cannot reduce prices seriously if you keep throwing away the hardware each flight.
In order to land a first stage you need to get it through the atmosphere slow enough to survive. Parachutes did not work, but retrothrust did.
A first stage, at launch, needs all the thrust it can get as it is over 90% fuel and oxidizer.
A first stage on landing is only 10% the weight, and in fact the problem is you cannot throttle the engines low enough.
But by using a large (9) number of engines, you can control thrust by throttling all nine as needed, and shutting down engines as needed. In fact during launch, they shut off two engines midway through the flight to reduce thrust as the vehicle burns off fuel and oxidizer.
For first stage recovery they fire three engines (It is unclear if the first two of three burns use 3 engines, or just the first), and finally one engine, which throttled down as low as it goes, still has too much thrust to actually hover. But it allows them to do the 'hover-slam' approach they use. (Come in fast, thrusting to slow down so that you hit the ground at 0 m/s).
The use of 9 engines (or 5, or 3) gives them a center engine, that is balanced for landing. It allows throttling. It allows engine out capability. It is not crazy like the N-1 with 30 engines. It is a reasonable tradeoff that seems to be working well with a dozen flights so far.
Around 2005, plans changed. Due to customer requirements SpaceX wanted a version with 9 engines. They extended the 1+3+1 configuration to a 3x3 grid, giving maximal commonality.
The Falcon 5 was cancelled eventually, but by then the Falcon 9 design was close enough to finished that they kept using the 3x3 grid arrangement for the first few launches.
The arrangement was later changed to the 8+1 "Octaweb" we see on the Falcon 9v1.1, which uses a more efficient (lighter) structure to transfer the thrust from the engines to the fuselage.
Many rocket manufacturers use different engines for different stages which are tailor-made for their purpose.
SpaceX, on the other hand, tries to save cost by using as many standardized parts as possible. So instead of developing new engines from scratch for every single purpose, they just developed one rocket engine for the lower stages of all of their rockets: the Merlin. Yes, there are the variants 1A, 1B, 1C and 1D, but these are incremental engineering improvements for an engine which all serve the same purpose. The only variant of the Merlin which is a spinoff and not an incremental improvement is the vacuum version of the C and D engine, which are in many ways very similar to the normal version.
But what do they do when their engine is too weak and they require more power, like on the first stage of a medium-lift rocket like the falcon 9? They just use more of them. 9 engines happens to be the ideal thrust-to-weight ratio for the first stage of the Falcon 9 rocket.
As you increase the number of rocket engines certain problems begin to arise. Back pressure increases, vibration increases, - In both cases I think that this is owing to the fact that all the engines must, at some point, all lead to a single fuel line from the fuel tank where the effects of a single engine all start to become compounded - I suppose stability in general. In summary, you cannot simply add more engines and expect more power without serious negative implications - It's very much a compromise. Hopefully someone with more expertise can elaborate on my answer.