|
| 1 | +# One Database, many services |
| 2 | + |
| 3 | +Often when a microservices architecture is assembled, each service has its own state (which |
| 4 | +manifests itself as a database for each service). I've worked on a couple different projects where |
| 5 | +this architecture has prevailed and it's made tracing the effect of a given request on a system |
| 6 | +really difficult. |
| 7 | + |
| 8 | +In Backstroke, the database is treated like any other service. As few services are given database |
| 9 | +access as possible, and unidirectional data flow is enforced whenever possible to minimize the |
| 10 | +bugs related to the system's shared mutable state. |
| 11 | + |
| 12 | +Currently, only two bits of code have database access - the `server` service and the |
| 13 | +`operation-dispatcher` job. |
| 14 | + |
| 15 | +The `server` service handles all of the CRUD operations for links and link operations. CRUD |
| 16 | +intrinsically requires some sort of persistence layer in order to store the state of the resource |
| 17 | +being acted upon. Most importantly, this is the only service that writes to the database. |
| 18 | + |
| 19 | +The `operation-dispatcher` job handles automatic link updates. Once every ten minutes, it checks to |
| 20 | +make sure that a link is up to date, and if it isn't, it will dispatch a new link operation into the |
| 21 | +link queue. |
| 22 | + |
| 23 | +## What about redis? |
| 24 | +The Backstroke system does have one common bit of shared state: they all communicate with each other |
| 25 | +using Redis. However, two important characteristics have been carefully designed around to minimize |
| 26 | +any problem that this shared state would have: |
| 27 | + |
| 28 | +1. Redis is used as a temporary cache to hold all items in the link operation queue, and to store |
| 29 | +the response of link operations. These are both ephemeral values and expire after 24 hours. If redis |
| 30 | +were cleared and all services restarted, the system would loose no permanant data. |
| 31 | + |
| 32 | +2. Outside of the queue, Redis is used as an append-only data source where everything expires. This |
| 33 | +was purposely chosen to attempt to eliminate potential sources for race conditions and other |
| 34 | +nasty, hard to detect bugs. The queue is managed by rsmq, an external queueing system, which helps |
| 35 | +to make this data storage mechanism safer. |
| 36 | + |
| 37 | +# Database Schema |
| 38 | +(coming soon) |
0 commit comments