This can be achieved by using powerful enough hardware that doesn't become fully loaded while gaming and capturing at the same time.
Use hardware encoding. The best hardware encoder in general still seems Nvenc from Nvidia GPUs. It isn't just the quality of the encoded video stream, it seems Nvidia achieved a very good and matured embedding of the encoder in the graphics engine in general, so getting data out (capturing) doesn't interfere much with getting data in (game displays its stuff).
Make sure computer load is balanced between game and OBS, so neither of them will starve for resources so it begins to stutter. This means you need to limit your game to not consume all GPU resources, since games tend to do that if they're not limited. Don't run your game with unlimited fps.
If you want to stream and record simultaneously, you might probably want to record with a higher quality than the stream. In this case you will run 2 encoder sessions at the same time: one for encoding the stream in CBR rate control and one for encoding the recording with a quality based rate control such as CQP. Make sure your GPU is capable of doing that. If it isn't, it might be necessary to resort to set "(use stream encoder)" as encoder for the recording, which just saves to disk what is being sent as stream.
If it comes to "optimizations", my recommendation: create a new profile with all default settings. Don't "optimize" anything. Just do essential settings like resolution, paths, encoder, bitrate (for streaming), quality (for recording). Nothing else. Now test how it works. Depending on the outcome, you might need to change other settings to overcome shortcomings, but do this only if there are issues. Don't "optimize" if there is no need to optimize, since the defaults are already as optimized as possible.