It gets complicated. For maximum compatibility, matching the manufacturer and complete part number (except for the date code) is the easiest way to go without having to think.
But that might lock you into a more expensive part when another one should be compatible too. So for maximum flexibility, or to optimize perofrmance, you have to understand and match a number of variables.
First there are the DDR generations (DDR-DDR4) they are not even physically compatible, so you jsut have to look up what your PCU/Motherbaord expect.
Let's focus on DDR3 now, to look at some of the more subtle compatibility issues.
We'll start with functional compatability (get these wrong and your PC won't boot) The well known ones are Buffering and ECC. Servers tend to have both, laptops tend to have neither.
In both cases, you either have it or you don't. If you don't it's easy, but if you do have it, then you have to pay attention to what kind of buffering or ECC is going on.
Buffering comes in 2 kinds: Regular or Registered and LR or Load Reduced. (NOT to be confused with Low voltage) Regular is best when you want fast memory, LR is better when you want to put as much memory as possible on each CPU.
The way ECC gets implemented depends on the number of bits on each physical memory chip. That is the "x-Value" that appears in 2Rx8, 2Rx4, 2Rx4, etc. The memory controller can handle either one, but they have to match. x4 is slightly preferred because it can correct for more types of errors, but it is not any faster. (in practice, all we really use is error detection. Memory is very reliable, but if it starts dropping bits, you replace it ASAP, rather than relying on flamboyant error correction algos)
Then there is the matter of Rank (also called "high" vs "Low" density or Single vs Double sided). Think of a stick with 2 Ranks as having 2 logical sticks of 1/2 the size mounted on one physical stick. Higher rank allows more interleaving, and higher capacity modules, but it adds overhead. Generally 2R is the fastest. 4 or 8R will show up on big LR type modules.
Finally there are issues that I call "perfromance compatability". Mess those up and your computer will still boot, but it might run slower or hotter than if you get it right.
These are chiefly speed and voltage levels. Low voltage chips will typically work at the standard voltage, but you can't use standard chips in a system that is set up for low voltage. (secretly the voltage is adjustable on many mother boards, either by you or automagically by the BIOS)
Speed is more accurately thought of as Speed Limit, or best case speed. If you mix modules with different speeds, the system will operate at the lower speed. But speed also depends on which modules you put where. Putting multiple modules on the same channel is very likely to result in the system deciding to lower the clock speed.
Loading is not a big deal on a laptop with only 2 slots, but on a big server that has 2 CPUs, 4 memory channels per CPU and up to 3 Banks per channel, it can affect the speed a great deal. (We are talking factors of 5x or more, just by rearranging how the sticks are loaded into the slots)
The right way to load them is to load the channels evenly, with identical modules in each bank and compatible modules across banks.
A lot of the documentation is terrible, but the rules tend to follow the CPU around. I found this Fujitsu paper that explained my Dell Server (using the same Xeon CPUs) much better than the Dell documents did!
https://sp.ts.fujitsu.com/dmsp/Publications/public/wp-ivy-bridge-ep-memory-performance-ww-en.pdf
It's actually kind of interesting, if you are the sort of person who likes walking on broken glass or removing band-aids slowly.... :-)
I haven't even mentioned "CAS Latency" which is another performance variable. Think of clock speed as how fast your legs move when you are running. Then latency is how long it takes you to get out of your chair and take the first step, after being told to start running.
You can get all the gory details here, if you are in to that sort of thing....
https://www.hardwaresecrets.com/understanding-ram-timings/#:~:text=CAS%20Latency%20(CL)%20Impact%20on%20RAM%20Speed&text=A%20memory%20with%20CL%20%3D%207,lowest%20CL%20will%20be%20faster.