Is it possible to write a scalable distributed backend?
Yes of course. It is not so hard. But do not bother with the microservice architecture.
- Avoid communications between backends. However, there is an exception: Find the places, where CPU is burnt. Typical places like image transformation, OCR, Excel export and so on. These are good candidates for the distribution where you can use a backend to backend call. The price for the network is lower than the price for the computation. You do not have to care about the consistency – if there is some problem, you can compute it again. The computed result is stored or not. Nothing between. Consider using queues instead of direct calls.
- Do not use locks on the backend side. Use locks on the database level. Do not use application level locking. Such locking is valid only within one instance and not the entire system. Use the database directly without ORM. You will have more control over SQL.
- Do not use HTTP sessions. Use short live cache on the HTTP request instead. It is difficult to synchronize session content from one instance to another.
- Assign a unique request ID for each request. You will be able to track calls from the API.
- If you want to maintain consistency, stay with a relational SQL database. If you want to really scale the database, use sharding. But be careful, shards should be as independent as possible.
- Be careful when writing things like scheduler. We do not want the scheduled task to run multiple times.
- Be careful when picking items from the shared queue. If there are multiple consumers, you must ensure that the item is not taken by two instances at the same time.
2 thoughts on “How to write scalable backend”