While not specifically related to product variants, this community post from a while back has a reply from our team that offers some additional context on 502 errors and is absolutely worth a looking through. Based on the error and that post, limiting the volume of data per request is a good first step.
If the application built with REST, pagination would be worth exploring. If you are using GraphQL, in addition to pagination, also consider removing any requested fields/connections/fragments that aren't absolutely crucial.
Past that, if you still feel you are facing a different and unexpected behavior, please log an example request body, URL, and response headers to provide as a starting point. The x-request-id response header returned along with an error will allow us to check our logs and pass back any other suggestions.