Developer Chats - Petr Petrenko [0:00:00] Noah Labhart: Hello, listeners. Today we are continuing our series entitled Developer Chats, hearing from the large-scale system builders themselves. In this episode, we are talking with Petr Petrenko, senior PHP backend engineer. Petr will take us through his developer journey and working on large-scale backends, managing the tension between stability and innovation, and designing systems to interact with culturally different economies. economies. Well, Petr, thank you for being on the show today. Thanks for being on Code Story. [0:00:32] Petr Petrenko: Thanks for having me. Excited to chat today. [0:00:35] Noah Labhart: I'm excited to dive into all of your experience and where you can enlighten us on supporting large-scale systems and all the things in your experience. But before we do, tell me and my audience a little bit more about you. [0:00:47] Petr Petrenko: I am in the camp for about 15 years, most focusing on high-level systems. That's something I really really enjoy. Before that, I spent around 10 years building providers' networks as a network administrator. I think that experience gave me a solid foundation for understanding how a coupling system works at each level. It also told me something really valuable, that using a result solution can be just as important as creating new ones. You know, when you're an admin, you don't usually write new code, you work with what you already have. And that mindset really helps me with legacy projects. You try to get the most out of what have. In my free time, I enjoy DIY electronics projects. [0:01:26] Noah Labhart: Very cool. I appreciate that overview. Let's dive into it then. So you've worked on large-scale backends that serve millions of users. At what point do systems start to outgrow the teams that built them? [0:01:38] Petr Petrenko: I think problems usually start when the team stops really understanding what they are really doing. They kind of lose focus on the main goal and start implementing things that don't really make sense together. So in most cases, it's actually more of product issues than a technical one. Another big challenge comes when the original people who are designing the system leave the team. It's really hard to pass on the deep conceptual understanding of how the architecture works and how the project should evolve in the future. [0:02:08] Noah Labhart: Yeah, I've experienced those things as well. So at some point, every mature backend reaches a stage where rewriting is no longer realistic. How do you recognize when a system has crossed that line? And what's the right way to handle it? [0:02:22] Petr Petrenko: Rewriting is always possible. But the real question is why are you doing it and how much it is going to cost. For me, rewriting should always be the last resort. It only makes sense in two cases. The first, when you just can't move forward with the current setup and need to change the whole tech stack, like moving from a web app to a native mobile app. has drifted too far from the original requirements. In other cases, good refactoring and cleanup are almost always a better option. I've had situations where people wanted to rewrite everything because the code was messy and hard to maintain, but they still needed to keep all the business logic. I managed to convince them to start with refactoring and documentation, and after that, everyone agreed the system was fine to work with. One of the best examples was a legacy project where we handled it on outsourcing. It was all about generation reports, and each one took 10 to 30 minutes. The project was more than 10 years, and the owner was ready to pay for a full rewrite. But after analyzing the system, I found the real issue, an outdated database. The queries were too heavy, optimize them. We couldn't just upgrade the database, so we fixed the queries, and in two weeks, reports went from 30 minutes to 10 seconds. If we had rewrite everything, it would take a last half of year. The owner was super happy as they fixed cost less than 10% of planning budget. So it is really important to stay focused on solving real problems, not just experimenting with new tech for fun. [0:04:10] Noah Labhart: tension between stability and innovation. How do you decide when a system needs refactoring versus when you just need to live with the technical debt? [0:04:19] Petr Petrenko: When you are working on a live project, there will always be some technical debt. That is normal. The important thing is to keep it under control so it doesn't cause real problems. Like I said before, it's all about clearly understanding what issue you are actively solving. For example, if your project is growing really really fast, but onboarding a new employee takes two years, that is a serious bottleneck. But if you have a project with a lot of technical debt with a small 30-minute but fix each half year, maybe you don't need to touch it all. Innovation should always come from user needs, not from your personal desire to try a new technology or add something to your CV. I actually gave a talk recently about how to unjustify its use of FII caused a lot of issue in one project. [0:05:05] Noah Labhart: Very good. Let's talk about the human side of legacy systems. What have you learned about culture, documentation, and knowledge transfer that keeps old systems alive and reliable? [0:05:16] Petr Petrenko: In any fast-growing project, there is always a problem with documentation. It's either incomplete or outdated. On one of my projects, we ever had a joke that the most completed and up-to-date documentation could only be found in a smoking area. Not many people enjoy writing documentation, but almost everyone is happy to share details about their component. So it's really important to stay in touch with your teammates. [0:05:42] Noah Labhart: No doubt, staying in touch with your teammates is absolutely critical. You've also built and maintained complex payment systems for global users. What's something most engineers underestimate about cross-border transactions? Oh, [0:05:55] Petr Petrenko: that is one of those situations where you can't really tell how much money you actually made today. A transaction happens with one exchange rate, the payment is processed with another and the conversion used a third one. In such cases it becomes really hard to detect anomalies or fraud. I once worked on a project that had grown quite critically before I joined it. The developers had not been very careful with rounding and currency conversion. At first it wasn't a big deal, but as the project grew, the delta in reports reached $100 and $1,000. The problem of this system is that they are usually built for a single country of market, because that is what was needed. Other logic is designed to route the entry specific to that market. market. Then, after someone decides to expand the system to other countries or use cases, management expects it to be fast and simple. That is when ugly workarounds appear, breaking the logic and slow down future development. For example, imagine you built a system for selling digital services, subscriptions or consumables all in whole numbers. But then suddenly you are running a marketplace where prices are per kilogram and products By the way, I have a funny story about one B2B marketplace. Someone a long time ago had a rule in the code that order item couldn't cost more than $1 million. Everything worked fine, until one day a customer tried to buy an Pilatus Saffron. That's about a ton of the most expensive spice, which can cost from $2 to $10 per 1 gram. Of course, the system just refused the order until the team fixed it. That's a pretty common situation in a successful project. You never really know what will break next. My main advice is always be careful with numbers. If you find even in one cent difference, track it down and fix it. [0:07:57] Noah Labhart: No doubt, you can't leave those things lingering. When you're designing systems that deal with different currencies, laws, and tax regulations, how do you balance the technical with the ethical? For example, user privacy or data sovereignty? [0:08:12] Petr Petrenko: is quite well regulated by law, we just follow the rules. For example, GDPR gives very clear rules on what information can store, when and how. You just need to understand it properly and stick to the requirements. A lot of issue comes from user hotel. Let's imagine you have a user registered in Europe and accepted EU terms and conditions, but he is in Japan and buy a service for Asian market, and he pays with an American payment method. All these kind of cases need to be careful through in advance. Laws that require storing user data only within a specific country also make things more complicated. In such conditions, it's really easy to break some rules without even realizing it. And sooner or later, almost everyone ends up a paying a fine. But overall, the industry has learned how to handle these challenges pretty well. Most companies are now much more responsible and take data protection seriously. [0:09:08] Noah Labhart: So for the engineers listening who want to build something durable, not just fast, what advice would you give about writing code that will still make sense years from now? [0:09:19] Petr Petrenko: I think the idea to build something durable is actually a mistake. Our real goal is to create a product that users need right now. I have seen small teams with big ambitions. They think they are building the next Google, but they overcomplicated everything, trying to predict what will happen after 10 years. In the end, they run out of budget, and the project gets shut down. You can just create universal code that will work perfectly in every possible future scenario. Of course, you shouldn't go to other extreme ether, when the whole project becomes a collection of hacks and quick fixes that no one really understands. an universal answer on how to find the right balance. Because every situation is different. But what really helps is taking part in all kinds of projects. It gives you a perspective and helps you see the long-term result in different decisions. Learn from others, don't refrain the contributor open source projects, and of course, build your own pet projects. [0:10:19] Noah Labhart: That's very good advice. One of your most impressive projects is a high-performance image matching system you built yourself, capable of scanning tens of millions of images with second results. Can you walk us through the moment you realized you needed to redesign the system, and what engineering choices made that level of performance possible? [0:10:40] Petr Petrenko: That was an amazing project. It was about external validation of real estate ads. The whole system was built around comparing photos, even after they had been modified, things like cropping, rotation, or local edits. Fuzzy image matching is a real problem for computer science. We could have limited it to just one or a few cities, but we want to check on the platform. That meant up to 150,000 photos every 24 hours. The database could contain more than 100 million photos after a few years. So, the requirement was to able to search an average two photos per second across a database of 100 million photos. That set back pretty high right from the start, and I needed to find a solution that was also financially reasonable. It took quite a lot of time to build a working prototype. I had a special test database of 1 million photos that I used to compare different approaches. The first one was a simple linear search algorithm. Search 1.14 database of 1 million took about 4.5 hours. Obviously, it was not acceptable. Then I tried many other methods like H-engine, VP trees, and very special engines. I implemented almost all of them in PHP. That is my main language. But the problem wasn't the performance of the language. It was the algorithm complex of the task itself. No amount of optimization could fix that. Finally I found the interesting data structure that helped me reach the performance I needed. I built a prototype in PHP, it worked well, but I knew it could be faster. So I wrote core on the C and added multithreading support that gave me the required performance. After that I added a lot of additional optimization, like non-blocking updates when adding new photos to the database. base. And memory optimization. The system requires a huge amount of RAM. About 5 gigabytes per million photos. Before optimization, it was more than 10 gigabytes. To achieve this result, I had, firstly, definitely clear performance requirements with measurable metrics. Secondly, do research on suitable algorithms, including review academic papers. Thirdly, build a testing environment to check solutions. [0:12:52] Noah Labhart: Okay, one more question. You've also worked on billing systems and fraud mitigation at scale. Was there ever a moment when you had to choose between a technically quote-unquote clean solution and a solution that better protected users or the business? And really, what I'm looking for is, how did you make that call? To [0:13:09] Petr Petrenko: be honest, your question sounds a bit strange to me. Data protection has always been my top priority. If I see a technical clean solution that isn't secure, I will do everything I can to stop it from going to production until all the security issues are fixed. And of course, it would pass an internal audit anyway. I think that mindset is pretty standard for engineers who work with billing systems. Users can forgive a lot of things, but they will never forgive losing their money. So security is the number one priority for all my colleague as well. And honestly, anyone who will think differently doesn't stay in this field for long. [0:13:48] Noah Labhart: Well, I certainly agree with that. Well, Petr, thank you for being on the show today and highlighting your experience. You've built some amazing things around large-scale backends that serve millions of users, processing images, and I appreciate you walking through how you made those decisions. I think the engineers tuning in will really give value out of learning from your approaches approaches to these systems, whether it be new builds, legacy systems, payments, all the things, and how important security is to factor in throughout the whole process. Thanks again for being on the show today. [0:14:20] Petr Petrenko: Thanks, Noah. I enjoyed it. [0:14:22] Noah Labhart: As you can see, Petr knows his stuff. The way that he approaches large-scale systems, keeping them highly performant, secure, and awesome is quite impressive. If you'd like to connect with Petr, check the show notes for all the appropriate links. And thanks again for listening.