Skip to content
POLARISAI ACADEMY
R02-401 · Navigator

Full-Stack AI Application Engineering

You can allow or reject a session from the clock and the row, claim one queued job so the next worker changes 0 rows, accept 2 of 4 JSON objects and log the call on a line with 5 keys, split an SSE stream and resume from the last id, and count migration statements plus upstream fallback calls.

Level
Navigator
Time
30 h (estimate)
Prerequisites
R02-301
Course 4 of 4
Jump to

After this course you can

The student can build a full-stack application with streaming responses, database migrations, and automatic upstream failover that passes end-to-end integration and fault-injection tests.

The session row and the cookie

Authentication here is a row in the session table plus the cookie on the request. The table has three columns: token, user_name, and exp. exp is a whole minute on this clock. The handler allows the request when a row matches the cookie and now is still below exp. The worked rows are ab12 for ada with exp 1800, and cd34 for bo with exp 1700, so the table starts with 2 rows. The clock reads 1730. Cookie sid=ab12 matches ada. 1800 minus 1730 is 70, so 70 minutes remain, and the handler allows the request. The row count stays 2. Cookie sid=cd34 matches bo. 1730 minus 1700 is 30, so the clock is 30 minutes past exp. The handler answers status 401 and deletes that row. The table then holds 1 row, ab12. Cookie sid=zz99 matches 0 rows. The handler answers status 401 and deletes 0 rows, so the count stays 1.

A login compares two finished hash strings, character by character. ada's stored hash is h7, 2 characters. The checker returns h7 for the password horse, and horse has 5 characters. The hash strings match, so the handler inserts token ef56 with exp 1730 plus 60, which is 1790. The table holds 2 rows again. The checker returns h8 for the password mule. h8 and h7 differ, so the handler inserts 0 rows and answers status 401. The password horse appears in 0 session columns. The cookie on the matching login is sid=ef56; HttpOnly; SameSite=Lax. HttpOnly keeps that cookie out of page script. With SameSite=Lax the browser sends the cookie on a same-site request and on a top-level cross-site GET, and it withholds the cookie on a cross-site POST. Of those three sends, the cookie goes out on 2.

Claim one queued job

The background job queue is the job table. Each row has id, note_id, and state. state is queued, run, or done. The worked rows are id 1 queued, id 2 run, and id 3 queued, so the queued count starts at 2. The claim statement is UPDATE job SET state = 'run' WHERE id = ? AND state = 'queued', with the id bound beside the question mark. SQLite applies one write at a time. Worker A claims id 1 and the statement changes 1 row. id 1 is now run, and the queued count is 1, the remaining id 3. Worker B runs the same statement for id 1 and it changes 0 rows, because the WHERE clause still asks for queued. B leaves the model call to A. Worker A claims id 3 and the statement changes 1 row. The queued count is 0 and the run count is 3. A claim of id 2 changes 0 rows, because id 2 was already run.

The finish statement is UPDATE job SET state = 'done' WHERE id = ? AND state = 'run'. Worker A finishes id 1 and the statement changes 1 row. The run count is 2 and the done count is 1. A finish aimed at an id that is still queued changes 0 rows, because that WHERE clause asks for run. The integer you write down is rows changed. 1 means this worker took the step. 0 means the row was already past the state the clause names.

Four objects and one log line

Structured output here is one JSON object with a fixed schema. The object must have the keys sku and qty, and any other key is rejected. sku is a string of length 1 through 8, and each character is an uppercase letter or a digit. qty is an integer from 0 through 99, written with digits and with a decimal point nowhere in the token. Object 1 is {"sku":"OATS","qty":2}. sku has length 4 and qty is 2, so it passes. Object 2 is {"sku":"OATS","qty":"2"}. qty is a string, so the object is rejected. Object 3 is {"sku":"OATMEAL1","qty":0}. OATMEAL1 has length 8, its last character is a digit, and qty is 0, so it passes. Object 4 is {"sku":"OATS","qty":2,"note":"x"}. That object has 3 keys, so it is rejected. Of the 4 objects, 2 pass.

Structured JSON logging writes one JSON object on its own line for each upstream attempt. The line has exactly the keys ts, level, event, status, and calls, which is 5 keys. A passing primary call writes {"ts":"2026-11-14T17:00:00Z","level":"info","event":"upstream","status":200,"calls":1}. The value OATS appears 0 times on that line. The reply body stays in the response. The line records the call. A status 200 whose object fails the schema writes level error and event parse, with status still 200 and calls 1. The value parse has 5 characters, and the value error has 5 characters.

Three dispatched SSE events

Server-Sent Events, SSE, is the stream of blocks below. A blank line ends a block. Block 1 has the lines id: 1, retry: 3000, and data: oat. Block 2 has id: 2 and data: s. Block 3 has : ping. Block 4 has id: 3, event: done, and data: {"sku":"OATS","qty":2}. A line that begins with a colon is a comment and adds nothing to the data buffer. The id line sets the id buffer after one leading space that follows the colon is removed, so id: 1 stores the one-character string 1. retry: 3000 sets the reconnect delay to 3000 milliseconds, which is 3 seconds, because the value is all digits. data: oat contributes oat, 3 characters, and data: s contributes s, 1 character. One leading space after the colon is removed, and the rest of the value stays.

A blank line dispatches an event when the data buffer has text, then clears that buffer. The dispatched event takes the id buffer as the last event id. An empty buffer dispatches 0 events, which is what block 3 does. Blocks 1 and 2 omit the event line, so each dispatches with type message. Block 4 sets the type to done. The stream dispatches 3 events. The handler then joins the data of each message event in id order, as its own step, while the wire keeps the events separate. oat followed by s is oats, 4 characters. The done data is {"sku":"OATS","qty":2}, 22 characters, the same text as object 1. After the full stream the last event id is 3. When the socket closes after the second dispatch, the last event id is 2. The reconnect request carries the header Last-Event-ID with the value 2, and the client waits 3000 milliseconds first. This server then sends only blocks whose id, read as an integer, is greater than 2. It sends block 4, and the client receives 1 new event. The joined message text stays oats.

Up, down, and the backup host

Database migrations are the numbered SQL files. You apply the lowest missing version, and you roll back the highest version. 001_up.sql has 3 statements: create schema_migration with a version integer primary key, create note with id and body, insert version 1. 001_down.sql has 2 statements: drop note, drop schema_migration. 002_up.sql has 2 statements: create job with id, note_id, and state, insert version 2. 002_down.sql has 2 statements: drop job, delete the row whose version is 2. 003_up.sql has 2 statements: create note_sku with note_id and sku, insert version 3. 003_down.sql has 2 statements: drop note_sku, delete the row whose version is 3. On an empty file, up through 3 runs 3 plus 2 plus 2 statements, which is 7. MAX(version) is 3, and the tables are schema_migration, note, job, and note_sku, which is 4. Down from 3 to an empty file runs 2 plus 2 plus 2 statements, which is 6, and the table count is 0, so the version you read back is 0. With versions 1 and 2 already stored, MAX(version) is 2 and the table count is 3. The next up runs 003 only, 2 statements, MAX(version) becomes 3, and the table count becomes 4. One down step from 3 runs 2 statements, MAX(version) returns to 2, and the table count returns to 3. Editing 001_up.sql after versions 1 and 2 are stored changes 0 stored rows. The next change is a new file.

Upstream fallback calls the primary host, then at most one backup host. Status 200 with an object that passes the schema stops on the primary: the call count is 1 and the outcome is primary. Status 429 or status 500 calls the backup once. A backup status 200 with a passing object ends at call count 2 and the outcome backup. A backup status 429 or 500 ends at call count 2 and the outcome failed. Status 400 stops on the primary: the call count is 1 and the backup call count is 0. Status 200 with a rejected object stops on the primary as well: the call count is 1, the backup call count is 0, and the outcome is parse. The worked batch has four requests. Request A is primary 200 with object 1: calls 1, outcome primary. Request B is primary 429, then backup 200 with object 3: calls 2, outcome backup. Request C is primary 500, then backup 500: calls 2, outcome failed. Request D is primary 200 with object 2: calls 1, outcome parse. The four call counts are 1, 2, 2, and 1, and they add to 6. The backup host is used on 2 of the 4 requests.

An example

One afternoon, counted on paper

Start at minute 1730 with the two session rows and the three job rows. Cookie sid=ab12 matches ada, 70 minutes remain, and the handler allows the request. The claim of id 1 changes 1 row. The primary answers with the four SSE blocks and status 200. The stream dispatches 3 events, the handler joins the message data to oats, 4 characters, and the done block carries object 1, 22 characters, which passes the schema. Upstream calls stay 1, the outcome is primary, and the log line has 5 keys with the value OATS appearing 0 times. The database already holds versions 1 and 2, so this request runs 0 migration statements and MAX(version) stays 2.

Keep going on the same sheet. Cookie sid=cd34 is 30 minutes past exp 1700, so the handler answers 401 and the session table holds 1 row. Worker B claims id 1 and changes 0 rows. After A has also claimed id 3, the queued count is 0. Object 2 fails the schema, and that body is request D: calls 1, outcome parse, backup calls 0. Request B, primary 429 then backup 200 with object 3, uses 2 calls. Request C, primary 500 then backup 500, uses 2 calls and the outcome is failed. Adding the batch gives 1 plus 2 plus 2 plus 1, which is 6. From version 2, 003_up.sql runs 2 statements and MAX(version) becomes 3, with 4 tables.

Write one row of marks you can recompute. Minutes left on ab12: 70. Status for cd34: 401. Session rows after that delete: 1. Rows the second claim of id 1 changes: 0. Objects that pass: 2 of 4. Keys on the log line: 5. Copies of OATS on that line: 0. SSE events dispatched: 3. Last-Event-ID after a drop once id 2 has dispatched: 2. New events from this server: 1. Statements in the next up from version 2: 2. Calls in the batch: 6. Backup uses in the batch: 2.

Exercise

Check it on paper

The clock reads 200. The session rows are k1 with exp 200, and k2 with exp 260. Using the same rule, now must be below exp, write the status for k1 and the status for k2, then write how many rows remain after the expired row is deleted. The stored hash is qq and the checker returns qq. Write the exp you store on the new row when you add 60 minutes to 200, and write the row count after that insert. Jobs 4, 5, and 6 are queued. One worker claims 4, then claims 5. A second worker claims 4. Write the rows changed for each of those three statements, and write the queued count at the end. Say what the schema does with {"sku":"BEEF","qty":99}, with {"sku":"BEEF","qty":99.0}, and with {"sku":"beef","qty":1}. An SSE stream has id: 1 with data: be, id: 2 with data: ef, and id: 3 with event: done and data: {"sku":"BEEF","qty":99}. Write the joined message text. The socket closes after the first dispatch. Write the Last-Event-ID value and how many events this server sends next. MAX(version) is 1. Write the statement count when you apply 002_up.sql only, the new MAX(version), and the table count. The primary returns 500 and the backup returns 200 with the BEEF object that passes. Write the call count and the outcome. Then write the call count and the backup call count for a primary status 400.

k1 gets status 401, because now equals exp, and k2 is allowed, because 260 minus 200 is 60. After the delete, 1 row remains. The new exp is 260, and the table holds 2 rows after the insert. The claims change 1 row, then 1 row, then 0 rows, and the queued count ends at 1. {"sku":"BEEF","qty":99} passes. {"sku":"BEEF","qty":99.0} is rejected because the qty token contains a decimal point. {"sku":"beef","qty":1} is rejected because the sku has lowercase letters. The joined message text is beef, 4 characters. Last-Event-ID is 1, and this server sends 2 events, the block with id 2 and the block with id 3. 002_up.sql runs 2 statements, MAX(version) becomes 2, and the tables are schema_migration, note, and job, which is 3. The 500 then 200 path uses 2 calls and the outcome is backup. Primary status 400 uses 1 call, and the backup call count is 0.

Check questions

Answer on your own first. Then open a question to see the answer.

At minute 1730, what status does cookie sid=cd34 receive, and how many session rows remain after that delete?

Status 401. The clock is 30 minutes past exp 1700, the row is deleted, and 1 session row remains.

id 1 starts queued. Worker A claims it, then worker B runs the same claim. How many rows does each statement change, and what is the queued count after A also claims id 3?

A changes 1 row and B changes 0 rows. After A claims id 3 as well, the queued count is 0.

How many of the four worked objects pass the schema, and how many copies of OATS does the info log line contain?

2 of the 4 objects pass. The log line has 5 keys, and OATS appears 0 times on it.

The SSE socket closes after id 2 has dispatched. What is Last-Event-ID, how many new events does this server send, and how many calls does request B make?

Last-Event-ID is 2, and this server sends 1 new event. Request B, primary 429 then a passing backup, makes 2 calls.

Take with you

Score the request with the clock against exp, the rows a claim changes, the objects that pass, the 5 keys on the log line, the SSE events and the last id, the statements in the next migration file, and the calls before the backup host is used.

Final task

Submit a full-stack web application container/bundle that passes 50 automated end-to-end tests, including simulated upstream 500/429 responses, SSE stream recovery, and clean up/down SQL migrations. (Optional Portfolio item: live public demo).

The final task is marked against fixed answer keys and automated tests once the tests open.

Terms in this course

This course has no terms of its own in the glossary yet. Terms from earlier courses are used here too.

More lessons coming

The first lesson is open to read. The rest of the course is being written, and tests and diplomas come later. Join the waitlist